|Windows 10 Universal Apps - Adaptive Triggers|
|Written by Mike James|
Page 2 of 2
You can create your own triggers by creating a custom class inheriting from StateTriggerBase. All you have to do is set up a dependency property that the trigger is going to use as its condition and call SetTriggerValue with true or false depending on whether or not the condition is satisfied.
To see this in action lets us create a new AdaptiveTrigger which corresponds to the XAML tag:
The idea is that we need to create a Class called MyTrigger with a dependency property corresponding to myCondition. When the myCondition property changes you can compare its value to any other value you care to use - the day of the month or the model number of the device etc. If your condition is satisfied i.e. it is the tenth day of the month then you call SetTriggerValue(true) and SetTriggerValue(false) otherwise.
Notice that this scheme only checks the trigger when myCondition is changed not when the day of the month changes. This means that the trigger will usually only be checked once when the application starts. If you want to check for changes in another value such as day of the month you need to hook into its changed event.
If you are puzzled by what is going on in creating the MyTrigger class see - How XAML works - Creating Objects With XAML.
All we need is to create a public MyTrigger class that registered a suitable dependency property that is set as the "condition" in the trigger tag. We need to use a dependency property because we need an onChanged event and handler and this is part of the standard behavior of a dependency property.
If you need to find out about dependency properties see: Inside Dependency Properties.
For simplicity add the MyTrigger Class to the project using Add,New Item. The class has to inherit from StateTriggerBase :
and to make this work we also need to add:
We next need to set up a dependency property associated with the CLR property myCondition:public int myCondition:
As is always the case the dependency property has the same name but with Property tagged onto the end.
Next we need to register the dependency property and in this case we need an OnChanged event handler.
That is more or less all we have to do, but to give the new Trigger some sort of behavior we need the OnChanged handler to do something. For simplicity, let's just activate the trigger if the value set is greater than 50:
The dependency object that the event occured on is passed in as d and we just cast it to an instance of MyTrigger and then simply call SetTriggerValue with true or false.
Now we are ready to test our custom trigger. Switch to the XAML and add:
so that we can use the classes defined in the application.
After adding this to the Page tag the rest of the XML is:
If you run the program with a value of MyCondition of 50 or less you will see the button's label as "Not Triggered" and for values greater than 50 it will say "Triggered".
Yes, it really is this simple.
The complete class is:
This particular Trigger is really only suitable for testing if the device has a set characteristic like a wide screen, which isn't going to change after the program has started running. For characteristics like orientation that can change while the program is running, you need to use the OnChanged event for the quality - orientation - being monitored.
Where Next - Views
Although the new ViewStateManager facilities are very nice they aren't really powerful enough to deal with big changes to layout. You could use a trigger with, say, the new relative layout to position things, but this isn't easy.
When it comes to big changes you really need to design a layout for each type of screen and this is where the new Views facility comes into the picture - see the next article for more details.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: email@example.com
|Last Updated ( Thursday, 09 April 2015 )|