Let me begin with the advantages of using Prism framework.

Create new Universal Windows project, I have named it as MyPrismApp.



Install the below packages using Package Manager Console.



App.xaml.cs

Remove the class "Application" which was inherited by the class "App" and replace with the class "PrismUnityApplication".

(For those who used prism library in Universal windows 8.1- we are not inheriting with MvvmAppBase any more).

Remove OnLaunched method and add the below code.

  1. protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
  2. {
  3. NavigationService.Navigate("Main", null);
  4. Window.Current.Activate();
  5. return Task.FromResult<object>(null);
  6. }
  7. protected override Task OnInitializeAsync(IActivatedEventArgs args)
  8. {
  9. RegisterTypes();
  10. return base.OnInitializeAsync(args);
  11. }
  12. private void RegisterTypes()
  13. {
  14. ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
  15. {
  16. var viewModelTypeName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "MyPrismApp.ViewModels.{0}ViewModel, MyPrismApp", viewType.Name);
  17. var viewModelType = Type.GetType(viewModelTypeName);
  18. return viewModelType;
  19. });
  20. }

The ViewModelLocationProvider code tells Prism where to look for ViewModels file with naming convention.

Your App.xaml.cs page looks like below,

  1. using Prism.Unity.Windows;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Prism.Mvvm;
  8. using System.Threading.Tasks;
  9. using Windows.ApplicationModel;
  10. using Windows.ApplicationModel.Activation;
  11. using Windows.Foundation;
  12. using Windows.Foundation.Collections;
  13. using Windows.UI.Xaml;
  14. using Windows.UI.Xaml.Controls;
  15. using Windows.UI.Xaml.Controls.Primitives;
  16. using Windows.UI.Xaml.Data;
  17. using Windows.UI.Xaml.Input;
  18. using Windows.UI.Xaml.Media;
  19. using Windows.UI.Xaml.Navigation;
  20. namespace MyPrismApp
  21. {
  22. /// <summary>
  23. /// Provides application-specific behavior to supplement the default Application class.
  24. /// </summary>
  25. sealed partial class App : PrismUnityApplication
  26. {
  27. /// <summary>
  28. /// Initializes the singleton application object. This is the first line of authored code
  29. /// executed, and as such is the logical equivalent of main() or WinMain().
  30. /// </summary>
  31. public App()
  32. {
  33. this.InitializeComponent();
  34. this.Suspending += OnSuspending;
  35. }
  36. protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
  37. {
  38. NavigationService.Navigate("Main", null);
  39. Window.Current.Activate();
  40. return Task.FromResult<object>(null);
  41. }
  42. protected override Task OnInitializeAsync(IActivatedEventArgs args)
  43. {
  44. RegisterTypes();
  45. return base.OnInitializeAsync(args);
  46. }
  47. private void RegisterTypes()
  48. {
  49. ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
  50. {
  51. var viewModelTypeName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "MyPrismApp.ViewModels.{0}ViewModel, MyPrismApp", viewType.Name);
  52. var viewModelType = Type.GetType(viewModelTypeName);
  53. return viewModelType;
  54. });
  55. }
  56. /// <summary>
  57. /// Invoked when Navigation to a certain page fails
  58. /// </summary>
  59. /// <param name="sender">The Frame which failed navigation</param>
  60. /// <param name="e">Details about the navigation failure</param>
  61. void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
  62. {
  63. throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
  64. }
  65. /// <summary>
  66. /// Invoked when application execution is being suspended. Application state is saved
  67. /// without knowing whether the application will be terminated or resumed with the contents
  68. /// of memory still intact.
  69. /// </summary>
  70. /// <param name="sender">The source of the suspend request.</param>
  71. /// <param name="e">Details about the suspend request.</param>
  72. private void OnSuspending(object sender, SuspendingEventArgs e)
  73. {
  74. var deferral = e.SuspendingOperation.GetDeferral();
  75. //TODO: Save application state and stop any background activity
  76. deferral.Complete();
  77. }
  78. }
  79. }

App.xaml

Add the namespace "xmlns:prism="using:Prism.Unity.Windows"" and replace Application with "prism:PrismUnityApplication".

  1. <prism:PrismUnityApplication
  2. x:Class="MyPrismApp.App"
  3. xmlns:prism="using:Prism.Unity.Windows"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:local="using:MyPrismApp"
  7. RequestedTheme="Light">
  8. </prism:PrismUnityApplication>
Now we are done with App page.

Next we need to delete MainPage and create three folders "Views", "Models", "ViewModels" and create a page under the Views folder and name it as MainPage.

MainPage.xaml

