7

Tôi có một trang Silverlight lấy dữ liệu của nó từ một lớp mô hình khung nhìn tổng hợp một số dữ liệu từ các dịch vụ miền khác nhau (dịch vụ RIA). Lý tưởng là tôi muốn trang có thể kết nối dữ liệu với các thuộc tính của đối tượng mô hình khung nhìn, nhưng vì DomainContext.Load thực thi truy vấn một cách không đồng bộ, dữ liệu không có sẵn khi tải trang.Làm cách nào để sử dụng DomainContext.Load để điền các thuộc tính của ViewModel của tôi?

trang Silverlight của tôi có XAML sau:

<navigation:Page x:Class="Demo.UI.Pages.WidgetPage" 
       // the usual xmlns stuff here... 
       xmlns:local="clr-namespace:Demo.UI.Pages" mc:Ignorable="d" 
       xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 

       d:DataContext="{d:DesignInstance Type=local:WidgetPageModel, IsDesignTimeCreatable=False}" 

       d:DesignWidth="640" d:DesignHeight="480" 
       Title="Widget Page"> 
     <Canvas x:Name="LayoutRoot"> 
      <ListBox ItemsSource="{Binding RedWidgets}" Width="150" Height="500" /> 
     </Canvas> 
    </navigation:Page> 

My ViewModel trông như thế này:

public class WidgetPageModel 
{ 
    private WidgetDomainContext WidgetContext { get; set; } 

    public WidgetPageModel() 
    {   
     this.WidgetContext = new WidgetDomainContext(); 

     WidgetContext.Load(WidgetContext.GetAllWidgetsQuery(), false);    

    } 

    public IEnumerable<Widget> RedWidgets 
    { 
     get 
     { 
      return this.WidgetContext.Widgets.Where(w => w.Colour == "Red"); 
     } 
    } 
} 

Tôi nghĩ rằng phương pháp này phải sai lầm cơ bản bởi vì bản chất không đồng bộ của Load nghĩa là widget danh sách không nhất thiết được phổ biến khi dữ liệu ListBox liên kết. (Điểm dừng trong kho lưu trữ của tôi cho thấy rằng mã để điền vào bộ sưu tập đang được thực hiện, nhưng chỉ sau khi trang hiển thị.)

Có thể ai đó vui lòng chỉ cho tôi cách phù hợp để thực hiện việc này không?

Trả lời

4

Phần còn thiếu của câu đố là tôi cần phải nâng cao sự kiện khi các thuộc tính thay đổi.

cập nhật ViewModel của tôi là như sau:

public class WidgetPageModel : INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private WidgetDomainContext WidgetContext { get; set; } 

    public WidgetPageModel() 
    {   
     this.WidgetContext = new WidgetDomainContext(); 

     WidgetContext.Load(WidgetContext.GetAllWidgetsQuery(), 
      (result) => 
      { 
       this.RedWidgets = this.WidgetContext.Widgets.Where(w => w.Colour == "Red"); 
      }, null);    

    } 

    private IEnumerable<Widget> _redWidgets; 
    public IEnumerable<Widget> RedWidgets 
    { 
     get 
     { 
      return _redWidgets; 
     } 
     set 
     { 
      if(value != _redWidgets) 
      { 
       _redWidgets = value; 
       RaisePropertyChanged("RedWidgets"); 
      } 
     } 
    } 
} 

Các điều khiển ràng buộc để các đặc tính này được cập nhật khi các đám cháy sự kiện thay đổi tài sản.

Các vấn đề liên quan