PrismAdapter description

PrismAdapter is lightweight Prism for Windows Runtime wrapper.PrismAdapter don't inherit MvvmAppBase and VisualStateAware page classes.
But, add same features your application.

Getting start

This section create Hello world application.

Create project

Create empty Universal windows app project. Project name is HelloApp.

Add PrismAdapter from NuGet package manager.
https://www.nuget.org/packages/PrismAdapter/

Install-Package PrismAdapter

  • Soluttion 'HelloApp'
    • HelloApp folder
      • HelloApp.Windows (check install)
      • HelloApp.WindowsPhone (check install)

Edit App.xaml.cs at HelloApp.Shared.

using PrismAdapter;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace HelloApp
{
    /// <summary>
    /// Application
    /// </summary>
    public sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }

        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif
            await PrismAdapterBootstrapper.Initialize(b =>
            {
				// run application
                b.Run(n => n.Navigate("Main", e.Arguments));
				// return task
                return Task.FromResult<object>(null);
            }, e);
        }
    }
}

Create view.

Delete MainPage.xaml from HelloApp.Windows and HelloApp.WindowsPhone projects.
Add Views folder and Views.MainPage.xaml(Empty page) at HelloApp.Shared project.

using PrismAdapter;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace HelloApp.Views
{
    public sealed partial class MainPage : Page
    {
        public PrismNavigationHelper NavigationHelper { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
            this.NavigationHelper = new PrismNavigationHelper(this);
            this.NavigationHelper.SaveState += this.NavigationHelper_SaveState;
            this.NavigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.NavigationHelper.EnablePlatformSupport();
        }

        private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }

        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.NavigationHelper.OnNavigatedFrom(e);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.NavigationHelper.OnNavigatedTo(e);
        }
    }
}

It's very simple! and Code Snippet is packages\PrismAdapter\Snippet folder.

Create ViewModel

Add ViewModels filder and ViewModels.MainPageViewModel class. at HelloApp.Shared project.
Edit MainPageViewModel class.

using Microsoft.Practices.Prism.StoreApps;
using System;

namespace HelloApp.ViewModels
{
    // It is same code at prism.
    public class MainPageViewModel : ViewModel
    {
        private string message;

        public string Message
        {
            get { return this.message; }
            set { this.SetProperty(ref this.message, value); }
        }

        public DelegateCommand GreetCommand { get; private set; }

        public MainPageViewModel()
        {
            this.GreetCommand = new DelegateCommand(() => this.Message = "Hello world at " + DateTime.Now.ToString());
        }
    }
}

Add ViewModelLocator at MainPage.xaml.

<!-- Page tag attribute -->
xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
prism:ViewModelLocator.AutoWireViewModel="True"

and Create page cntent. This sample application behavior is this.
  • Click appbar button.
  • Show "Hello world at timestamp" in screen.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HelloApp.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:ViewModels="using:HelloApp.ViewModels"
    x:Class="HelloApp.Views.MainPage"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d" 
    d:DataContext="{d:DesignInstance ViewModels:MainPageViewModel, IsDesignTimeCreatable=True}">
    <Page.BottomAppBar>
        <CommandBar>
            <AppBarButton 
                Icon="Accept" 
                Label="Greet" 
                Command="{Binding GreetCommand, Mode=OneWay}"/>
        </CommandBar>
    </Page.BottomAppBar>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock 
            TextWrapping="Wrap" 
            Text="{Binding Message}" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            Style="{StaticResource HeaderTextBlockStyle}"/>
    </Grid>
</Page>

Run application!!

  • Run application.
  • click appbar button.
  • show Hello world at timestamp.

This application can run as Windows store app, Windows Phone app.

Using UnityContainer and NavigationService

Unity.dll included PrismAdapter. You can user Unity at Windows 8.1 and Windows Phone 8.1 apps.

Setup UnityContainer

Edit App.xaml.cs.

using HelloApp.ViewModels;
using Microsoft.Practices.Unity;
using PrismAdapter;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace HelloApp
{
    /// <summary>
    /// Application
    /// </summary>
    public sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }

        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif
            await PrismAdapterBootstrapper.Initialize(b =>
            {
				// create unity container
                var c = new UnityContainer();
				// set viewmodel resolve logic.
                b.Resolve = t => c.Resolve(t);
				// fill prism instance to UnityContainer.
                c.RegisterInstance(b.NavigationService.Value);
                c.RegisterInstance(b.SessionStateService.Value);
                c.RegisterInstance(b.FrameFacade.Value);
				// run application
                b.Run(n => n.Navigate("Main", e.Arguments));
				// return task
                return Task.FromResult<object>(null);
            }, e);
        }
    }
}

