Windows Phone 8 - MVVM ViewModels and App.xaml.cs
Asked Answered
B

1

7

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?

Beecher answered 24/9, 2013 at 8:15 Comment(0)
F
8

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.

Frear answered 24/9, 2013 at 8:42 Comment(3)
Great answer, thanks very much! And the references are great too :) Just wondering, if I were to go with this approach, what would be the best way to Unload data from my ViewModels if I didn't want them taking up too much memory when not in use?Beecher
I think one option would be to implement IDisposable in your ViewModel and Dispose it when leaving the View.Frear
expanding andersZubi's answer, I like to create a singleton that represents the current state of the app, and has a property of type Dictionary<string, IViewModel>. Every Page or control that needs a view model, looks in the dictionary first, then creates it if it doesnt yet exist, and adds the viewModel to the Dictionary. Then if it's IDisposable, you dont have to dispose of it when unloading the view... since you have a reference to it you can choose to dispose of it based on any arbitrary trigger (even after the view is unloaded), if you have the "key" for view model in the dictionary.Bhili

© 2022 - 2024 — McMap. All rights reserved.