【问题标题】:How can I connect the ModelView to the Model in this Silverlight MVVM example?在此 Silverlight MVVM 示例中,如何将 ModelView 连接到模型?
【发布时间】:2009-03-31 08:48:54
【问题描述】:

我正在使用文章 Model View View-Model (MVVM) in Silverlight 作为基础,在 Silverlight 中创建我自己的 MVVM 示例。

我已经得到了以下所有部分:

  • 主页(加载所有内容)
  • 查看(带有绑定的 XAML 文件)
  • Model(生成虚假 List 集合的 Customer 类)
  • ModelView(继承 INotifyPropertyChanged 并拥有 View 需要的两个字段的 PropertyChanged)

在我的主页上:

  • 创建 ViewModel
  • ViewModel 绑定到 ViewDataContext
  • 创建模型(客户)

但是现在如何将 ModelView 连接到 Model? 我觉得我需要以某种方式将客户模型注入到 CustomerViewModel 中,对吗?但具体如何?完成这个 MVVM 示例的下一步是什么,以便我可以开始使用 MVVM 模式的优点,例如用测试模型换出模型,用新视图换出视图等。


MainPage.xaml.cs: 创建 ViewModel,将 View 附加到 ViewModel

using System.Windows.Controls;
using System.Collections.Generic;

namespace TestMvvm345
{
    public partial class MainPage : UserControl
    {
        private CustomerViewModel customerData;

        public MainPage()
        {
            InitializeComponent();

            customerData = new CustomerViewModel();
            customerHeaderView.DataContext = customerData;
            List<Customer> customers = Customer.GetCustomers();
        }
    }
}

MainPage.xaml: 在主页的上下文中显示视图

<UserControl x:Class="TestMvvm345.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestMvvm345"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel HorizontalAlignment="Left">
            <local:CustomerHeaderView x:Name="customerHeaderView" Margin="10"/>
        </StackPanel>
    </Grid>
</UserControl>

CustomerViewModel.xaml: ViewModel

using System.ComponentModel;

namespace TestMvvm345
{
    public class CustomerViewModel : INotifyPropertyChanged
    {
        private string firstName;
        private string lastName;

        public string FirstName
        {
            get { return firstName; }
            set
            {
                firstName = value;
                RaisePropertyChanged("FirstName");
            }
        }

        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                RaisePropertyChanged("LastName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

CustomerHeaderView.xaml 视图

<UserControl x:Class="TestMvvm345.CustomerHeaderView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel HorizontalAlignment="Left">
            <ListBox x:Name="CustomerList" ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock
                            Text="{FirstName}"/>
                            <TextBlock
                            Text="{LastName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</UserControl>

Customers.cs 模型

using System;
using System.Collections.Generic;

namespace TestMvvm345
{
    public class Customer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int NumberOfContracts { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
            customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 });
            customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 });
            customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 });
            customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 });
            return customers;
        }
    }
}

【问题讨论】:

    标签: silverlight mvvm


    【解决方案1】:

    我使用 ViewModel 的方式有些不同。在我的例子中,它实际上包装了 Model 类,将大部分数据传递给模型。这样,模型中的所有标准业务规则仍然按照它们应该立即的方式工作。 ViewModel 仅公开数据绑定或其他 UI 目的实际需要的那些属性。此外,ViewModel 可能包含用于数据绑定的其他属性/方法。

    因此,以您的客户为例: 命名空间 TestMvvm345 { 公共类 CustomerViewModel : INotifyPropertyChanged {

        private Customer _model;
        public Customer Model
        {
            get
            { return _model; }
            set
            {
                if (_model != null)
                    Model.PropertyChanged -= Model_PropertyChanged;
    
                _model = value;
    
                if (_model != null)
                    Model.PropertyChanged += Model_PropertyChanged;
            }
        }
    
        void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            RaisePropertyChanged(e.PropertyName);
            if (e.PropertyName == "FirstName" || e.PropertyName == "LastName")
                RaisePropertyChanged("FullName");
        }
    
        public string FirstName
        {
            get { return Model.FirstName; }
            set { Model.FirstName = value; }
        }
    
        public string LastName
        {
            get { return Model.LastName; }
            set { Model.LastName = value; }
        }
    
        public string FullName
        {
            get { return FirstName + " " + LastName; }
        }
    
    
        public static IList<CustomerViewModel> GetCustomers()
        {
            var result = new List<CustomerViewModel>();
            var customers = Customer.GetCustomers();
            foreach (var customer in customers)
            {
                result.Add(new CustomerViewModel() { Model = customer });
            }
            return result;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
    
    
    public class Customer : INotifyPropertyChanged
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int NumberOfContracts { get; set; }
    
        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
            customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 });
            customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 });
            customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 });
            customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 });
            return customers;
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    }
    

    }

    现在在您的 UI 中,您可以使用以下代码进行绑定: customerHeaderView.DataContext = CustomerViewModel.GetCustomers();

    ViewModel 中的代码确实变得有些大,但很多都可以放在基类中,而且模型中的所有属性都完全相同(想想 T4 模板)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2011-08-02
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多