In the Silverlight ApplicationProgramWhen interacting with customers, you do not need to write back-end information.CodeWith the XAML code, we will learn about trigger triggers in this article.
Trigger trigger: trigger factors, such as mouse clicks, keyboard input, double-click, enter, and scroll. These are the conditions for triggering action interaction.
There are two types of triggers:
1. eventtrigger and propertytrigger are defined by the system.
Ii. User-defined trigger. For example, if there is no double-click event in sl4, we can create a doubleclicktrriger, the timer detects that mouse-triggered actions are triggered when the time interval between clicks in the same part of the page is less than 300 milliseconds.
EventtriggerIt mainly specifies the name of the event to be triggered. The following example triggers the changepropertyaction action when mouseleftbuttonup. This example is not described in detail:
<Rectangle width ="300">
<I: interaction. triggers>
<I: eventtrigger eventname ="Mouseleftbuttonup">
<EI: changepropertyaction/>
</I: eventtrigger>
</I: interaction. triggers>
</Rectangle>
Custom trigger: In this example, We customize a flip trigger. by pressing a button on the specified object, move the mouse to the left or right, and then open the mouse, the system automatically checks whether to flip the page to the left or to the right. Like behavior, a Custom trigger only needs to override the onattached and ondetaching methods. A Custom trigger must inherit from the triggerbase <t> class.
The Custom trigger code is as follows:
Public Class Paggertrigger: triggerbase <uielement>
{
Private Point _ downposition;
Protected Override Void Onattached ()
{
Base . Onattached ();
// Load events
Associatedobject. mouseleftbuttondown + = New Mousebuttoneventhandler (associatedobject_mouseleftbuttondown );
Associatedobject. mouseleftbuttonup + = New Mousebuttoneventhandler (associatedobject_mouseleftbuttonup );
}
Void Associatedobject_mouseleftbuttondown ( Object Sender, mousebuttoneventargs E)
{
Uielement element = sender As Uielement;
_ Downposition = E. getposition (element );
}
Void Associatedobject_mouseleftbuttonup (Object Sender, mousebuttoneventargs E)
{
Uielement element = sender As Uielement;
Point Position = E. getposition (element );
Double X_content = position. X-_ downposition. X;
Pageenum = pageenum. pageleft;
If (Math. Abs (x_content)> 10 )
{
If (X_content> 0 )
{
Pageenum = pageenum. pageright;
}
Else
{
Pageenum = pageenum. pageleft;
}
Invokeactions (pageenum );
}
}
Protected Override Void Ondetaching ()
{
Base . Ondetaching ();
// Uninstall event
Associatedobject. mouseleftbuttondown-= New Mousebuttoneventhandler (associatedobject_mouseleftbuttondown );
Associatedobject. mouseleftbuttonup-= New Mousebuttoneventhandler (associatedobject_mouseleftbuttonup );
}
}
/// <Summary>
/// Page turning Enumeration
/// </Summary>
Public Enum Pageenum
{
/// <Summary>
/// Flip left
/// </Summary>
Pageleft,
/// <Summary>
/// Right flip
/// </Summary>
Pageright
}
The Custom Action Code is as follows:
// Action
Public Class Invokeaction: targetedtriggeraction <uielement>
{
Protected Override Void Invoke ( Object Parameter)
{
If (Toinvoke! = Null )
{
Toinvoke (parameter, New Routedeventargs (){});
}
}
Public Delegate Void Handler ( Object Sender, routedeventargs E );
Public Event Handler toinvoke;
}
The XAML code for calling on the home page is as follows:
<Usercontrol X: class = " Sltrigger. mainpage "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "
Xmlns: D = " Http://schemas.microsoft.com/expression/blend/2008 "
Xmlns: MC = " Http://schemas.openxmlformats.org/markup-compatibility/2006 "
Xmlns: I = " CLR-namespace: system. Windows. interactivity; Assembly = system. Windows. Interactivity "
Xmlns: Ei = " CLR-namespace: Microsoft. expression. interactivity. Core; Assembly = Microsoft. expression. Interactions "
Xmlns: Me = " CLR-namespace: sltrigger "
MC: ignorable = " D "
D: designheight =" 300 " D: designwidth = " 400 " >
<Grid X: Name = " Layoutroot " Background = " White " >
<Image width = " 300 " Source = " /Sltrigger; component/chun.jpg " Margin = " 50,127, 50,-16 " >
<I: interaction. triggers>
<Me: paggertrigger>
<Me: invokeaction toinvoke = " Pageclickhandler " />
</Me: paggertrigger>
</I: interaction. triggers>
</Image>
</GRID>
</Usercontrol>
The code in XAML. CS is as follows:
Public Partial Class Mainpage: usercontrol
{
Public Mainpage ()
{
Initializecomponent ();
}
// Event handler for the specified action
Private Void Pageclickhandler ( Object Sender, routedeventargs E)
{
Pageenum = (pageenum) sender;
String Info = String . Empty;
If (Pageenum = pageenum. pageleft)
{
Info = " Flip to the left " ;
}
Else If (Pageenum = pageenum. pageright)
{
Info = " Flip to the right " ;
}
MessageBox. Show (Info );
}
}
To download the source code, click sltrigger.zip to reference system. Windows. interactivity. dll.