Windows 10 Universal Apps (UAD) - Adaptive Triggers Creating UI For Multiple Screens


The VisualStateManager has a collection of VisualStateGroups, each one being a set of VisualStates that may apply in a given situation. Each of the groups is independent in the sense that one VisualState from each group might be active at any one time. For simplicity, let's just deal with one VisualStateGroup from which only one VisualState is active.
In this case, the basic XAML is.
  1. <VisualStateManager.VisualStateGroups>    
  2. <VisualStateGroup>    
List of visual states.
  1. </VisualStateGroup>    
  2. </VisualStateManager.VisualStateGroups>     
This is how the VisualStateManager worked before Windows 10, but you could only activate a VisualState in code and the changes to the UI were specified as animations. With Windows 10 you can now activate a state using an AdaptiveTrigger object using just XAML and no code. In addition, you no longer need to pretend that your UI is changing via an animation. Now you can use a Setter to change the value of any property on any object.
Each of the VisualState objects can have a list of StateTriggers and a list of Setters and of course, the order doesn't matter but it is slightly more logical to start with the StateTriggers.
The StateTriggers set conditions for the VisualState to be active and the Setters can be used to set properties on any XAML object when the VisualState is activated.
An AdaptiveTrigger takes the following simple form-
  1. <AdaptiveTrigger property=value />  
The property is set to the value that you specify and this is usually used in a comparison with some other value to determine when the trigger fires.
At the moment the only triggers are MinWindowWidth and MinWindowHeight but it is possible to add custom triggers. These conditions test the current width and height of the window and trigger if the window is larger than the specified value.
  1. <AdaptiveTrigger MinWindowsWidth=400 />     
Is the active window's width, 400 effective pixels or greater.
Notice that what happens is that you set the value of MinWindowsWidth to 400 and this is compared to the actual width of the window whenever it changes. The trigger fires if the actual window width is greater than or equal to MinWindowsWidth.
A Setter takes the form.
  1. <Setter Target="fully qualified property name"    
  2. Value="value" />    
  3. So for example:    
  4. <Setter Target="Button1.Width"    
  5. Value="200" />     
Sets the Width of Button1 to 200 pixels if the state is active.
You can put multiple conditions and multiple setters into a single state.
  1. <VisualState x:Name="Big">    
  2.     <VisualState.StateTriggers>    
  3.         <AdaptiveTrigger MinWindowWidth="600"/>    
  4.     </VisualState.StateTriggers>    
  5.     <VisualState.Setters>    
  6.         <Setter Target="Button1.Content"    
  7. Value="Wide"/>    
  8.     </VisualState.Setters>    
  9. </VisualState>     
In this case, the trigger is if the window is 600 pixels or wider.
If the trigger fires then the Button's Content is set to "Wide" what is it set to if the trigger isn't active?
The simple answer is that whatever value the property had before is restored when the trigger isn't active. For example, if the Button's original definition was-
  1. <Button Name="Button1" Content="Narrow" />     
The label on the Button would change to "Wide" as the window was resized to larger than 600 pixels and to "Narrow" when it became smaller than 599 pixels. That is, when none of the VisualStates is active, the system restores the UI to what it was originally specified in the XAML.
If you would rather have the default state included as a part of the VisualStateManager you can include it as-
  1. <VisualState x:Name="Small">    
  2.     <VisualState.StateTriggers>    
  3.         <AdaptiveTrigger MinWindowWidth="0" />    
  4.     </VisualState.StateTriggers>    
  5.     <VisualState.Setters>    
  6.         <Setter Target="Button1.Content"    
  7. Value="Narrow" />    
  8.     </VisualState.Setters>    
  9. </VisualState>     
Putting the entire thing together gives the following-
  1. <Page    
  2. x:Class="App4.MainPage"    
  3.     xmlns="    
  4. winfx/2006/xaml/presentation"    
  5.     xmlns:x="    
  6. winfx/2006/xaml"    
  7.     xmlns:local="using:App4"    
  8.     xmlns:d="    
  9. expression/blend/2008"    
  10.     xmlns:mc="    
  11. markup-compatibility/2006"    
  12. mc:Ignorable="d">    
  13.     <Grid Background="{ThemeResource    
  14. ApplicationPageBackgroundThemeBrush}">    
  15.         <VisualStateManager.VisualStateGroups>    
  16.             <VisualStateGroup>    
  17.                 <VisualState x:Name="Small">    
  18.                     <VisualState.StateTriggers>    
  19.                         <AdaptiveTrigger MinWindowWidth="0" />    
  20.                     </VisualState.StateTriggers>    
  21.                     <VisualState.Setters>    
  22.                         <Setter Target="Button1.Content"    
  23. Value="Narrow" />    
  24.                     </VisualState.Setters>    
  25.                 </VisualState>    
  26.                 <VisualState x:Name="Big">    
  27.                     <VisualState.StateTriggers>    
  28.                         <AdaptiveTrigger MinWindowWidth="600" />    
  29.                     </VisualState.StateTriggers>    
  30.                     <VisualState.Setters>    
  31.                         <Setter Target="Button1.Content"    
  32. Value="Wide" />    
  33.                     </VisualState.Setters>    
  34.                 </VisualState>    
  35.             </VisualStateGroup>    
  36.         </VisualStateManager.VisualStateGroups>    
  37.         <Button x:Name="Button1"    
  38. HorizontalAlignment="Left"    
  39. VerticalAlignment="Top"    
  40. Margin="196,172,0,0"/>    
  41.     </Grid>    
  42. </Page>     
If you run this application and resize the width of the window you will see the label on the Button change as you cross the 600-pixel width limit. 
Of course, in a real application, it is unlikely that you would be setting the content property of the controls. What you would be doing is setting layout properties to cope with the changing size of the window. This isn't an easy thing to do if the changes to the layout are large. It is much more suited to make small tweaks to essentially the same layout.


In this article, we learned about Windows 10 Universal Apps (UAD) - Adaptive Triggers Creating UI For Multiple Screens.