Customize the trigger and associate it with the action

Source: Internet
Author: User

I have written an article about custom behaviors, through which we can quickly implement various behaviors and make them easy to use in blend. Of course, in combination with blend development, we also know that there are several other important attributes in XAML. In fact, in the previous article, we also saw them, namely, triggers and actions, they are also located in system. windows. interactivity and Microsoft. expression. under the interactions namespace. Next, let's take a look at how to use them in blend and customize the trigger and associate it with the corresponding action.

1. triggers and actions

As developers, these attributes may not be used much and are not used to them. I am also the same. They are rarely used in development. There are many reasons for this, one of the most important points is that for a long time, everyone has been accustomed to coding directly under Visual Studio, a powerful ide. Things such as triggers and actions can be fully implemented using events, method. Here, I just want to extend the idea: triggers and actions can also be used in this way. Besides, since Microsoft has provided us with the blend tool, we also have to adapt to the new thinking of integrating it with vs collaborative development. One of the major advantages is to greatly improve the development efficiency. However, everything has two sides. I am not here to make sure that you want to do so, it is entirely dependent on your actual work needs. Now, let's take a look at the basic concepts of triggers and actions:

  • Triggers (trigger): Like its name, it can trigger a series of actions at a specific time. In blend, eventtrigger, timertrigger, storyboardcompletedtrigger, keytrigger, and so on exist.

  • Actions (Action): It is a bit similar to the previous behavior. In blend, you can also see that they are actually classified as actions. As for the concept, there is nothing to explain, and its name is already very vivid. Changepropertyaction, controlstoryboardaction, playsoundaction, and other actions exist by default.

In general, triggers and actions are used together. For example, we use propertychangeaction in an eventtrigger (this action is used to change the attribute of an element ), when a specific event is triggered, this action is executed to change the attributes of the element. Of course, in triggers, you can define multiple actions to respond to different actions.

Note: actions and triggers are not suitable for use at any time. The official documents include the following:

Silverlight supports the use of eventtrigger andObject. Triggers attribute element, but we recommend that you do not use this method for the current application. Eventtrigger may only support one trigger operation for the loaded event (you can name this trigger event in the eventtrigger. routedevent attribute ). This makes eventtrigger technology not suitable for most runtime interactions. In this case, the eventtrigger and triggers attributes are not used, but one of the following two alternatives is considered:

  • To perform animation processing on properties of an object that does not belong to a control, place the dashboard in the regular resources of a page or application. Then, assign an event handler to the element where "triggers" is placed. When responding to related events, the event handler should retrieve the demo board from the resource dictionary. Then, you call begin for the retrieved storyboard. Another advantage of this technology is that the same demo board can be applied to the interaction of multiple users.

  • To perform animation processing (derived from control) on the properties of the control object, use the visualstatemanager technology and call gotostate to run an appropriate Animation Based on the control state or input information.


2. Example of Using Trigger and action

Open blend and create a Silverlight Project

Drag a rectangle on the canvas.

Next, we open the behavior panel under the assets (asset) item, drag a changepropertyaction from it to the top of the rectangle

Let's see what code is generated:

Select changepropertyaction to view its attributes.

Next, we will continue to modify the attributes to increase the length of the rectangle to 200 when an event is triggered, and the buffer animation effect of easy out will be displayed in four seconds:

Then, run the project and you will see the result. It is really easy. Next, we will implement a Custom trigger to associate it with an action.

3. Custom triggers

Here, we want to implement a mykeydown trigger. Right-click the project name and add a new project:

After confirmation, the newly created class file is automatically opened. We find that it inherits from. windows. the triggerbase <t> class in the interactivity space. Similar to the custom behavior, it also has two overloaded methods.OnattachedAndOndetachingIn the former, we mainly Insert the code to be run when the trigger is appended to an object. The latter mainly inserts the code to be run when the trigger is deleted from the object. Next, we will add the following code in them:

In the above Code, the most important thing isTriggerTriggered. You must callInvokeactionsMethod. CallInvokeactionsWill be notifiedTriggerAny associatedActionsRun. Next, generate the following project. We will continue to modify the changes in the previous project, and then open the [changepropertyaction] attribute panel. We will associate the Custom trigger:

After confirming, we can find a keys attribute on the property Panel, where we write AB

In this way, a Custom trigger is successfully associated with an action, which is different from a general keydown event. Because we filter the key in it, now, action is triggered only when you press the or B key. Other keys are invalid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.