我使用Silverlight 中的模型视图视图模型 (MVVM) 文章作为在 Silverlight中创建我自己的 MVVM 示例的基础。
我有下面的所有部分:
- 主页(加载所有内容)
- 查看(带有绑定的 XAML 文件)
- 模型(生成虚假列表集合的客户类)
- ModelView(继承 INotifyPropertyChanged 并具有 View 需要的两个字段的 PropertyChanged)
在我的主页中,我:
- 创建视图模型
- 将ViewModel绑定到View的DataContext
- 创建模型(客户)
但是现在我如何将 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;
}
}
}