C# MVVM模式实现
我想在WP应用程序中使用C# MVVM模式实现,c#,windows-phone-7,data-binding,mvvm,windows-phone-8,C#,Windows Phone 7,Data Binding,Mvvm,Windows Phone 8,我想在WP应用程序中使用MVVM模式。我对这种模式有一些想法。但有些事情我不明白。我不知道这样做是否是好的做法 所以,我有模型。 模型是一种数据结构。字段和属性集 型号 public class Person : INotifyPropertyChanged { private string name; private GeoCoordinate coordinate; public string Name { get {
MVVM模式
。我对这种模式有一些想法。但有些事情我不明白。我不知道这样做是否是好的做法
所以,我有模型
。
模型是一种数据结构。字段和属性集
型号
public class Person : INotifyPropertyChanged
{
private string name;
private GeoCoordinate coordinate;
public string Name
{
get
{
return name;
}
set
{
if (this.name != value)
{
this.name = value;
this.RaisePropertyChanged("Name");
}
}
}
public GeoCoordinate Coordinate
{
get
{
return this.coordinate;
}
set
{
if (this.coordinate != value)
{
this.coordinate = value;
this.RaisePropertyChanged("Coordinate");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class PersonViewModel : INotifyPropertyChanged
{
public Person User
{
get;
private set;
}
public PersonViewModel()
{
this.User = new Person();
}
public LoadData()
{
Service.GetUser((result) =>
{
this.User.Name = result.Name;
this.User.Coordinate = result.Coordinate;
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ViewModel初始化模型的字段
视图模型
public class Person : INotifyPropertyChanged
{
private string name;
private GeoCoordinate coordinate;
public string Name
{
get
{
return name;
}
set
{
if (this.name != value)
{
this.name = value;
this.RaisePropertyChanged("Name");
}
}
}
public GeoCoordinate Coordinate
{
get
{
return this.coordinate;
}
set
{
if (this.coordinate != value)
{
this.coordinate = value;
this.RaisePropertyChanged("Coordinate");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class PersonViewModel : INotifyPropertyChanged
{
public Person User
{
get;
private set;
}
public PersonViewModel()
{
this.User = new Person();
}
public LoadData()
{
Service.GetUser((result) =>
{
this.User.Name = result.Name;
this.User.Coordinate = result.Coordinate;
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
查看
PersonViewModel _viewModel;
this.DataContext = _viewModel;
_viewModel.LoadData();
以下是我想澄清的时刻:
public LoadData(Action<Person, Exception> act)
{
Service.GetUser((result, error) =>
{
if (error != null)
{
act.Invoke(null, error);
}
else
{
this.User.Name = result.Name;
this.User.Coordinate = result.Coordinate;
act.Invoke(result, null);
}
});
}
_viewModel.LoadData((result, error) =>
{
if (error != null)
{
//error data loading
}
else
{
//successfully loading
}
});
这太可怕了,可能这种方法破坏了整个概念。但是,例如,我与杰夫·威尔科克斯(Jeff Wilcox)一起工作
<jwMaps:StaticMap
Provider="Bing"
Visibility="Visible">
<jwMaps:StaticMap.MapCenter>
<geo:GeoCoordinate
Latitude ="50"
Longitude="50" />
</jwMaps:StaticMap.MapCenter>
</jwMaps:StaticMap>
然后工作
如果是代理,我可以在一个成功的分支中完成它
请提供帮助建议。您可以将消息从您的视图模型发送到您的视图,您可以使用mvvm light的Messenger类 我不会打破MVVM模式,因为MVVM模式是你必须在ViewModel中完成你的逻辑部分,但这并不意味着你不能在背后使用Page.xaml.cs代码。你可以在代码背后对可见性或任何UI相关代码做出决定。这也是实际上,它不是一种硬编码模式,用于以更好、更简单的方式组织您的项目,但如果您必须在视图和您的视图模型之间进行通信,原因是您无法从视图模型中解决,而无法使用代码隐藏。
我不是专家,但这是我的想法。希望在这个主题上有一些更新。你可能想从一个MVVM框架开始,否则你会编写大量的管道代码来让这些东西正常工作-如果没有太多的模式经验,你很可能会犯其他人已经解决的错误。我不是说你不应该尝试去理解它,但我是说它已经被理解了,并且有很多不同的方法来实现这个模式。我最喜欢的MVVM框架之一是Caliburn Micro,它适用于WP7(可能也适用于WP8,尽管我还没有检查最新版本)。尝试使用MVVM Light、Prism和Caliburn Micro之类的控件解决绑定问题—听起来您提到的控件不支持绑定。这是控件的一个更大的问题,但您可以通过使用事件聚合器(或中介模式的实现)来解决它——基本上,您可以在视图中订阅特定类型的消息,并从视图模型发送该类型的消息。你的调停人坐在中间,接收消息,把它们委托给订户。通过这种方式,您可以替换消息链的任意一端,因为它们互不依赖。谢谢!我检查了Caliburn Micro,打开了一些示例,到目前为止,它对我来说是神奇的。:)名称空间中有这么多类,需要研究。我在示例事件聚合器中看到过,我认为理解这个想法,我喜欢这种方法。