Windows Phone 8 - MVVM ViewModels 和 App.xaml.cs [英] Windows Phone 8 - MVVM ViewModels and App.xaml.cs

查看:57
本文介绍了Windows Phone 8 - MVVM ViewModels 和 App.xaml.cs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 MVVM 模式并将其在 Windows Phone 8 应用中付诸实践,我有一个关于在应用中初始化和访问 ViewModel 的最佳实践的问题.

当我从 WP8 SDK 模板创建数据绑定应用程序时,我注意到了 App.xaml.cs 文件中的这段代码:

public static MainViewModel ViewModel{得到{//延迟创建视图模型,直到有必要如果(视图模型 == 空)viewModel = new MainViewModel();返回视图模型;}}私有无效Application_Activated(对象发送者,ActivatedEventArgs e){//确保应用程序状态适当恢复如果 (!App.ViewModel.IsDataLoaded){App.ViewModel.LoadData();}}

据我所知,这意味着 App 类包含 MainViewModel 作为静态成员,当应用程序被激活时,ViewModel 会被加载.>

既然如此,我有以下问题:

  1. 如果我的应用有多个 ViewModel,它们是否都作为成员存储在 App.xaml.cs 文件中?

  2. 如果每个 ViewModel 的数据同时加载,我该如何管理我的应用程序的内存?是否可以卸载每个 ViewModel 的数据而只加载我的 View 正在使用的 ViewModel?

解决方案

有许多不同的方法来实例化 ViewModel.其中一些会在启动时实例化,而另一些不会在需要时实例化 ViewModel.

在以下博客文章中,您将找到一些实例化 ViewModel 的可能方法:

MVVM 实例化方法

回答您的问题;1.- 按照您的方法,您必须在 App.xaml.cs 文件中为所有 ViewModel 定义成员.2.- 您可以遵循一种在需要时才实例化 ViewModel 的方法.

存在一些工具包,例如 MVVM LightCaliburn Micro,简化了 MVVM 模式的实现.我个人使用 MVVM Light Toolkit,它使用定位器方法.使用此工具包,默认情况下会在需要时加载 ViewModel,但您可以将其设置为在启动时加载特定的 ViewModel,这在某些情况下会很有用.

I've been studying the MVVM pattern and putting it into practice in a Windows Phone 8 app, and I have a question about the best practices for initializing and accessing ViewModels in an app.

When I create a Databound Application from the WP8 SDKs templates, I noticed this code in the App.xaml.cs file:

public static MainViewModel ViewModel
{
    get
    {
        // Delay creation of the view model until necessary
        if (viewModel == null)
            viewModel = new MainViewModel();

            return viewModel;
    }
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    // Ensure that application state is restored appropriately
    if (!App.ViewModel.IsDataLoaded)
    {
        App.ViewModel.LoadData();
    }
}

From what I understand, that means that the App class contains the MainViewModel as a static member, and when the application is activated, the ViewModel is loaded.

That being the case, I have the following questions:

  1. If my App has several ViewModels, would all of them be stored as members inside the App.xaml.cs file?

  2. If every ViewModel's data is loaded at the same time, how do I manage my app's memory? Is it possible to unload each ViewModel's data and only load the ViewModel that is being used by my View?

解决方案

There are many different approaches to instantiate ViewModels. Some of them will instantiate all at launch, while other don't instantiate the ViewModel until it is needed.

In the following blog post you will find some possible approaches to instantiate a ViewModel:

MVVM Instantiation Approaches

Answering your questions; 1.- Following your approach you would have to define members for all of your ViewModels in your App.xaml.cs file. 2.- You can follow an approach that doesn't instantiate the ViewModel until it is needed.

There exist some toolkits, such MVVM Light or Caliburn Micro, that ease the implementation of MVVM pattern. I personally use MVVM Light Toolkit, which uses the Locator approach. Using this toolkit, ViewModels are loaded when needed by default, but you can set it to load a specific ViewModel at launch, which can be useful in some scenarios.

这篇关于Windows Phone 8 - MVVM ViewModels 和 App.xaml.cs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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