2013-12-23 169 views
2

我有一个简单的应用程序,其中ObservableCollection正在代码中更新,并且当添加新项目时UI也被更新。要更新UI,我使用Dispatcher,它作为属性传递给ViewModel。我的代码是作品,但我不知道我是否合适。如何在ObservableCollection更新时更新UI?

下面是代码:

MainWindow.xaml.cs

/// <summary> 
/// Логика взаимодействия для MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    MainWindowViewModel model = new MainWindowViewModel(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = model; 
     this.model.dispatcher = this.Dispatcher;  
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     string url = urlToCheck.Text; 

     Task task = new Task(() => 
     { 
      model.GetUrls(url); 
     }); 

     task.ContinueWith((previousTask) => 
     { 
      label.Content = "Все ссылки собраны."; 
     }, 
     TaskScheduler.FromCurrentSynchronizationContext()); 

     label.Content = "Идёт сбор ссылок..."; 
     task.Start(); 
    } 
} 

MainWindowViewModel.cs

class MainWindowViewModel 
{ 
    public ObservableCollection<Url> Urls { get; set; } 
    public bool NeedToGetResponseForChildUrls { get; set; } 
    public bool NeedToDeletePreviousResults { get; set; } 
    public Dispatcher dispatcher; 

    some code..................... 


     **and something like this i am updating ObservableCollection:** 

     if (NeedToDeletePreviousResults) 
      { 
       dispatcher.Invoke(() => 
       { 
        Urls.Clear(); 
       }); 

      } 

Url.cs

using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace CheckUrl 
{ 
    public class Url : INotifyPropertyChanged 
    { 
     private string _absoluteUrl; 
     public string AbsoluteUrl 
     { 
      get { return _absoluteUrl; } 
      set 
      { 
       if (_absoluteUrl != value) 
       { 
        _absoluteUrl = value; 
        OnPropertyChanged("AbsoluteUrl"); 
       } 
      } 
     } 

     private int _responseStatusCode; 
     public int ResponseStatusCode 
     { 
      get { return _responseStatusCode; } 
      set 
      { 
       if (_responseStatusCode != value) 
       { 
        _responseStatusCode = value; 
        OnPropertyChanged("ResponseStatusCode"); 
       } 
      } 
     } 

     private string _responseStatusDescription; 
     public string ResponseStatusDescription 
     { 
      get { return _responseStatusDescription; } 
      set 
      { 
       if (_responseStatusDescription != value) 
       { 
        _responseStatusDescription = value; 
        OnPropertyChanged("ResponseStatusDescription"); 
       } 
      } 
     } 

     public enum Status { Working, Broken }; 

     private Status _urlStatus; 
     public Status UrlStatus 
     { 
      get { return _urlStatus; } 
      set 
      { 
       if (_urlStatus != value) 
       { 
        _urlStatus = value; 
        OnPropertyChanged("UrlStatus"); 
       } 
      } 
     } 

     private string _color; 
     public string Color 
     { 
      get { return _color; } 
      set 
      { 
       if (_color != value) 
       { 
        _color = value; 
        OnPropertyChanged("Color"); 
       } 
      } 
     } 

     private ObservableCollection<ChildUrl> _childUrlsValue = new ObservableCollection<ChildUrl>(); 
     public ObservableCollection<ChildUrl> ChildUrls 
     { 
      get 
      { 
       return _childUrlsValue; 
      } 
      set 
      { 
       _childUrlsValue = value; 
      } 
     } 

     /// <summary> 
     /// Конструктор класса Url. 
     /// </summary> 
     public Url(string absoluteUrl, int responseStatusCode, string responseStatusDescription, Status urlStatus, string color) 
     { 
      this.AbsoluteUrl = absoluteUrl; 
      this.ResponseStatusCode = responseStatusCode; 
      this.ResponseStatusDescription = responseStatusDescription; 
      this.UrlStatus = urlStatus; 
      this.Color = color; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 
+0

你能显示你的Url类代码吗? – Vishal

+0

您应该更喜欢在模型中使用FromCurrentSynchronizationContext以避免耦合到UI框架 –

+0

在这样的窗口类中添加逻辑代码也是不好的形式。在模型中创建一个ICommand属性,为其分配一个RelayCommand并将其绑定到该按钮的Command属性而不是Click处理程序。 –

回答

2

ObservableCollection可以使用绑定自动更新您的UI。使用ObservableCollection的列表并添加/删除is的项目。做的ObservableCollection作为公共property.In的主窗口的构造函数写:

This.DataContext=This; 

使用绑定到你的列表框/你需要显示出对项目的任何其他控制。 ObservableCollection已经在其中实现了IINotifyPropertyChanged。一旦你改变ObservableCollection中的项目,你的UI也会改变。

相关问题