gpt4 book ai didi

c# - MVVM 模式实现

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:25 24 4
gpt4 key购买 nike

我想在 WP 应用程序中使用 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));
}
}
}

ViewModel 初始化模型的字段。

View 模型

 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();

以下是我想澄清的时刻:

  1. ViewModel 如何通知 View 加载数据错误、加载结束?
  2. 我可以将部分日期传递给 View(没有数据绑定(bind),从技术上讲这是可能的,我的意思是,这在模式下是允许的)?

例如,在 ViewModel 中:

   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);
}
});
}

View 中:

_viewModel.LoadData((result, error) =>
{
if (error != null)
{
//error data loading
}
else
{
//successfully loading
}
});

这太糟糕了,可能这种方法破坏了整个概念。但是,例如,我使用 Jeff Wilcox 静态 map 。

           <jwMaps:StaticMap                                
Provider="Bing"
Visibility="Visible">
<jwMaps:StaticMap.MapCenter>
<geo:GeoCoordinate
Latitude ="50"
Longitude="50" />
</jwMaps:StaticMap.MapCenter>
</jwMaps:StaticMap>

我无法将坐标绑定(bind)到此控件。我试过了,不行。如果使用

 StaticMap.MapCenter =
new GeoCoordinate() { Latitude = user.Latitude, Longitude = user.Longitude };

然后工作。

如果是委托(delegate),我可以在成功的分支中完成...

请大家帮忙指点。

最佳答案

您可以将消息从您的 View 模型发送到您的 View ,因为您可以使用mvvm light 的Messenger 类

而且我不会破坏MVVM 模式 因为MVVM 模式是您必须在ViewModel 中完成您的逻辑部分,但它确实并不意味着您不能使用您的 Page.xaml.cs 代码隐藏。您可以在您的代码隐藏中对可见性或任何 UI 相关代码做出决定。此外,它实际上不是硬编码模式以更好和更简单的方式组织您的项目,但是如果您必须在 view 和您的 viewModel 之间进行通信,而您无法从 viewmodel 中解决您可以使用的某些原因你隐藏代码。我不是专家,但这就是我的想法..希望对这个主题有一些更新..

关于c# - MVVM 模式实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com