简单的 MVVM 工具包 - 切换 ViewModel 时丢失 ViewModel

发布于 2024-12-08 10:04:03 字数 1312 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-12-15 10:04:03

我建议您使用任何类型的 IoC 容器(例如 Unity),

public class ViewModelLocator
{
    public static UnityContainer Contaner { get; private set;}

    static ViewModelLocator()
    {
        Container = new UnityContainer();

        Container.RegisterType<ProductListViewModel>(new ContainerControlledLifetimeManager());
    }

    public ProductListViewModel ProductViewModel
    {
        get
        {
            return Container.Resolve<ProductListViewModel>();
        }
    }
}

我认为在 MVVM Light Toolkit 中您可以使用 SimpleIoc - IoC 容器的轻量级实现。

I will recommend you to use any type of IoC container for that (for example Unity)

public class ViewModelLocator
{
    public static UnityContainer Contaner { get; private set;}

    static ViewModelLocator()
    {
        Container = new UnityContainer();

        Container.RegisterType<ProductListViewModel>(new ContainerControlledLifetimeManager());
    }

    public ProductListViewModel ProductViewModel
    {
        get
        {
            return Container.Resolve<ProductListViewModel>();
        }
    }
}

I think in MVVM Light Toolkit you have SimpleIoc - lightweight implementation of IoC container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文