简单的 MVVM 工具包 - 切换 ViewModel 时丢失 ViewModel
我正在使用 MVVM 工具包。 在我的 ViewModel 中,我保留了将 ViewModel 切换到另一个视图时要保存的数据。
负责切换 ViewModel 的是 ViewModelLocator:
http://simplemvvmtoolkit.codeplex.com/wikipage?title= Getting%20Started 第 8 点。ViewModelLocator
每次都会返回新的 ViewModel:
public class ViewModelLocator
{
// Create ProductListViewModel on demand
public ProductListViewModel ProductListViewModel
{
get
{
IProductServiceAgent serviceAgent = new MockProductServiceAgent();
return new ProductListViewModel(serviceAgent);
}
}
}
我不想破坏 MVVM 规则。我正在考虑创建这样的新对象:
public class ViewModelLocator
{
private ProductListViewModel productListViewModel;
// Create ProductListViewModel on demand
public ProductListViewModel ProductListViewModel
{
get
{
IProductServiceAgent serviceAgent = new MockProductServiceAgent();
if (productListViewModel == null)
productListViewModel = new ProductListViewModel(serviceAgent);
return productListViewModel;
}
}
}
...或者在切换 ViewModel 时序列化 ViewModel,在加载回来时 - 反序列化...
这个问题的正确解决方案是什么?
I'm using MVVM Toolkit.
In my ViewModels I'm keeping data which I'd like to save when switching ViewModel to another.
Responsible for switching ViewModels is ViewModelLocator:
http://simplemvvmtoolkit.codeplex.com/wikipage?title=Getting%20Started point 8.
ViewModelLocator everytime returns new ViewModel:
public class ViewModelLocator
{
// Create ProductListViewModel on demand
public ProductListViewModel ProductListViewModel
{
get
{
IProductServiceAgent serviceAgent = new MockProductServiceAgent();
return new ProductListViewModel(serviceAgent);
}
}
}
I don't want to break MVVM rules. I was thinking about creating new objects like this:
public class ViewModelLocator
{
private ProductListViewModel productListViewModel;
// Create ProductListViewModel on demand
public ProductListViewModel ProductListViewModel
{
get
{
IProductServiceAgent serviceAgent = new MockProductServiceAgent();
if (productListViewModel == null)
productListViewModel = new ProductListViewModel(serviceAgent);
return productListViewModel;
}
}
}
... or while switching ViewModel serialize ViewModel, when loading it back - deserialize...
What is the proper solution of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用任何类型的 IoC 容器(例如 Unity),
我认为在 MVVM Light Toolkit 中您可以使用 SimpleIoc - IoC 容器的轻量级实现。
I will recommend you to use any type of IoC container for that (for example Unity)
I think in MVVM Light Toolkit you have SimpleIoc - lightweight implementation of IoC container.