silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
想象一下,当用户完成一个操作(例如单击一个按钮),你希望能播放声音提示。如果不用行为的方式,是比较容易实现的。添加一个MediaElement到页面,提供音频文件的URI,然后在适当的时候调用MediaElement.Pla
触发器和动作是协同工作的。当某件事情发生的时候,触发器就会引发并调用一个动作,触发器和动作组成了最简单的行为表现形式。
创建一个动作:
想象一下,当用户完成一个操作(例如单击一个按钮),你希望能播放声音提示。如果不用行为的方式,是比较容易实现的。添加@L_966_1@mediaElement到页面,提供音频文件的URI,然后在适当的时候@L_934_0@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)
- {
-
- Panel container = FindContainer();
-
if (container != null)
- {
-
-
MediaElement media = new MediaElement();
-
media.source = this.source;
-
- media.MediaEnded += (s, E) => container.Children.Remove(media);
- media.Media@R_301_1488@ += (s, E) => container.Children.Remove(media);
-
-
media.AutoPlay = true;
- container.Children.Add(media);
- }
- }
-
-
private Panel FindContainer()
- {
-
FrameworkElement element = this.AssociatedObject;
-
-
while (element != null)
- {
-
if (element is Panel)
- {
-
return (Panel)element;
- }
-
element = VisualTreeHelper.GetParent(element) as FrameworkElement;
- }
-
return null;
- }
动作关联到元素:
要使用一个动作,需要触发器的帮助。触发器关联到元素,动作关联到触发器。使用PlaySoundAction的第一步就是要选择合适的触发器。所有的触发器派生自triggerBase类。System.Windows.Interactivity.dll程序集包含一个名为Eventtrigger的触发器,当一个特殊的事件发生的时候就会引发触发器。
使用PlaySoundAction,首先创建一个新的Silverlight项目,添加定义了PlaySoundAction类所在的Silverlight类库程序集、System.Windows.Interactivity.dll程序集的引用,然后在XAML中映射这两个命名空间,假定PlaySoundAction类在SilverLightclassLibrary1.dll中。
- <UserControl x:Class="SilverlightApplication1.behaviors.ActionSample"
-
xmlns="http://scheR_433_11845@as.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://scheR_433_11845@as.microsoft.com/winfx/2006/xaml"
-
xmlns:d="http://scheR_433_11845@as.microsoft.com/expression/blend/2008"
-
xmlns:mc="http://scheR_433_11845@as.openxmlformats.org/markup-compatibility/2006"
-
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
-
xmlns:custom="clr-namespace:SilverLightclassLibrary1;assembly=SilverLightclassLibrary1"
-
@H_971_410@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>
本文
链接地址为:
http://blog.csdn.net/fan_ken/article/details/6727380