Add ViewModels.NextPageViewModel and Views.NextPage.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:NavigationApp.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:ViewModels="using:NavigationApp.ViewModels"
    x:Class="NavigationApp.Views.NextPage"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d" d:DataContext="{d:DesignInstance ViewModels:NextPageViewModel, IsDesignTimeCreatable=True}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock TextWrapping="Wrap" Text="{Binding Message}" Style="{StaticResource HeaderTextBlockStyle}" Margin="10,0"/>
    </Grid>
</Page>

using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Microsoft.Practices.Unity;

namespace NavigationApp.ViewModels
{
    public class MainPageViewModel : ViewModel
    {
        [Dependency]
        public INavigationService NavigationService { get; set; }

        public DelegateCommand NavigateCommand { get; private set; }

        public MainPageViewModel()
        {
            this.NavigateCommand = new DelegateCommand(() => this.NavigationService.Navigate("Next", "from MainPage"));
        }
    }
}

Edit MainPageViewModel and MainPage

// MainPageViewModel
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Microsoft.Practices.Unity;

namespace NavigationApp.ViewModels
{
    public class MainPageViewModel : ViewModel
    {
        [Dependency]
        public INavigationService NavigationService { get; set; }

        public DelegateCommand NavigateCommand { get; private set; }

        public MainPageViewModel()
        {
            this.NavigateCommand = new DelegateCommand(() => this.NavigationService.Navigate("Next", "from MainPage"));
        }
    }
}

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:NavigationApp.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:ViewModels="using:NavigationApp.ViewModels"
    x:Class="NavigationApp.Views.MainPage"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d" d:DataContext="{d:DesignInstance ViewModels:MainPageViewModel, IsDesignTimeCreatable=True}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock TextWrapping="Wrap" Text="Nav app" Style="{StaticResource HeaderTextBlockStyle}" Margin="10,0"/>
        <StackPanel Grid.Row="1">
            <Button Content="Navigate" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Command="{Binding NavigateCommand, Mode=OneWay}"/>
        </StackPanel>
    </Grid>
</Page>

Run application

click NextPage AppBarButton. Navigate NextPage. Phone application back page at Hardware back button.

Change log

  • 2014-10-11 ver 1.0
    • Fix
      • code snipetts
    • Change
      • remove Unity dependency.
      • add dependency Prism.StoreApps 1.1.
  • 2014-05-24 ver 0.1.10-beta3
    • Fix
      • Windows 8.1 dependency.
    • Change
      • pagesetup code snippets add import PrismAdapter namespace.
    • Add
      • appsetup code snippets.
  • 2014-05-23 ver 0.1.10-beta2
    • Change
      • Initialize sequence. show sample.
  • 2014-05-23 ver 0.1.10-beta1
    • Change
      • change Unity version.
  • 2014-05-12 ver 0.1.9.0
    • Add
      • IsInitialized at PrismAdapterBootstrapper class.
  • 2014-05-09 ver 0.1.8.8
    • Bug
      • Fix phone japanese language support at EnablePlatformSupport method.
  • 2014-05-07 ver 0.1.8.7
    • Change
      • Add phone japanese language support at EnablePlatformSupport method.
  • 2014-04-19 ver 0.1.8.1-0.1.8.6
    • Change
      • Unity version maintenance.
  • 2014-04-19 ver 0.1.8.0
    • Change
      • Change default value, Page NavigationCache Enable to Disable.
  • 2014-04-19 ver 0.1.7.3
    • Fix
      • Rerun error startup sequence from Windows Phone app.
  • 2014-04-19 ver 0.1.7.2
    • Change
      • PrismAdapterBootstrapper.Current property is ThreadLocal.
  • 2014-04-19 ver 0.1.7.1
    • Fix
      • Rerun error startup sequence from Windows Phone app.
  • 2014-04-11 ver 0.1.7
    • Change
      • EnablePlatformSupport set NavigationCacheMode = Required(Phone only).
  • 2014-04-11 ver 0.1.6
    • Add
      • PrismAdapterBootstrapper RegistPrismInstanceTo(IUnityContaner c) method
    • Change
      • rename EnablePhoneSupport method to EnablePlatformSupport. EnablePhoneSupport method obsolate.
  • 2014-04-06 ver 0.1.3
    • Add
      • add Unity at dependency project.
  • 2014-04-06 ver 0.1.2
    • many bug fix.
  • 2014-04-06 ver 0.1.1
    • many bug fix.
  • 2014-04-05 ver 0.1.0
    • first release.

Lisence

MS-PL.
https://prismadapter.codeplex.com/license

Dependency libraries

Last edited Oct 11, 2014 at 12:18 PM by okazuki, version 21