Add the namespace "xmlns:prism="using:Prism.Windows.Mvvm"" and replace "page" with "prism:SessionStateAwarePage" and add "prism:ViewModelLocator.AutoWireViewModel="True""

  1. <prism:SessionStateAwarePage
  2. x:Class="MyPrismApp.Views.MainPage"
  3. xmlns:prism="using:Prism.Windows.Mvvm"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:local="using:MyPrismApp.Views"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. prism:ViewModelLocator.AutoWireViewModel="True"
  10. mc:Ignorable="d">
  11. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  12. <StackPanel>
  13. <TextBlock Text="{Binding Title}" FontSize="{StaticResource TextStyleExtraLargeFontSize}" Margin="15"/>
  14. <ListView Margin="15" ItemsSource="{Binding Countries}">
  15. <ListView.ItemTemplate>
  16. <DataTemplate>
  17. <Grid>
  18. <Grid.ColumnDefinitions>
  19. <ColumnDefinition Width="100"/>
  20. <ColumnDefinition Width="*"/>
  21. </Grid.ColumnDefinitions>
  22. <TextBlock Text="{Binding CountryCode}"/>
  23. <TextBlock Text="{Binding CountryName}" Grid.Column="1"/>
  24. </Grid>
  25. </DataTemplate>
  26. </ListView.ItemTemplate>
  27. </ListView>
  28. </StackPanel>
  29. </Grid>
  30. </prism:SessionStateAwarePage>

MainPage.xaml.cs

Remove the class "Page" which was inherited by the class "MainPage" and replace with the class :SessionStateAwarePage".

(For those who used prism library in Universal windows 8.1- we are not inheriting with VisualStateAwarePage any more).

  1. using Prism.Windows.Mvvm;
  1. namespace MyPrismApp.Views
  2. {
  3. public sealed partial class MainPage : SessionStateAwarePage
  4. {
  5. public MainPage()
  6. {
  7. this.InitializeComponent();
  8. }
  9. }
  10. }

Model Class

Add a class file in Models folder and name it as Country.

  1. namespace MyPrismApp.Models
  2. {
  3. public class Country
  4. {
  5. private string countryCode;
  6. private string countryName;
  7. public string CountryCode
  8. {
  9. get { return countryCode; }
  10. set { countryCode = value; }
  11. }
  12. public string CountryName
  13. {
  14. get { return countryName; }
  15. set { countryName = value; }
  16. }
  17. }
  18. }
ViewModel Class

Add a class in ViewModels folder and name it as MainPageViewModel (Should use the same name of your view followed by ViewModel.)

Inherit your ViewModel class with ViewModelBase.
  1. using Prism.Windows.Mvvm;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Prism.Windows.Navigation;
  8. using System.Collections.ObjectModel;
  9. using MyPrismApp.Models;
  10. namespace MyPrismApp.ViewModels
  11. {
  12. public class MainPageViewModel : ViewModelBase
  13. {
  14. private string title = "Countries with code";
  15. public string Title
  16. {
  17. get { return title; }
  18. set { title = value; }
  19. }
  20. private ObservableCollection<Country> countries;
  21. public ObservableCollection<Country> Countries
  22. {
  23. get { return countries; }
  24. set { countries = value; }
  25. }
  26. public override void OnNavigatedTo(NavigatedToEventArgs e, Dictionary<string, object> viewModelState)
  27. {
  28. base.OnNavigatedTo(e, viewModelState);
  29. Countries = new ObservableCollection<Country>();
  30. Countries.Add(new Country() { CountryCode = "1", CountryName = "Canada" });
  31. Countries.Add(new Country() { CountryCode = "1", CountryName = "United States" });
  32. Countries.Add(new Country() { CountryCode = "44", CountryName = "United Kingdom" });
  33. Countries.Add(new Country() { CountryCode = "91", CountryName = "India" });
  34. }
  35. }
  36. }
Now we are good to go, hit run button.



Prism has one more important advantage called "Design Time", we can bind the data and display it in screen before running it, which really helps for design alignment and saves lot of time -- cool isn't it?

DesignTime

Create a folder "DesignTime" and add a class and name it as "MainPageDesignTimeViewModel" and paste the below code.
  1. using MyPrismApp.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MyPrismApp.DesignTime
  9. {
  10. public class MainPageDesignTimeViewModel
  11. {
  12. public MainPageDesignTimeViewModel()
  13. {
  14. Title = "Countries with code";
  15. Countries = new ObservableCollection<Country>();
  16. Countries.Add(new Country() { CountryCode = "1", CountryName = "Canada" });
  17. Countries.Add(new Country() { CountryCode = "1", CountryName = "United States" });
  18. Countries.Add(new Country() { CountryCode = "44", CountryName = "United Kingdom" });
  19. Countries.Add(new Country() { CountryCode = "91", CountryName = "India" });
  20. }
  21. private string title;
  22. public string Title
  23. {
  24. get { return title; }
  25. set { title = value; }
  26. }
  27. private ObservableCollection<Country> countries;
  28. public ObservableCollection<Country> Countries
  29. {
  30. get { return countries; }
  31. set { countries = value; }
  32. }
  33. }
  34. }
MainPage.xaml

Add the namespace "xmlns:designTime="using:MyPrismApp.DesignTime"" and set the data context of designtime viewmodel.
  1. <d:prism.DataContext>
  2. <designTime:MainPageDesignTimeViewModel/>
  3. </d:prism.DataContext>
Build the app once and see the design page where you can see the list of data binded to listview.



Source code ha been included, if you have any queries feel free to ask.