2013-12-23 103 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也會改變。

相關問題