ViewModels

With in the framework the only requirement for a class to be a ViewModel is for the class to be exported under a name. If you inherit from any of the base ViewModel classes the class will automatically be exported by name. Below is an example of how to define a new ViewModel that does not inherit from a base class.

    using StyleMVVM.DependencyInjection;

    [Export]
    public class MainPageViewModel
    {

    }

Resolution

ViewModel resolution happens using the dependency property View:ViewModel.Name. At View construction time the dependency property will be set which in turn triggers the ViewModel to be resolved and bound to the View's DataContext. Below is an example of binding the above ViewModel to a View.

    <View:LayoutAwarePage 
        x:Class="ExampleApp.Views.MainPageView"
        View:ViewModel.Name="MainPageViewModel"

Base ViewModels

StyleMVVM provides multiple base ViewModel classes for you to inherit from.
  • BaseViewModel - This is the base ViewModel class, its main functions are to Export the ViewModel under its default name and Import the IDependencyInjectionContainer that created it.
  • PageViewModel - provides a base class for a Page View. It includes methods and properties to help aid with Page Navigation within a Frame control.
  • RegionAwareViewModel - provides a base class for ViewModels used in a region navigation scheme. It includes properties and methods to help aid navigating a region.
  • PopupViewModel - inherit from this class when the ViewModel is used on a View that is opened as a Popup. It includes navigation methods as well as an IsOpen property.

Last edited Nov 3, 2012 at 3:26 PM by ipjohnson123, version 4

Comments

No comments yet.