Prism for Windows Runtime: Creating and showing a Flyout using the FlyoutService class

This is the third post in a series that walk you through creating a simple Windows Store app using the Prism for Windows Runtime library. Please review the second post in the series for the steps necessary to create the basic MVVM style app based on MvvmAppBase.

You can get the latest source on CodePlex.

The following procedure shows how to create and show a Flyout by using the Microsoft.Practices.Prism.StoreApps library. You will discover how to use the FlyoutService class within a view model to show the Flyout, and how to add a SettingsCommand that will launch the Flyout from the Settings pane.

  1. Complete the Creating a basic implementation of the MVVM pattern walk through from the previous post.

  2. First let’s create a Flyout. Add a blank page named SignInFlyout to the Views folder.
    The FlyoutService will use a default convention to look for Flyouts in the Views namespace where the name of the Flyout ends in “Flyout”.

  3. Add a TextBlock to the Grid control in the SignInFlyout page.

     <Grid Background="White">
        <TextBlock Text="Sign In!!!" Foreground="Black" />
  4. Change the SignInFlyout class to derive from the FlyoutView base class in the Microsoft.Practices.Prism.StoreApps library. Also, specify the width in the constructor. The StandardFlyoutSize class is defined in the Microsoft.Practices.Prism.StoreApps library.

     public sealed partial class SignInFlyout : FlyoutView
        public SignInFlyout() : base(StandardFlyoutSize.Narrow)
  5. Update the SignInFlyout.xaml file so that the SignInFlyout view extends the FlyoutView base class in the Microsoft.Practices.Prism.StoreApps library.

  6.   <Infrastructure:FlyoutView
  7. Next, let’s update the StartPage to show the SignInFlyout. Import the interfaces from the Microsoft.Practices.Prism.StoreApps namespace into the StartPageViewModel class.

     using Microsoft.Practices.Prism.StoreApps.Interfaces;
  8. Add a constructor to the StartPageViewModel class that takes a parameter of type IFlyoutService.

     public StartPageViewModel(IFlyoutService flyoutService)
  9. Notice that the StartPageViewModel class does not have a default constructor.

  10. Run the app.

    You will receive a MissingMethodException that indicates that there isn't a parameterless constructor defined for the StartPageViewModel object. This occurs because the ViewModelLocator uses its default convention to locate the StartPageViewModel. The ViewModelLocator then used its default view model factory (Activator.CreateInstance) to construct an instance of the view model. In order for this to work, the type being constructed must possess a default constructor.

  11. Let’s provide the ViewModelLocator with a factory to create view model instances for the StartPage. Override the OnInitialize method in the App class to train the ViewModelLocator how to construct the StartPageViewModel instance.

     protected override void OnInitialize(IActivatedEventArgs args)
                () => new StartPageViewModel(FlyoutService));
  12. The Register method takes a string representation of the view type, and a delegate that returns the view model. You will see that the StartPageViewModel instance is created with the FlyoutService property of the MvvmAppBase class being passed to its constructor. Now that the ViewModelLocator knows how to construct the view model for the StartPage, it will not follow its convention for the StartPage.

  13. Add a Button to the StackPanel in StartPage that will show the SignInFlyout.

        <TextBlock Text="Hello World!!!" />
        <TextBox Text="{Binding FirstName, Mode=TwoWay}" />
        <Button Content="ShowFlyout"
                Command="{Binding SignInCommand}" />
  14. Notice that the Button's Command property will be bound to a SignInCommand in the StartPageViewModel class.

  15. Import the System.Windows.Input namespace into the StartPageViewModel class.

     using System.Windows.Input;
  16. Add the SignInCommand property to the StartPageViewModel class.

     public ICommand SignInCommand { get; private set; }
  17. The property has a private setter because it will only be set internally.

  18. Set the SignInCommand property to an instance of a DelegateCommand that invokes the FlyoutService.

     public StartPageViewModel(IFlyoutService flyoutService)
         SignInCommand = new DelegateCommand(() => flyoutService.ShowFlyout("SignIn"));
  19. The DelegateCommand class, provided by the Microsoft.Practices.Prism.StoreApps library, is constructed with a delegate that will be invoked when the command is executed. Here, a lambda expression is specified that calls the ShowFlyout method of the FlyoutService class, passing the name of the Flyout.

  20. Run the app.

  21. Select the SignIn Button.

    The SignInFlyout appears. Yeah!

  22. Next, let’s add an item to the Settings pane that will also show the SignInFlyout. Stop the app and override the GetSettingsCharmActionItems method in the App class. This method will be called when the CommandsRequested event of the SettingsPane class is raised. Add a SettingsCharmActionItem to the Settings pane that will open the SignInFlyout.

     protected override IList<SettingsCharmActionItem> GetSettingsCharmActionItems()
        return new List<SettingsCharmActionItem>{ new SettingsCharmActionItem("Sign In", 
            () => FlyoutService.ShowFlyout("SignIn"))};
  23. The SettingsCharmActionItem class, provided by the Microsoft.Practices.Prism.StoreApps library, takes two constructor parameters. The first is a string title that will be displayed in the Settings pane. The second is an Action delegate that will be invoked when the user selects the “Sign In” item in the Settings pane.

  24. Run the app. Invoke the charms menu by making a horizontal edge gesture, swiping left with a finger or pointing device from the right of the screen.

  25. Select the Settings charm to display the Settings pane.

  26. Select the “Sign In” item.

    The SignInFlyout appears.

Next let’s integrate a dependency injection container into the app.