玩在Silverlight中声音的MediaElement和微卡利 [英] Playing a sound in Silverlight with MediaElement and Caliburn Micro
本文介绍了玩在Silverlight中声音的MediaElement和微卡利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何播放使用微卡利在Silverlight中的MP3?
How can I play an MP3 in Silverlight using Caliburn Micro?
本的MediaElement的玩()方法需要基于视图模型一个布尔变量来执行。
The MediaElement's "play()" method needs to be executed based on a boolean variable in the ViewModel.
在此先感谢!
推荐答案
使用的IResult。 样品code
编辑:基于一个布尔值,如果你描述的这个场景我可以改变样品
Use an IResult. sample code based on a Boolean value, if you describe the scenario of this I can alter the sample.
查看:
<Grid>
<MediaElement AutoPlay="False"
Source="../Assests/Kalimba.mp3"></MediaElement>
<Button x:Name="Play"
Content="Play"
Height="50"
Width="150" />
</Grid>
视图模型:
public class MediaViewModel : Screen
{
public MediaViewModel()
{
DisplayName = "Media Sample";
}
public IEnumerable<IResult> Play()
{
var result = new PlayMediaResult();
yield return result;
}
}
PlayMediaResult:
PlayMediaResult:
public class PlayMediaResult : IResult
{
public void Execute(ActionExecutionContext context)
{
var view = context.View as FrameworkElement;
var mediaElement = FindVisualChild<MediaElement>(view);
if (mediaElement != null)
{
mediaElement.Play();
Completed(this, new ResultCompletionEventArgs {});
}
Completed(this, new ResultCompletionEventArgs {});
}
public event EventHandler<ResultCompletionEventArgs> Completed;
public static TChildItem FindVisualChild<TChildItem>(DependencyObject obj)
where TChildItem : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is TChildItem)
return (TChildItem) child;
var childOfChild = FindVisualChild<TChildItem>(child);
if (childOfChild != null)
return childOfChild;
}
return null;
}
}
}
这篇关于玩在Silverlight中声音的MediaElement和微卡利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文