Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing State handling in windows 8 using MVVM Light

How i can implement state handling (running / resume / Terminate states ) using mvvm light. The major issue i a facing is with Navigation. I am totally uncontrolled with the navigation stack. How i can effectively manage this with MVVM Light.

like image 493
StezPet Avatar asked Jul 29 '12 16:07

StezPet


2 Answers

Start a new project using either the GridView or SplitView templates and take a look at SuspensionManager.cs in the Common folder. It has a method called RegisterFrame which, when called, starts tracking all of the navigation events from the frame and attempts to save off and restore state when the application suspends and resumes.

The applications main frame is registered with the SuspensionManager in App.OnLaunched (App.xaml.cs) and Saving is done in App.OnSuspending.

Finally, take a look at LayoutAwarePage.cs, also in the Common folder. You can inherit from LayoutAwarePage to get Portrait and Snapped design time support. It also attempts to handle navigation state caching by leveraging the SuspensionManager. So, no matter whether your page is navigated to from another page or as part of a resume, the virtual method LoadState is called with the correct data.

Obviously this pattern is managing navigation state directly in the page itself, but you could tweak this pattern to create a sort of "SuspensionService" that your ViewModels could leverage in the same way.

like image 91
Jared Bienz - MSFT Avatar answered Dec 31 '22 19:12

Jared Bienz - MSFT


You might be interested in the open source Okra App Framework that is freely available on CodePlex and NuGet (disclaimer: I am the lead developer on this project). This has been designed from the ground up for Windows 8 applications, in particular those that use the MVVM pattern (and you can still use the MVVM Light base classes to define your view-models).

Of particular interest it includes,

  • A navigation manager that understands the Windows 8 navigation model
  • A navigation stack that can automatically persist its state upon application termination
  • A mechanism for view-models to persist their own state (via a simple interface)
  • Automatic view and view-model construction and wiring up (by default using MEF attributes)
like image 34
Andy Wilkinson Avatar answered Dec 31 '22 20:12

Andy Wilkinson