在这个问题上得到了慷慨的帮助,我创建了以下MVVM结构,它可以实时显示模型中的更改(当前日期/时间),非常好。
这种设置的一个很酷的优点是,当您在Visual Studio或Blend的设计模式下查看视图时,您将看到时间正在滴答作响,这意味着在设计时您可以访问来自模型的实时数据。
在使其正常工作的过程中,我惊讶地发现大部分代码从ViewModel移动到了Model中,包括实现INotifyPropertyChange。 另一个变化是,我不再绑定到ViewModel上的属性,而是绑定到方法。
因此,当前这是我最喜欢的MVVM结构:
View是愚蠢的:
- 每个对象所需的ObjectDataProvider都有一个
- 每个ObjectDataProvider映射到ViewModel上的一个方法(而不是属性)
- XAML元素中没有x:Name属性
ViewModel瘦身:
- ViewModel中唯一的内容是视图绑定到的方法
Model臃肿:
- 模型在其每个属性上实现INotifyPropertyChanged。
- 对于ViewModel中的每个方法(例如GetCurrentCustomer),模型都有一个相应的单例方法(例如GetCurrentCustomer)。
- 模型处理此示例中的任何实时线程功能。
问题:
- 那些在实际情况下实施MVVM的人,您是否也采用了这种基本结构?如果不是,您的结构有何变化?
- 如何扩展此结构以包括路由命令和路由事件?
如果您只复制XAML和代码到新的WPF项目中,以下代码将正常工作。
XAML:
<Window x:Class="TestBinding99382.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBinding99382"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider
x:Key="DataSourceCustomer"
ObjectType="{x:Type local:ShowCustomerViewModel}"
MethodName="GetCurrentCustomer"/>
</Window.Resources>
<DockPanel DataContext="{StaticResource DataSourceCustomer}">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Path=LastName}"/>
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=TimeOfMostRecentActivity}"/>
</StackPanel>
</DockPanel>
</Window>
后台代码:
using System.Windows;
using System.ComponentModel;
using System;
using System.Threading;
namespace TestBinding99382
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
//view model
public class ShowCustomerViewModel
{
public Customer GetCurrentCustomer() {
return Customer.GetCurrentCustomer();
}
}
//model
public class Customer : INotifyPropertyChanged
{
private string _firstName;
private string _lastName;
private DateTime _timeOfMostRecentActivity;
private static Customer _currentCustomer;
private Timer _timer;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
this.RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
this.RaisePropertyChanged("LastName");
}
}
public DateTime TimeOfMostRecentActivity
{
get
{
return _timeOfMostRecentActivity;
}
set
{
_timeOfMostRecentActivity = value;
this.RaisePropertyChanged("TimeOfMostRecentActivity");
}
}
public Customer()
{
_timer = new Timer(UpdateDateTime, null, 0, 1000);
}
private void UpdateDateTime(object state)
{
TimeOfMostRecentActivity = DateTime.Now;
}
public static Customer GetCurrentCustomer()
{
if (_currentCustomer == null)
{
_currentCustomer = new Customer
{ FirstName = "Jim"
, LastName = "Smith"
, TimeOfMostRecentActivity = DateTime.Now
};
}
return _currentCustomer;
}
//INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}