티스토리 뷰
JSON ListView : http://susemi99.kr/1651
준비 :
[프로젝트명] 우클릭 - [Nuget 패키지관리] 클릭 -"Newtonsoft.Json" 다운로드
http://www.gamedevforever.com/255 에서 제공해 준 dll을 참조
App.xaml
1 2 3 4 5 6 7 8 9 | <Application x:Class="MVVM_Json_ListView.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVM_Json_ListView" Startup="Application_Startup"> <Application.Resources> </Application.Resources> </Application> | cs |
App.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using MVVM_Json_ListView.View; using MVVM_Json_ListView.ViewModel; namespace MVVM_Json_ListView { /// <summary> /// App.xaml에 대한 상호 작용 논리 /// </summary> public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { MainView mv= new MainView(); mv.DataContext = new MainViewModel(); mv.Show(); } } } | cs |
MainView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <Grid> <Button Content="Clear" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="75" Command="{Binding ClearCommand}"/> <Button Content="Refresh" HorizontalAlignment="Right" Margin="0,10,90,0" VerticalAlignment="Top" Width="75" Command="{Binding RefreshCommand}"/> <ListView x:Name="IssueListView" Margin="10,42,10,10" ItemsSource="{Binding Path=Issues}"> <ListView.View> <GridView> <GridViewColumn Header="Subject" Width="580" DisplayMemberBinding="{Binding Subject}"/> <GridViewColumn Header="Author" Width="100" DisplayMemberBinding="{Binding Author}"/> <GridViewColumn Header="Done %" Width="50" DisplayMemberBinding="{Binding Done}"/> </GridView> </ListView.View> </ListView> </Grid> | cs |
MainView.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using MVVM_Json_ListView.ViewModel; namespace MVVM_Json_ListView.View { /// <summary> /// MainView.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainView : Window { public MainView() { InitializeComponent(); DataContext = new MainViewModel(); } } } | cs |
ViewModelBase.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.ComponentModel; namespace MVVM_Json_ListView.ViewModel { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if(PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } | cs |
MainViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | using MVVM_Json_ListView.Model; using Apparelbase.MVVMBase; using System.Windows.Input; using System.Net.Http; using Newtonsoft.Json.Linq; namespace MVVM_Json_ListView.ViewModel { public class MainViewModel : ViewModelBase { #region Constructor public IssueModel Issues { get; set; } public string SubjectToRefresh { get; set; } public string DoneToRefresh { get; set; } public string AuthorToRefresh { get; set; } public MainViewModel() { Issues = IssueModel.Current; } #endregion private DelegateCommand clearCommand; public ICommand ClearCommand { get { if(clearCommand == null) { clearCommand = new DelegateCommand(Clear); } return clearCommand; } } private void Clear() { Issues.Clear(); } private ICommand _RefreshCommand; public ICommand RefreshCommand { get { if(_RefreshCommand == null) { _RefreshCommand = new DelegateCommand(async delegate () { string json = await RequestJson(); JObject obj = JObject.Parse(json); JArray array = JArray.Parse(obj["issues"].ToString()); foreach(JObject itemObj in array) { SubjectToRefresh = itemObj["subject"].ToString(); DoneToRefresh = itemObj["done_ratio"].ToString(); AuthorToRefresh = itemObj["author"]["name"].ToString(); Issues.AddAIssue(SubjectToRefresh, DoneToRefresh, AuthorToRefresh); } }); } return _RefreshCommand; } } private async Task<string> RequestJson() { string url = "http://www.redmine.org/issues.json"; HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync(url); string result = await getStringTask; return result; } } } | cs |
Issue.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System.Collections.ObjectModel; namespace MVVM_Json_ListView.Model { public class Issue { public string Subject { get; set; } public string Done { get; set; } public string Author { get; set; } public Issue(string Subject, string Done, string Author) { this.Subject = Subject; this.Done = Done; this.Author = Author; } } public class IssueModel : ObservableCollection<Issue> { private static object _threadLock = new Object(); private static IssueModel current = null; public static IssueModel Current { get { lock (_threadLock) { if (current == null) { current = new IssueModel(); } } return current; } } private IssueModel() { } public void AddAIssue(string Subject, string Done, string Author) { Issue New_Issue = new Issue(Subject, Done, Author); Add(New_Issue); } } } | cs |
'C# > WPF' 카테고리의 다른 글
MVVM 예제 - Multiple Views (0) | 2017.07.10 |
---|---|
MVVM 기본 세팅 (0) | 2017.07.09 |
MVVM 예제1 (0) | 2017.07.09 |
WPF tutorial - ListView sorting (0) | 2017.07.04 |
WPF tutorial - ListView grouping (0) | 2017.07.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BOJ
- BFS
- MVVM
- Add TapGesture
- 문자열
- dfs
- 데이터 바인딩
- WPF
- XAML
- Grid
- Cell Animation
- command
- FEED
- listview
- UIView Animation
- 생활코딩
- 백준
- 타일링
- Fakebook
- 코딩야학
- 그래프
- 백준온라인
- Custom Cell
- CollectionView
- CustomCollectionViewCell
- 스택
- 객체
- DP
- C++
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함