WinRT MVVM Framework
WinRT is a great platform for touch based applications. As XAML is the go-to description language for User Interfaces in C# and C++ applications, DataBinding is supported as an elegant means to connect your WinRT app’s UI to backing data.
DataBinding was one of the key features of WPF which led to the birth of the Model View ViewModel (MVVM) pattern. The MVVM architecture is a great way to decouple your business logic and application data from the User Interface.
I am a big fan of this pattern and want to leverage it in my C# WinRT applications. Unfortunately, the way the WinRT API is designed rather encourages a “code-behind” approach which forces you to tightly couple your Views and ViewModels.
As a consequence, the developed MVVM framework fills the WinRT API’s Gaps and allows you to interact with Windows 8’s new features solely from within your ViewModels.
In particular, the framework offers the following feature set:
- ViewModel-based Page Navigation: By default, the WinRT API only allows you to navigate to a page by passing the Page-Type to the API. Using the framework, you can navigate to other pages from within a Page-ViewModel by simply
specifying the ViewModel Type. This ensures that your ViewModels stay decoupled from the Views.
- Transient State Handling: With its extended application lifecycle, WinRT applications throw a monkey wrench into the way Desktop developers are used to maintain their application states. With applications getting suspended and
eventually terminated by the Operating System due to memory shortages, the developer now is responsible for recovering the previous state of the application. The basic framework that Microsoft provides you with when
you develop a C# WinRT application, comes with the base class “LayoutAwarePage”. If you derive your Pages from this class, you can persist and recover your Pages’ transient states in
code behind. Our framework allows your ViewModels to seamlessly manage their transient states - without having to deal with the Pages.
- Share Contract: One of Win8’s greatest new features, Sharing, allows you to share arbitrary data with any other installed application that can receive data. The coolest part about this feature is, that the application,
which decides to share data, doesn’t have to know anything about the receiving applications. Windows 8 bridges the gap between the Share Source and Share Target. Our framework allows your application to incorporate
this feature solely from within your ViewModels. This means: You can share data from within the business logic of your ViewModel, and you can also declare your ViewModel as being capable of receiving shared data.
- Settings Contract: Win8 standardizes the way applications are supposed to expose settings to the user, by providing them with an API to inject their own settings into WinRT’s Settings Charme. This
is, however, all the API does for you. It is still the developer’s responsibility to create the Settings Dialog’s UI and present it to the user – of course following Microsoft Design Guidelines. The
framework takes this boilerplate burden off your shoulders… all from within your ViewModels!
- Search Contract: Win8 now also allows the Operating System’s user to not only search through the computer’s hard drive, but even through every installed application that supports the Search Contract. The
framework allows you to easily leverage this great feature.
- Component Library: The framework not only bridges the gap to the mentioned Win8 features, but also provides you with common classes that you might find useful when you develop WinRT applications. DelegateCommand and Bindable
(a ViewModel Base Class) are probably the most famous ones. The framework also integrates Microsoft’s most useful base and helper classes (such as the mentioned LayoutAwarePage).