触发器和动作是协同工作的。当某件事情发生的时候,触发器就会引发并调用一个动作,触发器和动作组成了最简单的行为表现形式。
创建一个动作:
想象一下,当用户完成一个操作(例如单击一个按钮),你希望能播放声音提示。如果不用行为的方式,是比较容易实现的。添加一个MediaElement到页面,提供音频文件的URI,然后在适当的时候调用MediaElement.Play()方法。但是,这些细节添加了不必要的混乱。如果希望播放一系列不同的声音提示响应不同的事件,那么就需要管理大量的代码。
动作关联到元素:
要使用一个动作,需要触发器的帮助。触发器关联到元素,动作关联到触发器。使用PlaySoundAction的第一步就是要选择合适的触发器。所有的触发器派生自TriggerBase类。System.Windows.Interactivity.dll程序集包含一个名为EventTrigger的触发器,当一个特殊的事件发生的时候就会引发触发器。
创建一个动作:
想象一下,当用户完成一个操作(例如单击一个按钮),你希望能播放声音提示。如果不用行为的方式,是比较容易实现的。添加一个MediaElement到页面,提供音频文件的URI,然后在适当的时候调用MediaElement.Play()方法。但是,这些细节添加了不必要的混乱。如果希望播放一系列不同的声音提示响应不同的事件,那么就需要管理大量的代码。
可以用一个动作来管理声音提示的播放就避免了这个问题。首先,创一个Silverlight类库程序集,然后添加System.Windows.Interactivity.dll程序集的引用,最后创建一个 Action类派生自TriggerAction类。Silverlight中的媒体支持有一个限制,如果想要通过MediaElement进行播放,即使只是打算播放普通的音频文件且没有可视化的呈现,必须把它放置于元素的层次结构,即页面的可视树(Visual Tree)中。
public class PlaySoundAction : TriggerAction<FrameworkElement>
{
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(Uri), typeof(PlaySoundAction), new PropertyMetadata(null));
public Uri Source
{
get
{
return (Uri)GetValue(PlaySoundAction.SourceProperty);
}
set
{
SetValue(PlaySoundAction.SourceProperty, value);
}
}
protected override void Invoke(object parameter)
{
//查找一个位置以便添加MediaElement
Panel container = FindContainer();
if (container != null)
{
//创建并配置MediaElement
MediaElement media = new MediaElement();
media.Source = this.Source;
//播放完成时清除MediaElement
media.MediaEnded += (s, e) => container.Children.Remove(media);
media.MediaFailed += (s, e) => container.Children.Remove(media);
//添加MediaElement开始播放
media.AutoPlay = true;
container.Children.Add(media);
}
}
private Panel FindContainer()
{
FrameworkElement element = this.AssociatedObject;
//向上寻找可以添加MediaElement的布局容器
while (element != null)
{
if (element is Panel)
{
return (Panel)element;
}
element = VisualTreeHelper.GetParent(element) as FrameworkElement;
}
return null;
}
<UserControl x:Class="SilverlightApplication1.Behaviors.ActionSample"
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:custom="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="点击播放语言提示" Width="130" Height="50"
HorizontalAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<custom:PlaySoundAction Source="test.mp3" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</UserControl>
动作关联到元素:
要使用一个动作,需要触发器的帮助。触发器关联到元素,动作关联到触发器。使用PlaySoundAction的第一步就是要选择合适的触发器。所有的触发器派生自TriggerBase类。System.Windows.Interactivity.dll程序集包含一个名为EventTrigger的触发器,当一个特殊的事件发生的时候就会引发触发器。
使用PlaySoundAction,首先创建一个新的Silverlight项目,添加定义了PlaySoundAction类所在的Silverlight类库程序集、System.Windows.Interactivity.dll程序集的引用,然后在XAML中映射这两个命名空间,假定PlaySoundAction类在SilverlightClassLibrary1.dll中。