Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Using BuildIt.States for Managing States

Almost every software development course at some point covers object orientated programming where the concept of encapsulation is drummed in. We’re taught to create classes that track state – a typical example is an Order that has a series of items, and then goes through various states as the order is processed. All this information is held within the Order object, including some basic functions such as calculating the total of the items being ordered. As the systems we work on grows in complexity, so does the complexity of the classes that are used to encapsulate aspects of the system. Unfortunately this often results in large, bloated classes, where it’s hard to make changes without fear of breaking other parts of the system.

One common mistake that seems to creep in over time is that it becomes difficult to manage what state a class is in. This can result from either a lack of documentation about what states the class can be in, or the states themselves aren’t clearly defined. Take the Order example, the Order class may have properties such as OrderTimestamp and OrderId which are set when the Order is placed. If the Order is rejected, the OrderTimestamp may be reset, leaving the class is some weird state where it has an OrderId, yet no OrderTimestamp. This is one of the scenarios that BuildIt.States is designed to help with.

Before we jump in, we need to set down a basic understanding of how we define states. States can appear in groups but within a group, there can only ever be one current state. For example in the Order scenario, there might be a group called TransactionStates, and the Order can only be in one of the states, Unprocessed, Processing, Processed, OrderRejected, at any given time. This leads me to the starting point of working with BuildIt.States: I’m going to define an enumeration that defines the states that my class can go between – you’ll notice this includes a Default (I often use Base instead) which BuildIt.States will use to determine if the state has been set or not.

public enum MainStates
{
    Default,
    One,
    Two,
    Three,
    Four
}

The next thing we need is our class, in this case one that inherits from NotifyBase, and raises a PropertyChanged event when the CurrentState property changes.

public class MainViewModel:NotifyBase
{
    private string currentState;

    public string CurrentState
    {
        get { return currentState; }
        set
        {
            currentState = value;
            OnPropertyChanged();
        }
    }
}

Defining the states that this class can be in is as simple as adding a reference to the BuildIt.States NuGet package and then defining each of the states:

public IStateManager StateManager { get; } = new StateManager();

public MainViewModel()
{
    StateManager
        .Group<MainStates>()

        .DefineState(MainStates.One)
        .ChangePropertyValue(vm => CurrentState, "State One")

        .DefineState(MainStates.Two)
        .ChangePropertyValue(vm => CurrentState, "State Two")

        .DefineState(MainStates.Three)
        .ChangePropertyValue(vm => CurrentState, "State Three")

        .DefineState(MainStates.Four)
        .ChangePropertyValue(vm => CurrentState, "State Four");

}

The notation for defining state groups and states is very fluid in nature and we’re always looking for ways to make defining states more intuitive. Essentially each of these states updates a single property, CurrentState, with a new constant value.

Making use of this class is as simple as creating an instance of the MainViewModel and setting it as the DataContext for the page:

public sealed partial class MainPage : Page
{
    private MainViewModel ViewModel { get; } = new MainViewModel();
    public MainPage()
    {
        this.InitializeComponent();

        DataContext = ViewModel;
    }

    private void StateOneClick(object sender, RoutedEventArgs e)
    {
        ViewModel.StateManager.GoToState(MainViewModel.MainStates.One);
    }

    private void StateTwoClick(object sender, RoutedEventArgs e)
    {
        ViewModel.StateManager.GoToState(MainViewModel.MainStates.Two);
    }

    private void StateThreeClick(object sender, RoutedEventArgs e)
    {
        ViewModel.StateManager.GoToState(MainViewModel.MainStates.Three);
    }

    private void StateFourClick(object sender, RoutedEventArgs e)
    {
        ViewModel.StateManager.GoToState(MainViewModel.MainStates.Four);
    }
}

When this is run, clicking through each of the buttons triggers a state change in the MainViewModel instances. This in turn causes the CurrentState property to change, which is shown on the page via data binding. The really awesome thing about the way states are defined, is that you can have any number of properties being set, and the library is smart enough to set the property values back to what they were originally when the class moves to a different state.

*Note that this library is currently in beta

Migrating BuildIt to .NETStandard

At the beginning of the year I blogged quite a bit about a set of libraries that we use at Built to Roam that are available either from NuGet (https://www.nuget.org/packages?q=BuildIt) or as source on GitHub (https://github.com/builttoroam/BuildIt). These started a few years ago as an accumulation of helper functions for building, as the time Windows Phone applications. Since then the libraries have matured a bit and have evolved somewhat. The BuildIt.General library is still just a collection of helper method, whilst some, like BuildIt.Lifecycle, are more framework in nature. Over the coming weeks we’re investing a bit into tidying up these libraries, including:

  • Migrating any of the libraries that were targeting PCL profile 259 across to .NET Standard 1.0
  • Updating, where possible, all NuGet references
  • Setting up an automated build and release process, including support for publishing directly to NuGet
  • Rationalise the number of libraries on NuGet – namely where there are platform specific libraries, these will be combined in with the core library (eg BuildIt.General.UWP will get packaged with BuildIt.General and will be installed for UWP projects)
  • Add some additional packages
    • BuildIt.Media.UWP – This allows you to easily add Cortana voice commands for controlling media playback with the MediaElement control
    • BuildIt.Web.Config (this name may change) – This allows you to easily define application configuration values server side and have them flow to the client application

Right now, we’re going through and migrating the libraries to .NET Standard. This is a painful process as there are often NuGet references that don’t work with .NET Standard – we’re either removing these or deciding to wait on the package author to update. During this phase a lot of the packages will be marked as beta -  you should be able to start using these libraries but just be aware we haven’t completed the migration, so things may change over the coming weeks.

Updating BuildIt

It's been a while but I've just published an update to the BuildIt libraries with the aim to streamline the syntax for declaring states. For example, the following are the state declarations for the Flickr viewer sample application:
StateManager.Group<RegionStates>()
 .WithHistory()
  .DefineStateWithData<RegionStates, MainViewModel>(RegionStates.Main) 
   .WhenChangedTo(vm => vm.Load())
   .OnDefaultCompleteWithData(vm => vm.SelectedPhoto)
    .ChangeState(RegionStates.Details)
     .InitializeNewState<RegionStates, MainViewModel, DetailsViewModel, Photo>  
         ((vm, d) => vm.Photo = d)
  .DefineStateWithData<RegionStates, DetailsViewModel>(RegionStates.Details);

Triggering State Changes in BuildIt.States

The state management capability that BuildIt.Lifecycle uses is actually provided by a standalone library BuildIt.States which focusses on tracking states and allowing transitions between states. BuildIt.Lifecycle builds on this to adapt it to generating view models for specific states, along with the glue that connects states and transitions to the appropriate platform implementation (eg Pages and Navigation). The nice thing about the state management library being separate is that it can iterate independently. In this case, we’ve updated the library to include support for triggers.

One of the new features added to the Universal Windows Platform (UWP) for Windows 10, are state triggers. Out of the box, UWP comes with an AdaptiveTrigger class which is used to trigger a state change once a minimum width or height (or both) have been reached by a Window. The way triggers work is that they evaluate a particular condition and once the condition has been met, the IsActive property is changed to true. Once all triggers for a particular state are set to IsActive, the framework changes the active visual state. We’ve adapted this concept to apply it to state management within the BuildIt.States library.

Taking the FlickrViewer application that I discussed in my post, Building a Flickr Viewer App with BuildIt.Lifecycle, I’ll modify the MainViewModel to use triggers. Here you’ll see that in addition to adding triggers to the state definition, we’re also making use of an instance of the LoadingManager. A common scenario is to have states for when data is being loaded, and when it has been loaded. To handle this, the state management library includes a LoadingManager class which tracks which page/viewmodel state is used for both Loading and Loaded states. The nice artefact of using the LoadingManager is that the MainViewModel doesn’t explicitly need to indicate when it’s loading or not loading data. Instead it simply wraps the loading operation in a using statement. Behind the scenes this links to the LoadingTrigger instances used in the state declaration.

public enum MainStates
{
    Base,
    Loading,
    Loaded
}

LoadingManager<MainStates> LoadingManager { get; } = new LoadingManager<MainStates>()
{
    LoadingState = MainStates.Loading,
    LoadedState = MainStates.Loaded
};

public MainViewModel(IFlickrService flickr)
{

    StateManager
        .Group<MainStates>()
        .DefineState(MainStates.Loading)
            .AddTrigger(new LoadingTrigger<MainStates>(LoadingManager) {ActiveValue = MainStates.Loading})
        .DefineState(MainStates.Loaded)
            .AddTrigger(new LoadingTrigger<MainStates>(LoadingManager) {ActiveValue = MainStates.Loaded});
    Flickr = flickr;
}

public async Task Load()
{

    using (LoadingManager.Load())
    {
        var photos = await Flickr.LoadPhotos();
        photos.DoForEach(Photos.Add);
    }         
}

Building a Flickr Viewer App with BuildIt.Lifecycle

Last week at the Mobile .NET User Group I presented on the BuildIt.Lifecycle for the first time. The main motivation wasn’t to try and get people to start using it (although I’d definitely welcome feedback from anyone who does), it was really to try to get developers to start thinking differently about the way that they write applications. Instead of thinking of their application in terms of the apis that any one platform offer (and I treat XForms as just another platform, even though it sort of in-part aggregates iOS/Windows and Android) or in terms of raw navigation constructs (eg Activities/Intents, ViewControllers or Pages), but more in terms of states within their application.

Applications are littered with states, whether they be the states of a button (Normal, Focussed, Pressed, ToggledOn etc) or visual states of a Page (a Windows concept but you can think of visual states as being the different layouts of any given page/view of your application). In fact if you think of your application as a whole you realise that you can map the entire navigation within your application as just a series of state transitions, with your pages/views being the states that the application can be in. This is indeed the foundation of BuildIt.Lifecycle.

One other point I’ll make before jumping into building the Flickr Viewer application, which should demonstrate the core of the BuildIt.Lifecycle framework, is that most application frameworks, at least for mobile platforms, have been built around the notion of a single window or frame that houses the content that the user is currently looking at. As such most of the frameworks have some metaphor that maps to the application as a whole (often called the App or Application class), and is assumed to be a singleton. In exploring the use of states to model applications, it became evident that there are some platforms, such as Windows and Mac, where this assumption breaks down, and that in fact the framework needs an additional metaphor that maps to each window within an application. I’ve settled on Region for the timebeing, and there’s no surprises that there is a mapping between an instance of a Region in the framework to a Window presented at runtime. This should make more sense once we explore creating the application; alternatively there’s a post on working with Additional Windows using Regions with BuildIt.Lifecycle.

Let me start by giving an example of visual states, just to warm up and get us all thinking about using states to represent an application, or parts of the application. I’m going to start by creating a new Universal Windows Platform project, based on the Blank App (Universal Windows) project template, called FlickrViewer.

image

Next I’ll create another project, this time called FlickrViewer.Core, based on the Class Library (Portable) template, keeping the defaults in the Add Portable Class Library targets dialog.

image

To complete the setup of the projects, I just need to add a reference from the FlickrViewer UWP project to the FlickrViewer.Core PCL project.

image

The last reference I’m going to add initially is to the Microsoft.Net.Http and Newtonsoft.Json NuGet packages (Note that under the Install and Update Options the Dependency behaviour is set to Highest, and both packages are install into both projects).

image 

I’m going to add the FlickrService class (and corresponding IFlickrService interface) that’s listed at the end of this article, which I’ll use to download content from the public flickr feed, to the PCL. This application is going to consist of two pages, the first will display a list of images retrieved from the Flickr public feed; the second page will display the details of the image selected from the first page. I’ll add another page at this point called DetailsPage.xaml, based on the Blank Page item template.
Next, I’m going to switch across to Blend and create a couple of visual states: a Loading state, for when data is being loaded; and a Loaded state, for when data is available to be displayed on the screen. The following XAML for the MainPage includes a ListView, which will be visible once data has loaded (ie Loaded state), and a ProgressRing, which will be visible and active while data is being loaded (ie Loading state).

<Page x:Class="FlickrViewer.MainPage"
      xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:FlickrViewer"
      xmlns:d="
http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="MainStates">
                <VisualState x:Name="Loading">
                    <VisualState.Setters>
                        <Setter Target="progressRing.(UIElement.Visibility)" Value="Visible" />
                        <Setter Target="progressRing.(ProgressRing.IsActive)" Value="True" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="Loaded">
                    <VisualState.Setters>
                        <Setter Target="listView.(UIElement.Visibility)" Value="Visible" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <ListView x:Name="FlickrListView" Visibility="Collapsed">
          
<ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Height="100" Margin="0,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding media.m}"
                               Stretch="UniformToFill"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center" />
                        <TextBlock Text="{Binding title}"
                                   Grid.Column="1"
                                   TextWrapping="WrapWholeWords"
                                   FontSize="20"
                                   Margin="10,0"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ProgressRing x:Name="LoadingProgress"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Visibility="Collapsed"
                      Width="100"
                      Background="Red"
                      Height="100" />

    </Grid>
</Page>

When the MainPage loads, I’ll create an instance of the FlickrService and invoke the LoadPhotos method in order to retrieve data to be displayed on the MainPage.

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    VisualStateManager.GoToState(this, "Loading", true);
    var flickr = new FlickrService();
    var photos = await flickr.LoadPhotos();
    FlickrListView.ItemsSource = photos;
    VisualStateManager.GoToState(this, "Loaded", true);
}

Running the application at this point will display the progress ring while the data is being downloaded (Loading state), followed by the list of photos once the data has been retrieved (Loaded state).

image

So now that you’ve got the idea of visual states, let’s move on to talking about the application in terms of states. I’m going to use the BuildIt.Lifecycle framework, so I’ll start by adding references to the NuGet packages. Into the FlickrViewer project, I’m going to add the BuildIt.Lifecycle.UWP package; into the FlickrViewer.Core project, I’m going to add the BuildIt.Lifecycle package (there will be some refactoring coming which will consolidate these into a single package soon but for now there are platform specific packages to be added).

As with most frameworks I need to add a class to the PCL that will correlate to the application as a whole. There are a couple of different options available as base classes; in this case I’m going to pick the one that is already setup to be able to handle multiple regions, despite not actually requiring it for this application. Currently this base class does require the DefineApplicaitonRegions method to be defined, but in this case since we are only going to define a single region, PrimaryRegion, there is no need to do anything within this method. The type, PrimaryRegion, is provided as a generic argument when inheriting from the RegionAwareBaseApplication class, which registers and identifies the PrimaryRegion class as the first region of the application.

public class RootApplication : RegionAwareBaseApplication<PrimaryRegion>
{
    protected override void DefineApplicationRegions()
    {
    }
}

Next, I need to define the PrimaryRegion class, and for this I’m going have it inherit from the StateAwareApplicationRegion class. It’s possible to define regions of the application that aren’t state aware, meaning that the developer has to provide some other mechanism for navigating between pages/views. In this case, we want to think of our application in terms of states. Rather than thinking about the navigation through our application as going from MainPage to the DetailsPage, I’ll define two states, Main and Details, between the application will transition. These states are easily defined as enum values, and used within the PrimaryRegion to define what makes up each state:

public class PrimaryRegion : StateAwareApplicationRegion
{
    public enum RegionStates
    {
        Base,
        Main,
        Details
    }

    public PrimaryRegion()
    {
        StateManager.Group<RegionStates>()
            .DefineState(RegionStates.Main)
            .DefineState(RegionStates.Details);
    }
}

On of the interesting things about the pages of the PrimaryRegion is that they correspond to pages within the application, which in turn need a view model to data bind to. This is how most developers think of this relationship. Actually it’s almost the other way around. Each of the states within the PrimaryRegion will have a view model (think of this as the data that is related to that state). The platform specific implementation of moving to a particular state will display a page that correlates to the state, and will data bind the corresponding view model to the page. Hopefully this helps set the stage for the next step – we’re going to go back to the StateManager within the PrimaryRegion, and this time change to defining view models for each state:

public PrimaryRegion()
{
    StateManager.GroupWithViewModels<RegionStates>()
        .StateWithViewModel<RegionStates, MainViewModel>(RegionStates.Main)
        .EndState()
        .StateWithViewModel<RegionStates, DetailsViewModel>(RegionStates.Details)
        .EndState();
}

You’ll notice that this makes use of two view models, MainViewModel and DetailsViewModel, which I’ll need to create in the PCL. The code associates each of the view model types with the corresponding state enum value. For the timebeing the view models will be relatively basic, only inheriting from NotifyBase which is a simple implementation of the INotifyPropertyChanged interface.

public class MainViewModel:NotifyBase
{ }
public class DetailsViewModel:NotifyBase
{ }

So far I’ve added two states, with view models, to the PrimaryRegion but it doesn’t know which state to start at. By overriding the CompleteStartup method, it’s possible to direct the StateManager to go directly to the Main state.

protected override async Task CompleteStartup()
{
    await base.CompleteStartup();

    await StateManager.GoToState(RegionStates.Main);
}

The navigation to actual pages in the UWP application is handled at the UI layer, and relies on there being a known association between the state that the application is in, and the corresponding page that should be displayed. Some frameworks rely on convention to do this; we take the approach that we prefer this to be explicit, which has the secondary benefit of avoiding reflection. When the application launches, I need to register the MainPage and DetailsPage to the corresponding state:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;

    LifecycleHelper.RegisterView<MainPage>().ForState(PrimaryRegion.RegionStates.Main);
    LifecycleHelper.RegisterView<DetailsPage>().ForState(PrimaryRegion.RegionStates.Details);
}

I’m also going to replace all the startup logic that would normally reside in the OnLaunched method in the App.xaml.cs file, with the following code:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    var core = new RootApplication();
    var wm = new WindowManager(core);
    await core.Startup();
}

From this code you can I’m creating an instance of the RootApplication, which knows about the PrimaryRegion, which in turn manages the state transitions between Main (ie MainPage) and Details (DetailsPage). The WindowManager introduces all the glue necessary to navigate to the appropriate pages with the application, and to ensure the window completely loads by calling the Activate method.

For the timebeing I’m going to replace the contents of the MainPage with the following TextBlock and Button, contained within a vertical StackPanel.

<StackPanel>
    <TextBlock Text="{Binding Data}"
                FontSize="40" />
    <Button Click="GoToSecond">Go</Button>
</StackPanel>

Into the MainViewModel I’ll add a simple property that returns a static value:

public string Data { get; } = "Hello World!";

Running the application displays Hello World! on the screen, indicating that the MainViewModel has been setup as the DataContext for the MainPage that’s being displayed.

image

Next, is to wire up the Go button to transition the application to the Details state, in order to display the DetailsPage. The change in state needs to be controlled by the StateManager that belongs to the PrimaryRegion. This means that in order to change state, the Main state needs to raise an event, or send a message, to signal to the PrimaryRegion. In actual fact, it’s the MainViewModel that will raise the event but rather than simply define a custom event I will instead inherit from the BaseViewModelWithCompletion class, using the DefaultCompletion enum which defines a single usable value of Complete (if the state had multiple states it could transition to, I could define a new enum that has a value for each state to transition to). The MainViewModel class now looks like:

public class MainViewModel : BaseViewModelWithCompletion<DefaultCompletion>
{
    public string Data { get; } = "Hello World!";

    public void Complete()
    {
        OnComplete(DefaultCompletion.Complete);

    }
}

The Complete method simply gets invoked by the event handler for the Button

private void GoToSecond(object sender, RoutedEventArgs e)
{
    (DataContext as MainViewModel).Complete();
}

The last thing is to handle the completion of the MainViewModel/Main state in the PrimaryRegion. This can be done by extending the definition of the Main state.

StateManager.GroupWithViewModels<RegionStates>()
    .StateWithViewModel<RegionStates, MainViewModel>(RegionStates.Main)
        .OnComplete(DefaultCompletion.Complete)
        .ChangeState(RegionStates.Details)
    .EndState()
    .StateWithViewModel<RegionStates, DetailsViewModel>(RegionStates.Details)
    .EndState();

Clicking the Button will transition the application from the Main to Details state, and in doing so navigate from the MainPage to the DetailsPage. You’ll also see that a back button appears in the top navigation bar, allowing the transition back to the Main state – this works without having to manually define the back button behaviour.

image

I’ll return now to the MainPage and look at how to populate it with data. There are three main steps to this:

- Register the FlickrService
- Associated the FlickrService with the MainViewModel
- Use the FlickrService to populate data within the MainViewModel

The registration of services happen at an application level but can be done either within the RootApplication, if the implementation of the services is accessible within the PCL (like our FlickrService), of it can be done on App startup as part of the call to Startup using a delegate callback to register platform specific services. For example either the following:

RootApplication

protected override void RegisterDependencies(ContainerBuilder builder)
{
    base.RegisterDependencies(builder);

    builder.RegisterType<FlickrService>().As<IFlickrService>();
}

Or, App.Xaml.cs

await core.Startup(builder => {
       builder.RegisterType<FlickrService>().As<IFlickrService>();
});

In this case, since the FlickrService doesn’t rely on any platform specific implementation I’ll register it in the RootApplication.

Next, we want the instance of the FlickrService to be passed into the MainViewModel so that we can use it to retrieve feed data. To do this we rely on constructor injection by specifying an IFlickrService parameter to the MainViewModel constructor.

public IFlickrService Flickr { get; }

 

public MainViewModel(IFlickrService flickr)
{
    Flickr = flickr;
}

And then I need to expose a collection of photos and provide a method that will invoke the LoadPhotos method on the IFlickrService.

public ObservableCollection<Photo> Photos { get; } = new ObservableCollection<Photo>();
public async Task Load()
{
    var photos = await Flickr.LoadPhotos();
    photos.DoForEach(Photos.Add);
}

A lot of other MVVM style frameworks rely on the view models inheriting from a base view model which has init/dispose style methods defined for when the view model comes into focus or leaves focus (ie the user arrives or leaves at a page). In this case, because we’re defining our application behaviour in terms of states, we want to invoke code when we arrive at the Main state. Again we do this within the PrimaryRegion by adding a call to WhenChangedTo to the Main state definitiion.

.StateWithViewModel<RegionStates, MainViewModel>(RegionStates.Main)
    .WhenChangedTo(vm => vm.Load())
               
    .OnComplete(DefaultCompletion.Complete)
    .ChangeState(RegionStates.Details)
.EndState()

Before running the application at this point we need to return to our MainPage.xaml and return the layout to what we initially created with the ListView, the ProgressRing and the visual states. I needed to make two minor adjustments:

- Make sure the ListView is visible initially – since we don’t have anything to drive the Loaded/Loading visual state changes, yet
- Add the ItemsSource attribute to be data bound to the Photos property ie ItemsSource=”{Binding Photos}”

Running the application now should display a list of photos, similar to what I had at the beginning of this post. To add back the visual states, all I need to do is adjust the MainViewModel to include its own state manager:

public class MainViewModel : BaseStateManagerViewModelWithCompletion<DefaultCompletion>
{
    public enum MainStates
    {
        Base,
        Loading,
        Loaded
    }

    public MainViewModel(IFlickrService flickr)
    {
        StateManager.Group<MainStates>().DefineAllStates();
        Flickr = flickr;
    }

Into the MainViewModel we’ve added a new enumeration, whose values match the names of the visual states. I’ve also adjusted the base class for the MainViewModel to be one that already has a state manager defined. And finally I’ve called DefineAllStates which will create all the states that correlate to the values in the MainStates enumeration. That’s it! Run the application again, and the visual state changes are already wired up.

The final stretch of this application is handling when the user selects an item from the list of photos. The selected photo needs to be passed through to the details page so that the details of the photo can be displayed on the page. A lot of developers prefer to use command binding to pass through the click event to the method in the view model. To keep it simple I’m just going to wire up an event handler for the ItemClick event on the ListView

<ListView x:Name="FlickrListView"
            ItemsSource="{Binding Photos}"
            IsItemClickEnabled="True"
            ItemClick="FlickrListView_OnItemClick"
>
private void FlickrListView_OnItemClick(object sender, ItemClickEventArgs e)
{
    (DataContext as MainViewModel).DisplayPhoto(e.ClickedItem as Photo);
}

Inside the DisplayPhoto method on the MainViewModel I’m going to track the SelectedPhoto before calling the OnComplete method.

public Photo SelectedPhoto { get; private set; }
public void DisplayPhoto(Photo photo)
{
    SelectedPhoto = photo;
    OnComplete(DefaultCompletion.Complete);
}

In order to pass the selected photo into the DetailsViewModel, the state manager in the PrimaryRegion needs to be updated to hand the selected photo into the DetailsViewModel when the Details state is transitioned to.

StateManager.GroupWithViewModels<RegionStates>()
    .StateWithViewModel<RegionStates, MainViewModel>(RegionStates.Main)
        .WhenChangedTo(vm => vm.Load())
       
.OnCompleteWithData(DefaultCompletion.Complete, vm => vm.SelectedPhoto)
        .ChangeState(RegionStates.Details)
    .EndState()

    .StateWithViewModel<RegionStates, DetailsViewModel>(RegionStates.Details)
        .WhenChangedToWithData<RegionStates, DetailsViewModel, Photo>((vm, data) =>
        {
            vm.Photo = data;
        })
    .EndState();

There are two updates to the StateManager:
- The OnComplete in the Main state is replaced with OnCompleteWithData which is used to extract the SelectedPhoto so that it can be passed through to the new state (in this case the Details state)
- A call to WhenChangedToWithData is used to pass through the selected photo into the Photo property on the DetailsViewModel.

Of course, the DetailsViewModel needs to be updated to include the Photo property. As the Photo property may not be set by the time the DetailsViewModel is set as the DataContext on the page, it’s important that the Photo property raises the PropertyChanged event in the setter.

public class DetailsViewModel : NotifyBase
{
    private Photo photo;

    public Photo Photo
    {
        get { return photo; }
        set
        {
            photo = value;
            OnPropertyChanged();
        }
    }
}

The last thing to do is to update the design of the DetailsPage.xaml to render the photo:

<Page x:Class="FlickrViewer.DetailsPage"
      xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:FlickrViewer"
      xmlns:d="
http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image Source="{Binding Photo.media.m}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               Stretch="UniformToFill" />
        <Grid VerticalAlignment="Bottom">
            <Border Background="Black"
                    Opacity="0.9"/>
            <TextBlock Text="{Binding Photo.title}"
                       TextWrapping="WrapWholeWords"
                       FontSize="30"
                       Foreground="White"
                       MaxLines="3"
                       Margin="20,10" />
        </Grid>

    </Grid>
</Page>

Running the application now gives the following interface for viewing the public Flickr feed.

image


********************************************************************************************************************************************************
                 Flickr Download Service

********************************************************************************************************************************************************

public interface IFlickrService
{
    Task<IEnumerable<Photo>> LoadPhotos();
}

public class FlickrService : IFlickrService
{
    public async Task<IEnumerable<Photo>> LoadPhotos()
    {
        var client = new HttpClient();
        var json = await client.GetStringAsync(
                    "
http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1");
        var photos = JsonConvert.DeserializeObject<FlickrData>(json);
        return photos.items;
    }
}

public class Media
{
    public string m { get; set; }
}

public class Photo
{
    public string title { get; set; }
    public string link { get; set; }
    public Media media { get; set; }
    public string date_taken { get; set; }
    public string description { get; set; }
    public string published { get; set; }
    public string author { get; set; }
    public string author_id { get; set; }
    public string tags { get; set; }
}

public class FlickrData
{
    public string title { get; set; }
    public string link { get; set; }
    public string description { get; set; }
    public string modified { get; set; }
    public string generator { get; set; }
    public List<Photo> items { get; set; }
}

Conditional Back Button Visibility with BuildIt.Lifecycle

In my post, Adding Back Button Support to Xamarin.Forms with BuildIt.Forms, I showed the basic support I’d added to BuildIt.Lifecycle to handle showing/hiding the back button depending on whether there were previous states. The back button visibility is actually more complex than that as there may be conditions on the current page (ie the current state/view model) that determine whether clicking back should be possible. To keep things simple I’m going to assume that if clicking back is possible, the back button will be shown, otherwise it will be hidden – this may not suit everyone as you may want to show the back button but have it disabled (personally I don’t like this, as it’s hard for the user to understand why the back button is disabled). I’ve just added support to the states and corresponding viewmodel management to detect if the current view model is in a blocked state.

There is an interface IIsAbleToBeBlocked that a view model can implement in order to both return an IsBlocked property and raise an event when it changes. This interface has also been added to the BaseViewModel class to make it easier for developers. For example this method can be used within a view model to toggle the visibility of the back button. If IsBlocked is true, the back button will be hidden.

public async void Run()
{
    for (int i = 0; i < 10; i++)
    {
        IsBlocked = !IsBlocked;
        await Task.Delay(1000);
    }
}

Note that the IsBlocked property works in conjunction with the HasHistory property on the state group (which maps to the back stack at a page level). If there is no history in the state group (ie at the first stage of the application – the first page of the application), then the back button won’t be shown, irrespective of the IsBlocked property.

Arriving or Leaving a ViewModel with BuildIt.Lifecycle

Often when arriving at a page/view it’s necessary to invoke some code in order to load data, or refresh the contents on the page. Of course, this needs to happen within the ViewModel. For BuildIt.Lifecycle, this is means implementing the IArrivingViewModelState interface which defines a single method, Arriving, which will get invoked when the user arrives at the page/view (and the corresponding viewmodel).

public async Task Arriving()
{
    await Task.Delay(2000);
    Name += ".... arrived ....";
}

Note that the method returns a Task, so you can do asynchronous code in order to retrieve data.

On the Windows platform the Universal Windows Platform has methods OnNavigatedTo, OnNavigatingFrom and OnNavigatedFrom which can be overridden in the code behind of a Page. These correspond to when a user arrives, is about to leave and has left a page. The Arriving method in BuildIt.Lifecycle maps to the OnNavigatedTo method. There are two more interfaces, IAboutToLeaveViewModelState and ILeavingViewModelState, which can be used to define methods AboutToLeave and Leaving. These methods map to the OnNavigatingFrom and OnNavigatedFrom methods.

One thing to note about the AboutToLeave method is that it has a CancelEventArgs as a parameter. The code in the AboutToLeave method can set the Cancel property on this parameter to true in order to cancel the navigation away from the page (note that this maps to cancelling the state change which drives the change in pages).

public async Task AboutToLeave(CancelEventArgs cancel)
{
    cancel.Cancel = true;
}

Adding Back Button Support to Xamarin.Forms with BuildIt.Forms

In my previous post I discussed the support I added for hardware and virtual back buttons in a Universal Windows Platform application (UWP) using BuildIt.Lifecycle. At an application level the back button (irrespective of whether it’s a hardware back button, such as on a phone, or a virtual back button, such as in desktop or tablet mode) is designed to return the user to the previous state of the application. In most cases this correlates to going to either the previous page in an application, or to the previous application, if the user is on the first page of the current application. Occasionally, there may be sub-states within a page, in which case the back button should be able to step back through those states, before causing the application to go back to the previous page.

This means that the back button should be applied at the highest level of the active window, which means passing it into StateManager of the region that correlates to the active window. In a UWP application, this is relatively straight forward as the application can subscribe to the BackRequested event, for example:

SystemNavigationManager.GetForCurrentView().BackRequested += BackRequested;

In the event handler, the StateManager for the region is queried to find out if a previous state exists. If it does, a call to GoBackToPreviousState is made.

Unfortunately Xamarin.Forms (XForms) doesn’t offer a global event for the back button. Instead it’s up to every page to override the OnBackButtonPressed method and return true if the back navigation should be cancelled. Having every page of the XForms application inherit from a custom base page isn’t an option I wanted to force upon users of this library. Luckily it’s not necessary to intercept the OnBackButtonPressed method on every page; it can instead be intercepted in the NavigationPage at the root of the application. By providing a CustomNavigationPage class, and requiring that it be used as the root of the application, it’s possible for the back button to be intercepted and applied to the active region.

Back Button Support in BuildIt.Lifecycle

Previously I’ve dealt with going back to previous pages based on user interaction on the current page (eg hitting a back button that triggers an “end of state” behaviour to go back to the previous page). However, another common scenario is for the user to press tha back button, either on their device (eg the Windows Phone hardware back button) or a virtual back button (eg the virtual button that’s available to Windows 10 UWP applications either in the task tray (running in tablet mode) or in the top navigation bar, when running in desktop mode). This wasn’t currently handled, so I’ve added handlers for both the hardware back button and the virtual back button to the UWP branch of BuildIt.Lifecycle. This work isn’t totally complete because at the moment it assumes that the decision as to whether the current page supports a back operation is determined only by the root level StateManager – this may not be the case if you have a view model that has business logic that determines whether the current page can be navigated way from.

This update also supports hiding and showing the virtual back button when a UWP application is run in desktop mode eg:

image image

Fixing Back Behaviour and Navigation Button in Xamarin Forms with BuildIt.Lifecycle

There were two issues that I identified in my post, Using BuildIt.Lifecycle with Xamarin Forms, which were to do with the navigation between pages

1. The back operation actually did a forward navigation to a new page

2. The navigation button was always showing that there was a previous page, even if there wasn’t

The first issue was fixed with an update to the BuildIt.Lifecycle.XForms library which picks up on the IsNewState flag during state transition to determine whether to do a forward navigation (ie a PushAsync) or a backward navigation (ie a PopAsync).

The second issue was fixed by looking at the Navigation.NavigationStack and based on whether there were any entries in there, toggle whether the navigation bar is shown. The following would hide the navigation bar (it’s a static method on the NavigationPage class).

NavigationPage.SetHasNavigationBar(NavigationRoot, false);

Visual States in XForms with BuildIt.Lifecycle

When I created the layout for the AboutPage in my post, Using BuildIt.Lifecycle with Xamarin Forms, I cut out most of the elements to make it quicker to get something up and running. The irony is that fixing up the AboutPage to include a SplitView like pane was actually relatively quick, although there’s a bit of polish that needs to be added to make it look great. The following is the XAML for the AboutPage which includes two columns: the left column, initially hidden, includes the Close button which allows the user to navigate back to the home page of the app; the right column holds a couple of buttons that can switch states of the page between Expanded and Minimised. Note that there is another button that sits in the top left corner which will simply toggle between the states.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="
http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleStates.XForms.Pages.AboutPage">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid BackgroundColor="Yellow"
          x:Name="SplitPane"
          IsVisible="False">

      <Button Clicked="CloseClick"
              VerticalOptions="Center"
              HorizontalOptions="Center"
              Text="Close"/>
    </Grid>

    <StackLayout Grid.Column="1"
                 VerticalOptions="Center"
                 HorizontalOptions="Center" >
      <Label  Text="About Page"
              FontSize="40" />
      <Label  Text="{Binding AboutTitle}"
              FontSize="40" />
      <Label  Text="{Binding Data}"
              FontSize="40" />
      <Button Clicked="ExpandClick" Text="Expand"/>
      <Button Clicked="CollapseClick" Text="Collapse"/>
    </StackLayout>
    <Button
      Clicked="splitViewToggle_Click"
      Text="="
      HorizontalOptions="Start"
      VerticalOptions="Start"/>
  </Grid>
</ContentPage>

Unfortunately the XForms XAML doesn’t support defining visual states, so we have to define these in code using the StateManager class. By specifying the IHasStates interface, the framework will automatically associate the StateManager on the AboutPage with the StateManager in the AboutViewModel, keeping them in sync.

public partial class AboutPage : IHasStates
{
    public IStateManager StateManager { get; }
   
    public AboutPage()
    {
        InitializeComponent();

        StateManager = new StateManager();
        StateManager
            .Group<AboutExpandStates>()
                .DefineState(AboutExpandStates.Minimised)
                .DefineState(AboutExpandStates.Expanded)
                    .Target(SplitPane)
                        .Change(x => x.IsVisible, (x, c) => x.IsVisible = c)
                        .ToValue(true);
    }

Using BuildIt.Lifecycle with Xamarin Forms

Leaving the Universal Windows Platform behind for a little bit I wanted to show that the BuildIt.Lifecycle framework can be used to drive a Xamarin Forms (XForms) application.

The first step is to create a Xamarin Forms project. In this case I’ll select the Portable option.

image

This will actually create four projects, one which is contains the actually application logic, including the XAML markup for the pages, and then three platform specific projects for iOS, Droid and Windows Phone. The platform specific projects are where any code required for device integration or custom UX goes for each platform.

After creating the projects, the first thing to do is to upgrade the Xamarin.Forms NuGet reference to the latest version (v2.0.1.6495 at time of writing).

The next thing to do is to add references to the BuildIt.Lifecycle NuGet packages including the Xamarin Forms specific package (https://www.nuget.org/packages/BuildIt.Lifecycle.XForms ). I had a few issues here because I initially had the dependency setting in the NuGet manager set to Lowest and it refused to install one of the dependencies (NuGet still isn’t clever enough some times to resolve everything properly). Switching this to Highest, which I think should be the default, fixed this issue.

Of course, the core XForms project needs a reference to the SimpleStates.Core library which contains all my application, region and view model classes. At this point I realised I hadn’t set the PCL profile correctly on the project. Updating this turned out to be a bit of a mission as Visual Studio insisted the project had to be switched to NuGet v3, which you can’t do with any packages installed (according to the error message). So I had to uninstall all the NuGet packages (in the correct order) one at a time, and then update the targets as per the Change Targets dialog for the project.

image

Now that the projects have all the references I need, it’s time to wire up the application. To do this I need to initialise the BuildIt.Lifecycle application, and I need a XForms page that maps to each of the pages (ie my states) in the application. Creating the pages is the easiest thing as they all come straight from the Forms Xaml Page template (no changing of base page required!!).

image

The image shows me creating the MainPage but I also added a SettingsPage, AboutPage and LandingPage into the Pages folder in my XForms PCL project.

Hooking up the BuildIt.Lifecycle framework was also relatively easy. In the App.cs, I simply replace the in-code page that’s created when you create the new project with the following:

public class App : Application
{
    public App()
    {
        LifecycleHelper.RegisterView<MainPage>().ForState(AppStates.Home);
        LifecycleHelper.RegisterView<SettingsPage>().ForState(AppStates.Settings);
        LifecycleHelper.RegisterView<AboutPage>().ForState(AppStates.About);

        LifecycleHelper.RegisterView<LandingPage>().ForState(AdditionalStates.Landing);

        MainPage = new NavigationPage();
        StartApplication();
    }

    private async void StartApplication()
    {
        var core = new SimpleStatesApplication();
        var wm = new WindowManager(MainPage as NavigationPage, core);
        await core.Startup(builder =>
        {
            builder.RegisterType<FormsDataService>().As<IDataService>();
        });
    }
}

This also requires an implementation of IDataService which I called FormsDataService and returns slightly different Contacts than the UWP version did, just to prove the point.

public class FormsDataService : IDataService
{
    public async Task<IEnumerable<string>> LoadContacts()
    {
        return new[] { "XF: Joe", "XF: Bob", "XF: Frank" };
    }
}

My application will now run and display the MainPage but won’t show any content. To fix this I simply needed to go through each of the pages, translate the UWP XAML into XForms and then wire up any event handlers in the code-behind (which route through to the ViewModel anyhow). The XAML and code behind (where required) for the four pages is at the end of this post…. You’ll note that I cheated on the About page just to get something displaying (I’ll come back to that to implement visual states etc) and that neither the About, nor the Landing page currently have any code behind.

That’s it – I can run the application and my page transitions work without me having to do anything. This is very much in early stages as you’ll see that the back navigation isn’t working properly (it’s actually a forward navigation at the moment) and the navigation back button exists even on the first page of the application, which isn’t correct. In coming posts we’ll clear these issues up.

 

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="
http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleStates.XForms.Pages.MainPage">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition/>
    </Grid.RowDefinitions>
    <StackLayout >
      <Label Text="Main Page"
                  
                 FontSize="40" />
      <Button Clicked="SettingsClick">Settings</Button>
      <Button Clicked="AboutClick">About</Button>
      <Button Clicked="AnotherWindowClick">Another Window</Button>
    </StackLayout>
    <ListView Grid.Row="1" ItemsSource="{Binding Contacts}"></ListView>
  </Grid>
</ContentPage>

public partial class MainPage
{
    public MainPage()
    {
        InitializeComponent();
    }
    private MainViewModel ViewModel => BindingContext as MainViewModel;
    private void SettingsClick(object sender, EventArgs e)
    {
        ViewModel.DisplaySettings();
    }

    private void AboutClick(object sender, EventArgs e)
    {
        ViewModel.DisplayAbout();
    }
    private void AnotherWindowClick(object sender, EventArgs e)
    {
        ViewModel.OpenAnotherWindow();
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="
http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleStates.XForms.Pages.SettingsPage">

  <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
    <Label Text="Settings Page"
               FontSize="40" />
    <Label Text="{Binding SettingsTitle}"
               FontSize="40" />
    <Button Clicked="GoBackClick">Go Back</Button>
  </StackLayout>
</ContentPage>

public partial class SettingsPage
{
    public SettingsPage()
    {
        InitializeComponent();
    }
    private void GoBackClick(object sender, EventArgs e)
    {
        (BindingContext as SettingsViewModel)?.CompleteSettings();
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="
http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleStates.XForms.Pages.AboutPage">
  <StackLayout VerticalOptions="Center" HorizontalOptions="Center" >
    <Label  Text="About Page"
                   FontSize="40" />
    <Label  Text="{Binding AboutTitle}"
             FontSize="40" />
    <Label  Text="{Binding Data}"
             FontSize="40" />
  </StackLayout>
</ContentPage>

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="
http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SimpleStates.XForms.Pages.LandingPage">
  <Label Text="Landing" VerticalOptions="Center" HorizontalOptions="Center" FontSize="40" />
</ContentPage>

Keeping Visual States in Sync with Application States using Adaptive Triggers and BuildIt.Lifecycle

In earlier posts on BuildIt.Lifecycle I’ve covered how changing state via the StateManager can trigger events that will cause changes in visual states on the corresponding page. With the Universal Windows Platform (UWP) we have visual state triggers which can be used to invoke state changes from within the view. For example an AdaptiveTrigger can cause a change in visual state based on the size of the window, which I covered in the post, Using the Universal Windows Platform SplitView Control with Visual States and BuildIt.Lifecycle. These two mechanisms of changing visual states are independent and thus would lead to an inconsistent experience when it comes to maintaining state. For example, if I update the AboutViewModel to include state definitions that modify a Data property, then when the state changes the value of Data will change between “Minimised” and “Expanded”

private string data;
public string Data
{
    get { return data; }
    set
    {
        data = value;
        OnPropertyChanged();
    }
}

public AboutViewModel()
{

    StateManager
        .Group<AboutExpandStates>()
        .DefineState(AboutExpandStates.Minimised)
            .Target(this)
                .Change(x => x.Data, (x, c) => x.Data = c)
                .ToValue("Minimised")
        .DefineState(AboutExpandStates.Expanded)
            .Target(this)
                .Change(x => x.Data, (x, c) => x.Data = c)
                .ToValue("Expanded");
}

If the application was to change state by invoking ChangeState on the StateManager, this would trigger the appropriate change in Visual State on the page. If the user were to resize the page, the AdaptiveTriggers would kick in, causing a change in the Visual State. However, this change wouldn’t be reflected in the StateManager, thus not updating the value of Data (ie would say “Minimised” when the SplitView was expanded, or saying “Expanded” when the SplitView was collapsed). Luckily the VisualStateGroup class in UWP raises a CurrentStateChanged event, which can be intercepted and used to route the update back in the StateManager. This has been added to BuildIt.Lifecycle to ensure consistence between the state of the StateManager and the corresponding visual states. In the same way as the name of each VisualState has to match the enumeration value, the name of the VisualStateGroup has to match the name of the enumeration itself.

Toggling the SplitView using Visual States

Yesterday when I was Toggling the SplitView Pane using a Hamburger Button in a UWP Application, I did it by simply inverting the IsPaneOpen property on the SplitView. This is in conflict with the visual states I had previously declared. An alternative would be for the button to invoke a state change on the AboutViewModel – this relies on knowing what the current state is, as shown in the following code which sits within the AboutViewModel.

public void ToggleSplitView()
{
    var group = StateManager.StateGroups[typeof (AboutExpandStates)] as StateGroup<AboutExpandStates>;
    var current = group.CurrentState;
    var current = StateManager.CurrentState<AboutExpandStates>();
    StateManager.GoToState(current != AboutExpandStates.Expanded
        ? AboutExpandStates.Expanded
        : AboutExpandStates.Minimised);
}

Update: There is now a CurrentState method on the StateManager which eliminates the need to explicitly grab a reference to the state group. See bold lines in code above for the change

Toggling the SplitView Pane using a Hamburger Button in a UWP Application

Yesterday (Using the Universal Windows Platform SplitView Control with Visual States and BuildIt.Lifecycle) I introduced a SplitView into my sample application where I used a couple of buttons in the content. In a lot of applications the SplitView will be triggered by either AdaptiveTriggers used in conjunection with the visual states, or via a hamburger button. Today I’ll add both of these to our application. Firstly a couple of AdaptiveTriggers added to the visual state definitions:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="SplitViewStates">
        <VisualState x:Name="Minimised">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="0" />
            </VisualState.StateTriggers>
        </VisualState>
        <VisualState x:Name="Expanded">
            <VisualState.Setters>
                <Setter Target="RootSplitView.(SplitView.IsPaneOpen)"
                        Value="True" />
                <Setter Target="RootSplitView.(SplitView.DisplayMode)"
                        Value="Inline" />
            </VisualState.Setters>
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowHeight="0" MinWindowWidth="600" />
            </VisualState.StateTriggers>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
As the user adjusts the width of the window the Pane of the SplitView will open and close depending on whether the window is wide enough. Secondly, I need to add a hamburger button to the layout of the page. This is a regualr button control, styled to be a hamburger button – in this case the style for the hamburger button was taken from the UWP samples.

<Button Style="{StaticResource SplitViewTogglePaneButtonStyle}"
        Click="splitViewToggle_Click" />

private void splitViewToggle_Click(object sender, RoutedEventArgs e)
{
    RootSplitView.IsPaneOpen = !RootSplitView.IsPaneOpen;
}


// -------------------------------------- Page Resources ------------------------------

<Page.Resources>
    <ControlTemplate x:Key="SplitViewTogglePaneButtonTemplate"
                        TargetType="Button">
        <Grid x:Name="RootGrid"
                Background="{TemplateBinding Background}">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal" />
                    <VisualState x:Name="PointerOver">
                        <VisualState.Setters>
                            <Setter Target="RootGrid.Background"
                                    Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                            <Setter Target="ContentPresenter.Foreground"
                                    Value="{ThemeResource SystemControlHighlightBaseMediumHighBrush}" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter Target="RootGrid.Background"
                                    Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
                            <Setter Target="ContentPresenter.Foreground"
                                    Value="{ThemeResource SystemControlHighlightBaseMediumBrush}" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Disabled">
                        <VisualState.Setters>
                            <Setter Target="ContentPresenter.Foreground"
                                    Value="{ThemeResource SystemControlForegroundBaseLowBrush}" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ContentPresenter x:Name="ContentPresenter"
                                Padding="{TemplateBinding Padding}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                AutomationProperties.AccessibilityView="Raw"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                ContentTransitions="{TemplateBinding ContentTransitions}" />
        </Grid>
    </ControlTemplate>
    <Style x:Key="SplitViewTogglePaneButtonStyle"
            TargetType="Button">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="Foreground"
                Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
        <Setter Property="BorderBrush"
                Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="HorizontalAlignment"
                Value="Left" />
        <Setter Property="HorizontalContentAlignment"
                Value="Center" />
        <Setter Property="VerticalAlignment"
                Value="Top" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="UseSystemFocusVisuals"
                Value="True" />
        <Setter Property="FontFamily"
                Value="{ThemeResource SymbolThemeFontFamily}" />
        <Setter Property="Content"
                Value="&#xE700;" />
        <Setter Property="Height"
                Value="48" />
        <Setter Property="Width"
                Value="48" />
        <Setter Property="FontWeight"
                Value="Normal" />
        <Setter Property="FontSize"
                Value="20" />
        <Setter Property="Template"
                Value="{StaticResource SplitViewTogglePaneButtonTemplate}" />
    </Style>
</Page.Resources>

Using the Universal Windows Platform SplitView Control with Visual States and BuildIt.Lifecycle

In this post I’m going to add a SplitView to the About page. I’ve also created two visual states that will control whether the Pane of the SplitView is visible or not (ie IsPaneOpen) and whether the display behaviour is for it to overlay or shift the content over (ie Inline). In the main content area I’ve also added two buttons which will be used to switch between each visual state. Lastly, in the SplitView.Pane has a Close button which will be used to return to the previous page in the application.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
       <VisualStateManager.VisualStateGroups>
           <VisualStateGroup x:Name="SplitViewStates">
               <VisualState x:Name="Minimised" />
               <VisualState x:Name="Expanded">
                   <VisualState.Setters>
                       <Setter Target="RootSplitView.(SplitView.IsPaneOpen)"
                               Value="True" />
                       <Setter Target="RootSplitView.(SplitView.DisplayMode)"
                               Value="Inline" />
                   </VisualState.Setters>
               </VisualState>
           </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>

       <SplitView  x:Name="RootSplitView">
           <SplitView.Pane>
               <Grid>
                   <Border Background="Yellow" />
                   <Button Click="CloseClick">Close</Button>
               </Grid>
           </SplitView.Pane>

           <StackPanel VerticalAlignment="Center"
                       HorizontalAlignment="Center">
               <TextBlock Text="About Page"
                          FontSize="40" />
               <TextBlock Text="{Binding AboutTitle}"
                          FontSize="40" />
               <Button Click="ExpandClick">Expand</Button>
               <Button Click="CollapseClick">Collapse</Button>
           </StackPanel>
       </SplitView>
   </Grid>
And here is the code behind for the page:

private void ExpandClick(object sender, RoutedEventArgs e)
{
    (DataContext as AboutViewModel).Expand();
}
private void CollapseClick(object sender, RoutedEventArgs e)
{
    (DataContext as AboutViewModel).Collapse();
}

And then the implementation of the Expand and Collapse methods on the AboutViewModel. For this we’re defining states whose names match the visual states I declared in the XAML. The Expand and Collapse method then switch between these states. I’ve shown the whole class (excluding the AboutTitle property as I’ve changed the base class to include the StateManager and OnCompletion.

public class AboutViewModel : BaseStateManagerViewModelWithCompletion<DefaultCompletion>
{
    public AboutViewModel()
    {
        StateManager.Group<AboutExpandStates>().DefineAllStates();
    }

    public void Close()
    {
        OnComplete(DefaultCompletion.Complete);
    }

    public void Expand()
    {
        StateManager.GoToState(AboutExpandStates.Expanded);
    }

    public void Collapse()
    {
        StateManager.GoToState(AboutExpandStates.Minimised);
    }
}

Lastly, I needed to modify the About state for the PrimaryRegion so that the Completion event would return the application to the Home state:

.StateWithViewModel<AppStates, AboutViewModel>(AppStates.About)
    .OnComplete(DefaultCompletion.Complete).ChangeToPreviousState()

    .WhenChangedToWithData<AppStates, AboutViewModel, string>((vm, d) => vm.AboutTitle = d)
.EndState();

And here is the output with the state manager triggering changes that switch visual states expanding and collapsing the Pane of the SplitView.

image image

Additional Windows using Regions with BuildIt.Lifecycle

In the first post on Getting Started with BuildIt.Lifecycle I touched on the notion of an application region being a logical representation of a Window that’s part of an application. Most Windows Phone, iOS and Android applications will only have a single region but the very nature of Windows lends itself to multi-windowed applications. In this post we’ll extend the sample application to launch a new Window by creating a new region within the application. We’ll start with the declaration of the region:

public enum AdditionalStates
{
    Base,
    Landing
}

public class AdditionalRegion : StateAwareApplicationRegion
{

    public AdditionalRegion()
    {
        StateManager.GroupWithViewModels<AdditionalStates>()
            .StateWithViewModel<AdditionalStates, LandingViewModel>(AdditionalStates.Landing)
                .OnComplete(DefaultCompletion.Complete)
                .CloseRegion(this)
            .EndState();
    }

    protected override async Task CompleteStartup()
    {

        await base.CompleteStartup();

        await StateManager.GoToState(AdditionalStates.Landing, false);
    }
}

You may notice this is slightly different from the PrimaryRegion I defined in my initial post as it inherits from the StateAwareApplicationRegion instead of just ApplicationRegion, and that there’s no StateManager property. The StateAwareApplicationRegion was added as a convenience class, which contains the StateManager property and implements the IHasStates interface. Note that both regions in my sample application now inherit from this class.

There is only a single state defined for the AdditionalRegion, which will correlate to the only page that will get loaded. When this region is started, the state manager transitions to the Landing state, which will display the LandingPage. In order for this to work, we need to create and correlate a LandingPage to the Landing state. I’ve added a page to the application based on the Blank Page template, called LandingPage. In App.xaml.cs I then needed to add the association between the new page and the Landing state:

LifecycleHelper.RegisterView<LandingPage>().ForState(AdditionalStates.Landing);

On the MainPage of the application I’ve added another Button which simply calls a new OpenAnotherWindow method on the MainViewModel.

private void AnotherWindowClick(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    ViewModel.OpenAnotherWindow();
}

The OpenAnotherWindow method simply calls OnComplete with a new enumeration value of AnotherWindow (this enumeration value is independent of the fact that I’ll be using it to open a new window, I could have called it “ShowMeTheMoney” so long as it correlates to the handler I’ll define in the PrimaryRegion).

public void OpenAnotherWindow()
{
    OnComplete(MainCompletion.AnotherWindow);
}

As I just mentioned, I need to define what happens when OnComplete is called. To do this I need to update the state declarations in the PrimaryRegion. Here you can see the additional OnComplete which handles the AnotherWindow enumeration value, and calls LaunchRegion.

StateManager.GroupWithViewModels<AppStates>()
    .StateWithViewModel<AppStates, MainViewModel>(AppStates.Home)
        .OnCompleteWithData(MainCompletion.Settings,vm=>vm.HelloWithTime)
        .ChangeState(AppStates.Settings)

        .OnCompleteWithData< AppStates, MainViewModel,MainCompletion,string>(MainCompletion.About,null)
        .ChangeState(AppStates.About)
                   
        .WhenChangedTo(async vm => await vm.LoadData())
                   
        .OnComplete(MainCompletion.AnotherWindow)
        .LaunchRegion(this,TypeHelper.Ref<AdditionalRegion>())
    .EndState()

Note: The TypeHelper is there to try to eliminate the need to specify all the Type arguments for the LaunchRegion method. I’ll look to refactor this more as we go to make it less obtuse to use.

That’s it, when you run the application, clicking the new Button will spawn of new windows (and actually will keep spawning new windows each time you click the button, even if the previous window is still open).

Refactoring Data Passing Methods in BuildIt.Lifecycle

In my earlier post, Passing Data Between States with BuildIt.Lifecycle, the state declarations for the primary application region started to look really messy. As promised I’ve tidied up the extension helper methods, making it much easier to define delegates that are invoked when changing to and from a state. The updated state declartion now looks like:

StateManager.GroupWithViewModels<AppStates>()
    .StateWithViewModel<AppStates, MainViewModel>(AppStates.Home)
        .OnCompleteWithData(MainCompletion.Settings,vm=>vm.HelloWithTime)
            .ChangeState(AppStates.Settings)
        .OnCompleteWithData< AppStates, MainViewModel,MainCompletion,string>(MainCompletion.About,null)
            .ChangeState(AppStates.About)
        .WhenChangedTo(async vm => await vm.LoadData())
    .EndState()
               
    .StateWithViewModel<AppStates, SettingsViewModel>(AppStates.Settings)
        .OnComplete(DefaultCompletion.Complete).ChangeToPreviousState()
        .WhenChangedToWithData<AppStates, SettingsViewModel, string>((vm, d) => vm.SettingsTitle = d)
    .EndState()
               
    .StateWithViewModel<AppStates, AboutViewModel>(AppStates.About)
        .WhenChangedToWithData<AppStates, AboutViewModel, string>((vm, d) => vm.AboutTitle = d)
    .EndState();

Note that if you’re attempting to use these updates they haven’t yet made their way into the NuGet packages. I’ll endeavour to update these packages later this week. In the meantime I’d recommend downloading the source from the BuildIt repository on GitHub.

Loading Data Using Services with BuildIt.Lifecycle

The BuildIt.Lifecycle automatically creates instances of ViewModels and associates them with the corresponding page/view as the user steps between application states. Behind the scenes this uses Autofac to create these instances and ensure any dependencies are resolved. ViewModels that require access to device hardware, or to services that load or save data, can simply include the interface in the constructor. Take the following example where the MainViewModel relies on an instance of the IDataService interface to populate the Contacts collection.

public interface IDataService
{
    Task<IEnumerable<string>> LoadContacts();
}

public class MainViewModel : BaseViewModelWithCompletion<MainCompletion>
{
    public ObservableCollection<string> Contacts { get; }=new ObservableCollection<string>();
    private IDataService Data { get; }
    public MainViewModel(IDataService data)
    {
        Data = data;
    }
    public async Task LoadData()
    {
        var contactList = await Data.LoadContacts();
        contactList.DoForEach(Contacts.Add);
    }

We need to make two changes to our application declaration. The first is to register the implementation of IDataService, which we’ll do in App.xaml.cs

var core = new SimpleStatesApplication();
var wm = new WindowManager(core);
await core.Startup(builder =>
{
    builder.RegisterType<UWPDataService>().As<IDataService>();
});

public class UWPDataService : IDataService
{
    public async Task<IEnumerable<string>>  LoadContacts()
    {
        return new []{"Joe", "Bob", "Frank"};
    }
}

The other change is to then invoke this method when the MainViewModel is used, this is when the Home state is changed to. We add the following to our state declarations

(group.Item2.States[AppStates.Home] as IViewModelStateDefinition<AppStates, MainViewModel>)
    .WhenChangedTo(async vm => await vm.LoadData());

Note that we can also register services as part of our application class, allowing them to be registered within the PCL, rather than the platform specific libraries. This is useful for services that you want to use the same implementation for all supported platforms.

public class SimpleStatesApplication : RegionAwareBaseApplication<PrimaryRegion>
{
    protected override void RegisterDependencies(ContainerBuilder builder)
    {
        base.RegisterDependencies(builder);
        builder.RegisterType<BasicDebugLogger>().As<ILogService>();
    }
}

The ILogService is used within BuildIt.Lifecycle, so registering this implementatino will allow you to view the diagnostic information in the Output window within Visual Studio.

Passing Data Between States with BuildIt.Lifecycle

Yesterday I discussed in my post, Navigating Back using State History with BuildIt.Lifecycle, how we needed to extend the state manager concept to understand and track state history. The next challenge that app developers face, that doesn’t fit nicely into the current state manager concept is that you often need to pass data between pages of the application. Think of a simple example where you have a list of contacts on one page; the user taps on one of those contacts and is taken to a new page that shows the details of that person. What you need to pass between pages is information about which contact the user tapped. I’ll discuss later how we can pass data between states, and thus view models, later in the post, but first I wanted to dig into the concept of passing data between pages a bit further.

When the user tapped on the contact in the list, there is a lot of information that we can record about that event:

- Mouse/Touch position (don’t forget we’re not just dealing with mobile devices, apps are a big part of the desktop experience on Mac and PC now)

- List index (the index of the item tapped in the list)

- List item (the object in the list that corresponds to the cell that was tapped)

- Item Id (some sort of unique identifier of the item that was tapped)

With all these pieces of information, the question is which one to pass to the details page. Let’s review them individually:

- Mouse/Touch position – this in itself isn’t very useful as it only has meaning on the page that’s currently in view and that point in time. For example, if the user were to scroll the list, all of a sudden this information is useless as it now points to a different cell in the list

- List index – this is only useful if the details page has a reference to exactly the same list that is used to populate the contacts list page. If the underlying data set changed (eg sync with backend caused new contacts to be downloaded), then the list index might now point to a different item

- List item – if the items in the list contain all the details necessary to populate the details page then passing the list item to the details page is ok. However, if you have a large list off contacts, chances are you only load enough data about each contact to display the item in the list (eg perhaps only photo and name). In this case, passing the whole entity isn’t that useful since the details page has to load the whole contact anyhow. The other point worth considering is about serialization of navigation parameters – depending on the platform there are mechanisms for persisting navigation stack, including data parameters; some of these have size and complexity constraints on the data passed between pages.

- Item Id – this is probably the best choice for this scenario. It contains enough information for the contact to be loaded. If performance is a concern (ie you want the contact summary information to be instantly available when the user arrives at the details page, even if details have to be progressively loaded), then you should cache the contact summary information in memory in a service which can be accessed as soon as the details page is loaded. The cache service might also cache the last X number of loaded contacts in memory to make it quick for a user to go between contacts.

Ok, so this works for this scenario but how do we summarize this into a more generic form. Essentially it comes down to passing the minimal set of information between pages that is required to completely load the next page. Ideally pages (and their corresponding ViewModel) should be self sufficient, relying only on application services to load data. Application services, most likely passed into the ViewModel using some sort of dependency injection, provide an abstraction over loading/saving data both locally (in memory and to disk) and across the network (calling services or performing data sync). To this end, by supplying the minimum set of information (eg item id in the case of the contacts list scenario), the ViewModel is only reliant on that piece of information. This reduces the necessary testing surface area as there are few permutations of input data that need to be teted.

Now that we have an understanding of how to determine what data to pass between pages, we again need to come back to thinking in terms of states and passing data during a transition between states. If you recall, our ViewModels aren’t aware of states or the ability to change between them. All they’re able to do is indicate that they’re complete. In the following code, the MainViewModel exposes a public HelloWithTime property as well as a private AboutWithTime property, the former will be passed into the Settings state, the latter into the About state. You’ll observe that the OnComplete call for settings hasn’t been changed, whilst there is now a call to OnCompleteWithData, passing in the AboutWithTime property value, for completing with the About completion value.

public class MainViewModel : BaseViewModelWithCompletion<MainCompletion>
{
    public string HelloWithTime => $"Hello World at {DateTime.Now.ToString("h:mm tt")}";
    private string AboutWithTime => $"About information {DateTime.Now.ToString("h:mm tt")}";

    public void DisplaySettings()
    {
        OnComplete(MainCompletion.Settings);
    }
    public void DisplayAbout()
    {
        OnCompleteWithData(MainCompletion.About,AboutWithTime);
    }
}

Our state declaration now looks like the following:

var group = StateManager.GroupWithViewModels<AppStates>()
    .StateWithViewModel<AppStates, MainViewModel>(AppStates.Home)
        .OnCompleteWithData(MainCompletion.Settings,vm=>vm.HelloWithTime)
            .ChangeState(AppStates.Settings)
        .OnCompleteWithData< AppStates, MainViewModel,MainCompletion,string>(MainCompletion.About,null)
            .ChangeState(AppStates.About)
        .EndState()
    .StateWithViewModel<AppStates, SettingsViewModel>(AppStates.Settings)
        .OnComplete(DefaultCompletion.Complete).ChangeToPreviousState()
        .EndState()
    .StateWithViewModel<AppStates, AboutViewModel>(AppStates.About)
        .EndState();

(group.Item2.States[AppStates.Settings] as IViewModelStateDefinition<AppStates, SettingsViewModel>)
    .WhenChangedToWithData<AppStates, SettingsViewModel, string>((vm, d) => vm.SettingsTitle = d);

(group.Item2.States[AppStates.About] as IViewModelStateDefinition<AppStates, AboutViewModel>)
    .WhenChangedToWithData<AppStates, AboutViewModel, string>((vm, d) => vm.AboutTitle = d);

What’s interesting here is that there are two parts to passing data between states. The first is how the data is exposed out of the state that’s being completed. Here I’ve shown two ways – completion with Settings uses the public HelloWithTime property to retrieve the data to pass to the next state; completion with About can’t access the AboutWithTime property, since it’s private, which is why it relies on the MainViewModel to pass out the AboutWithTime value when it invokes OnCompleteWithData. Note that currently in the state declaration you still need to call OnCompleteWithData passing in a null value for the data accessor, otherwise the data value isn’t passed – this will be fixed shortly.

The second part of passing data between states is how the data is passed into the new state. This is done by calling the WhenChangedToWithData and specifying a function that will be invoked with the corresponding data. Note this method is a generic method and you can actually call it multiple times with different data types, which means that you can handle arriving at this state with different data (perhaps from different states). One thing to be aware of is that behind the scenes the data is being serialized to json, so whatever data you pass between states need to be serializable to json. Ideally the data being passed between states should be kept to a minimum.