WinRT Framework: Getting Started

Windows Runtime (WinRT) is used to make Windows store applications. The problem is that the WinRT API is designed in a way that Views and ViewModels are tightly coupled. This means that all the business logic, events like button click, are in “code-behind” of the view instead of it being in the view model. This Framework allows us to enforce a solid MVVM pattern that puts all the business logic in the view model making it decoupled from the view.

Before we make use of the functionalities of the framework, we need to make sure that it is referenced correctly in our project.
To read an in-depth explanation about the framework, refer to this: Introduction of WinRT Framework

1) In Visual Studio, create a new Blank App project

2) Add a reference to the framework
Framework Reference

3) We need to change the base class of the app-class to DefaultApplication. By doing this, the DefaultApplication takes care of the initialization of the framework. This means that the framework will automatically be set up for us. To do this, we need to change <application> tag to <mvvm:DefaultApplication> and we also need to add a namespace: xmlns:mvvm=”using:WinRTFramework.DefaultImplementations”

<mvvm:DefaultApplication x:Class="DemoProject.App"

4) Since we changed the base class to DefaultApplication on the xaml, we need to do the same thing in the code behind (App.xaml.cs)
We need the App to inherit from DefaultApplication.

sealed partial class App : DefaultApplication

5) Now, we need to add a new class called MainPageVM.cs. This will become the view model of our MainPage View.


6) Then we implement methods of DefaultApplication:
6a) IsSearchSupported tells the Framework if we support search or not..
For this instance, we will not be doing search so we can just set the value to ‘false’. Down the road, if we want to make an application that supports searching, we can just change the value to return ‘true’

protected override bool IsSearchSupported
	        get { return false; }

6b) Next we need to provide the framework with an adapter to our IoC Container, which will allow the framework to create instances from our ViewModels Types. If you use your own IoC Container (such as Autofac or Prism), you should develop an IoCAdapter by implementing the IoCAdapter Interface. If, however, your project does not use an IoC Container, you can use the framework’s DefaultIoCAdapter, which is a very simplistic IoC Container implementation that uses types’ default constructors to create their instances.

protected override IIoCAdapter IoCAdapter
		  get { return new DefaultIoCAdapter(_viewToVMMap.Values); }

6c) The framework uses an instance of a IViewViewModelMapper to find out which View belongs to which ViewModel and vice versa. If you follow a fixed naming pattern in your project (such as the ViewModel for Page XYZPage is always called XYZViewModel), you would develop an IViewViewModelMapper which finds matching partners according to this naming rule. If you, however, don’t have such a naming rule, you can use the Framework’s DefaultViewViewModelMapper which accepts an instance of a Dictionary that simply maps the type of a View to the type of a ViewModel

Dictionary _viewToVMMap = new Dictionary();
 public App()
			_viewToVMMap.Add(typeof(MainPage), typeof(MainPageVM));

protected override IViewViewModelMapper ViewViewModelMapper
		    get { return new DefaultViewViewModelMapper(_viewToVMMap, IoCAdapter); }

6d) Lastly, we want to tell the framework what page we want to navigate to. We achieve this by specifying in the Navigate() method what ViewModel of the page that we want to navigate to.
This means that if we want to navigate to the MainPage View, we need to specify its view-model to the Navigate method like this — Navigate<MainPageVM>().

	    protected override bool NavigateToInitialPage(INavigationService navigationService)
		    return navigationService.Navigate();

Here is the link to the solution

Leave a Reply

Your email address will not be published. Required fields are marked *