WPF MVVM中的状态管理 [英] State Management in WPF MVVM

查看:280
本文介绍了WPF MVVM中的状态管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以通过我的WPF MVVM应用程序中的视图模型告诉我如何维护集合对象。





谢谢

Kishore

Hi All,

Can any one tell me how to maintain a collection object through out the view model in my WPF MVVM application.


Thanks
Kishore

推荐答案

我会正常在这里使用MEF组成一个类的单个实例 - 这是一个非常复杂的区域,可以在一个答案中给出答案,但基本上你会创建一个包含管理LDAP条目的逻辑的模型。它可能看起来像这样(标有......的部分供您填写):
I''d normally use MEF to compose a single instance of a class in here - this is quite a complex area to give an answer to in a single answer, but basically you''d create a Model that contained your logic for managing the LDAP entries. It might look like this (the parts marked with ... are for you to fill in):
[Export(RequiredCreationPolicy = CreationPolicy.NonShared)]
public class LdapResources : ILdapResources
{
  public void Fill() { ... }
  public void Delete(.......) { ..... }
  // Other operations.
}

public interface ILdapResources
{
  void Fill();
  void Delete(....);
  // Other operations 
}

然后,在需要它的ViewModel中,您只需使用以下内容:

Then, in the ViewModels that need it, you''d simply use something like:

[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
public ILdapResources LDAP
{
  get; set; 
}

....
ILdapResource resource = LDAP;
resource.Fill();

你去了 - 在需要它的整个ViewModel中可以使用相同的实例。请注意,CreationPolicy.NonShared是默认值,因此您不需要指定它。看看这里 [ ^ ]了解更多信息。

There you go - the same instance is available throughout the ViewModels that need it. Note that CreationPolicy.NonShared is the default, so you don''t really need to specify it. Have a look here[^] for more information.


我所做的是在同一个模型中使用相同的模型MVVM。 ViewModel可以通过在模型中注册的事件来观察更改。
What I do is I use the same Model in the MVVM. The ViewModels can observe the changes through an event registered with the Model.


这篇关于WPF MVVM中的状态管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