Beginning Windows 8 Data Development: Using C# and JavaScript by Vinodh Kumar

Beginning Windows 8 Data Development: Using C# and JavaScript by Vinodh Kumar

Author:Vinodh Kumar [Kumar, Vinodh]
Language: eng
Format: epub, pdf
Tags: C#, Computers, Programming, Microsoft, Programming Languages
ISBN: 9781430249931
Google: jRXbAAAAQBAJ
Publisher: Apress
Published: 2013-09-10T00:00:00+00:00


ViewModel

The ViewModel folder consists of ViewModelLocator and two ViewModels: MainViewModel and BillViewModel. ViewModelLocator is a repository of ViewModels enabled by MVVM Light that locates the ViewModel from inside XAML and connects it to the View DataContent.

The App.xaml defines a global instance of the locator as shown in Listing 6-9, and individual views can bind their DataContent to properties of the locator that serve up the individual ViewModels.

Listing 6-9. Global Instance of the ViewModelLocator in App.xaml

<Application.Resources>

<vm:ViewModelLocator x:Key="Locator"

d:IsDataSource="True" />

</Application.Resources>

MVVM Light also made it simple to register Services and ViewModels by including a simple IOC container along with the framework, as shown in Listing 6-10.

Listing 6-10. SimpleIOC Container Registers DataService, NavigationService, MainViewModel, and BillViewModel

public class ViewModelLocator

{

static ViewModelLocator()

{

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

SimpleIoc.Default.Register<IDataService, DataService>();

SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());

SimpleIoc.Default.Register<MainViewModel>();

SimpleIoc.Default.Register<BillViewModel>();

}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",

"CA1822:MarkMembersAsStatic",

Justification = "This non-static member is needed for data binding purposes.")]

public MainViewModel Main

{

get

{

return ServiceLocator.Current.GetInstance<MainViewModel>();

}

}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",

"CA1822:MarkMembersAsStatic",

Justification = "This non-static member is needed for data binding purposes.")]

public BillViewModel Bill

{

get

{

return ServiceLocator.Current.GetInstance<BillViewModel>();

}

}

}

As shown in Listing 6-11, the ViewModels are exposed as properties of the ViewModelLocator and can be databound in XAML to the View DataContext.

Listing 6-11. MainViewModel Databound to the MainPage.xaml Datacontext

<Page x:Class="BillReminder.MainPage" xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "

xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml "

xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "

xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "

xmlns:ignore=" http://www.ignore.com "

mc:Ignorable="d ignore"

d:DesignHeight="768"

d:DesignWidth="1366"

DataContext="{Binding Main, Source={StaticResource Locator}}">



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.