Silverlight实用窍门系列:62.Silverlight中的Action动作TargetedTriggerAction、TriggerAction

    在Silverlight中的Action是指动作的意思,在Trigger触发器触发的时候,执行Action动作。在我们自定义Action的时候可以定义两种Action分别是继承于TargetedTriggerAction<T>和TriggerAction<T>的。

    TriggerAction<T>:继承于此的自定义Action动作的操作对象是关联采用此Action的对象。

    TargetedTriggerAction<T>:继承于此的自定义Action动作的操作对象可以是其他对象,由TagetName指定的对象。

           本文在前一篇文章的实例基础上增加了一个自定义的Action。继承于TriggerAction和TargetedTriggerAction都可以重写以下三个方法:OnAttached()、OnDetaching()、Invoke(object parameter),其中TargetedTriggerAction还需要另外多重写一个OnTargetChanged(UIElement oldTarget, UIElement newTarget)方法,以便在设置了TargetName属性的时候能够将操作对象设置为指定的其他对象。

           继承于TriggerAction<T>的Action代码如下:

    //动作
public class InvokeAction : TriggerAction <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;

/// <summary>
/// 装载对象
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
}

/// <summary>
/// 卸载对象
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
}

}

        继承于TargetedTriggerAction<T>的Action代码如下:

    public class InvokeTargetAction : TargetedTriggerAction<UIElement>
{
private UIElement element;

/// <summary>
/// 装载对象
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
if (Target != null)
{
element = Target;
}
}
/// <summary>
/// 卸载对象
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
element = null;
}

/// <summary>
/// 当此Action的TargetName属性赋值为某个按钮名称时出发此事件
/// </summary>
/// <param name="oldTarget"></param>
/// <param name="newTarget"></param>
protected override void OnTargetChanged(UIElement oldTarget, UIElement newTarget)
{
base.OnTargetChanged(oldTarget, newTarget);
if (element == null)
{
element = newTarget;
}
}

/// <summary>
/// 当触发器触发时运行此函数
/// </summary>
/// <param name="parameter"></param>
protected override void Invoke(object parameter)
{
ButtonBase btn = element as ButtonBase;
AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(btn); ;
IInvokeProvider invokeProvider = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProvider.Invoke();
}

}

    MainPage.xaml代码如下,使用两种Action:

<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:me="clr-namespace:SLTrigger"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="800">

<Grid x:Name="LayoutRoot" Background="White">
<Image Width="300" Source="/SLTrigger;component/chun.jpg" Margin="20,43,480,68">
<i:Interaction.Triggers>
<me:PaggerTrigger>
<me:InvokeAction ToInvoke="PageClickHandler" />
</me:PaggerTrigger>
</i:Interaction.Triggers>
</Image>
<Image Margin="395,43,105,68" Source="/SLTrigger;component/chun.jpg" Width="300">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<me:InvokeTargetAction TargetName="btnShow" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
<Button Content="Button" Height="23"
Name="btnShow" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>

    MainPage.xaml.cs代码如下:

    public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
//实现指定动作时出发的事件处理程序
private void PageClickHandler(object sender, RoutedEventArgs e)
{
PageEnum pageEnum = (PageEnum)sender;
string info = string.Empty;
if (pageEnum == PageEnum.PageLeft)
{
info = "向左翻页";
}
else if (pageEnum == PageEnum.PageRight)
{
info = "向右翻页";
}
MessageBox.Show(info);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("TargetedTriggerAction行为触发的事件");
}
}

    当然还有一个自定义的Trigger代码没有贴出来,您可以点击 SLAction.zip 下载源文件,效果图如下:

posted @ 2012-04-09 10:36  .NET架构  阅读(2586)  评论(3编辑  收藏  举报