简体   繁体   English

MVVM-模型和视图模型的数据层

[英]MVVM - data layer of models and viewmodels

I am refactoring my application to utilize MVVM. 我正在重构应用程序以利用MVVM。 I used to keep a List<Product> variable in the Application class that I was able to bind a ListView to. 我曾经将List<Product>变量保留在能够绑定ListView的Application类中。 This List made up my Data layer. 该列表构成了我的数据层。 The Page with this ListView is a master/detail layout. 具有此ListView的页面是主/详细布局。 With MVVM, I am thinking that the List should now hold instances of the ProductModel as it is the data layer. 使用MVVM,我认为List现在应该包含ProductModel的实例,因为它是数据层。 If I should be binding to ViewModels, do I need a separate List of ViewModels too? 如果我应该绑定到ViewModels,我是否还需要单独的ViewModels列表?

You might need to take a different perspective on MVVM. 您可能需要对MVVM采取不同的看法。 Your View is the page with the controls (XAML) and your ViewModel is the glue between your data model and the page. View是带有控件(XAML)的页面,而ViewModel是数据模型和页面之间的粘合剂。 The View's entire data context will be set to the ViewModel (done either in the XAML directly or in code-behind depending on which MVVM camp you subscribe to). View的整个数据上下文将设置为ViewModel(直接在XAML中或在代码隐藏中完成,具体取决于您所预订的MVVM阵营)。

In your example, you would move List<Product> onto the ViewModel as ObservableCollection<Product> and make sure that your ViewModel implements the INotifyPropertyChanged interface. 在您的示例中,您将List<Product>作为ObservableCollection<Product> List<Product>移到ViewModel上,并确保ViewModel实现了INotifyPropertyChanged接口。 INotifyPropertyChanged is the contract the View uses to know when to update it's binding. INotifyPropertyChanged是View用于了解何时更新其绑定的合同。 You will use an ObservableCollection<T> instead of a list because ObservableCollection<T> implements INotifyPropertyChanged itself. 您将使用ObservableCollection<T>而不是列表,因为ObservableCollection<T>本身实现了INotifyPropertyChanged。

Your View's DataContext property will be set to an instance of the ViewModel. 您的View的DataContext属性将设置为ViewModel的实例。 On the View, the ListBox control's ItemsSource property will be set to bind to the Product collection. 在视图上,ListBox控件的ItemsSource属性将设置为绑定到Product集合。 You can then have methods inside of your ViewModel that will be responsible for communicating with your data store to populate the observable collection. 然后,您可以在ViewModel内部使用方法,这些方法将负责与数据存储进行通信以填充可观察的集合。

ViewModel 视图模型

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<Product> _products = null;
    public ObservableCollection<Product> Products
    {
       get { return _products; }
       set
       {
           if( _products != value )
           {
               _products = value;

               if( this.PropertyChanged != null )
               {
                   this.PropertyChanged( this, new PropertyChangedEventArgs( "Products" ) );
                }
            }
        }
     }

     // have code in here that loads the Products list from your data store (i.e. service call to database)
}

View Code-Behind 查看隐藏代码

public MyView()
{   
    InitializeComponent(); 
    this.DataContext = new MyViewModel();
}

View 视图

<ListBox
    ItemsSource={Binding Path=Products, Mode=OneWay}
    SelectedItem={Binding Path=SelectedProduct, Mode=TwoWay}
    ...
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM