Windows 10 Development: Custom StateTriggers

By October 1, 2015Windows
Windows-10-Custom-Trigger-Falafel2Go-Complete-Responsive-Layout

We’ve seen how the Windows 10 AdaptiveTrigger can help you create a dynamic responsive UI based on the height or width of the device or screen. However, this trigger is limited to a specific value for each dimension, and offers no way to respond to changes in the relationship between them, such as to determine the orientation or aspect ratio of the app.

Fortunately, you are not limited to only the AdaptiveTrigger in Windows 10; you can create your own custom statetriggers based on virtually any property, even those not at all related to the UI. In this post, we’ll look at how we combined both the width and orientation properties of the app to create a custom OrientationSize trigger to switch between visual states using these properties.

StateTriggerBase

Custom triggers in Windows 10 inherit from StateTriggerBase which exposes the key method SetActive(). This method accepts a boolean parameter indicating whether or not the associated VisualState should be activated.

The custom trigger simply needs to call this method, passing in whatever value is appropriate based on any condition or custom code to which the app has access, including window size, network connectivity, time of day, etc.

In our case, we are concerned with both the Orientation and the Window size, the cross product of which results in six possible options. I’ve encapsulated these values into a custom enumeration:

The goal is to create a trigger that can identify if it is in one of these states so that the layout can adjust accordingly.

We begin then by inheriting from the required base class, with a property that represents the desired orientation enumeration value for the trigger:

Now, the trigger we create obviously needs to be able to respond to changes in the application size. This can be done by listening to the SizeChanged event of the current window, wired up in the constructor:

Any change in the window size or orientation will launch a custom method which I’ve called SetTrigger to compare the current state of the window size to the state required by the defined trigger instance properties:

Here I’ve used the three threshold values of 0, 720, and 1280 to define the boundaries of the different sizes, combined with the existing Orientation property of the ApplicationView state to determine the overall state of the application. If it is a match for the trigger’s expected property, we set the associated VisualState to be active.

Although we have defined six possible states, as we saw in our last post, we only need three for the different states of Falafel2Go for small, medium, and wide views. However, as we also saw the views aren’t defined strictly by the width or height; the screen could be small but also landscape, in which case we do not want to use a vertical layout.

Instead, we can simply combine the states, mixing and matching their arrangements to properly enable the states that layout the app properly given the resulting state.

Here’s the XAML that does exactly that:

Notice here that we combine the the layouts that make sense (such as Landscape and NarrowLandscape) while separating out the unique WideLandscape to accommodate the unique, full-screen desktop views, like the tiled view for blogs:

Windows-10-AdaptiveTriggers-Falafel2Go-Wide

You might be wondering why there isn’t an entry for the regular Portrait enumeration; the reason this is missing is that this is the “default” state of the XAML page, so if none of the triggers are satisfied, it will fall back to that state, completing all of the possible required views for the app.

Firing Triggers on Startup with Dependency Properties

There’s still one last missing piece of the puzzle here, which you will discover if you exit the app while not in the default (“Portrait”) state. Exiting a Windows 10 app preserves the window size and orientation for the next launch. As you recall, the default state for our app is Portrait, and we only update our custom triggers when the window is resized.

This means that if we exit our app in a different VisualState, then relaunch it, it will attempt to restore the default “Portrait” state, even though that state is not correct for the launched orientation, resulting in an unexpected layout:

Windows-10-AdaptiveTriggers-Falafel2Go-Narrow-Landscape

You might think, as I sure did, that the fix is to simply force the trigger to evaluate in the constructor:

Unfortunately this does not appear to be the correct fix, as in my testing, the Orientation property appeared to be fixed to the default, first value of the enumeration (in this case “NarrowPortrait”) for all of the triggers, disregarding what we set in the XAML properties.

The reason for this is that we need to instead configure our trigger to rely on Dependency Properties which, as previously discussed, contain all the plumbing and infrastructure to manage binding at runtime.

Replacing our OrientationSize with an implentation using Dependecy Properties, and using the changed event to call the trigger yields the following complete definition for our custom trigger:

 

Note that I’ve pre-fixed the SetTrigger method with the design detection feature of MvvmLight to avoid throwing any errors in the designer.

With that, we’ve completed the custom trigger that allows the fully dynamic, responsive layout of Falafel2Go (click to open and animate the GIF):

Windows-10-Custom-Trigger-Falafel2Go-Complete-Responsive-Layout

We applied the same triggers in different combinations throughout Falafel2Go to achieve the different layouts required, automatically triggered by any change in device or screen orientation.

Wrapping Up and Next Steps

Custom triggers allow your Windows 10 applications to respond to a wide variety of conditions to create dynamic, responsive layouts. Whether it’s simply reflowing the UI to accommodate different sizes and orientations, or responding to changes in network connectivity, these triggers help your apps feel more alive, and more importantly, in-tune to your user’s expectations and experiences.

With this post we’ve completed a trip through the basics of app development, from setting up an MVVM framework, to managing state and lifecycle, to the different UI controls and strategies for adapting your layout, and is everything you need to build a simple app for Windows 10.

In our next round of posts, we’ll take a look at more advanced topics, really diving into the platform-specific features of Windows 10, including Live Tiles, background tasks, local storage, and even cloud services like push notifications and mobile apps.

If there’s any topic you’d like to see be sure and sound off in the comments, and of course, we at Falafel would be glad to work with you on your next Windows 10 project! Take a look at our consulting and training packages today and let us help you take your Windows apps to the next level.

As always, I hope this first round of posts has been helpful, stay tuned for more and thanks for reading!

The following two tabs change content below.
  • Kartheek Javvaji

    When is the next part going to be posted….?

    • I’m putting together topics and notes, i hope to have continue this series in mid december and into 2016. Thank you for reading!

      • Kartheek Javvaji

        ohh…..Thank you…This series is one of the best sources for learning UWA.

  • Nicolas Krier

    Hey Josh ! Nice post! And by the way, nice video (the one with leap motion) 😀