无法在由时钟驱动的 MediaElement 上设置源
我创建了一个简单的 WPF 应用程序。 在我的 XAML 中,我有:
<StackPanel Orientation="Horizontal">
<MediaElement x:Name="media" Width="200" Height="200" Source="D:\Wpf project\wpfSampleApp\wpfSampleApp\Wildlife.wmv">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded" SourceName="media">
<EventTrigger.Actions>
<BeginStoryboard Name= "myBegin">
<Storyboard SlipBehavior="Slip">
<MediaTimeline Source="{Binding Source,ElementName=media,Mode=OneWay}" Storyboard.TargetName="media"
BeginTime="0:0:0" Duration="0:10:59" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
<Button x:Name="btnnext" Content="Next" Width="100" Height="30" Click="btnnext_Click"/>
</StackPanel>
在 XAML.cs 文件中,单击下一个按钮时,我必须播放下一个视频:
media.Source = new Uri(@"D:\\Wpf project\\wpfSampleApp\\wpfSampleApp\\B.wmv");
media.Play();
相反,我遇到一个错误,提示“无法在由时钟驱动的 MediaElement 上设置源”。
请帮助我克服这个异常,我更愿意修改 XAML 而不是在 XAML.cs 文件中编写代码。
任何帮助将不胜感激,谢谢
I have created one simple WPF app.
In my XAML I have:
<StackPanel Orientation="Horizontal">
<MediaElement x:Name="media" Width="200" Height="200" Source="D:\Wpf project\wpfSampleApp\wpfSampleApp\Wildlife.wmv">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded" SourceName="media">
<EventTrigger.Actions>
<BeginStoryboard Name= "myBegin">
<Storyboard SlipBehavior="Slip">
<MediaTimeline Source="{Binding Source,ElementName=media,Mode=OneWay}" Storyboard.TargetName="media"
BeginTime="0:0:0" Duration="0:10:59" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
<Button x:Name="btnnext" Content="Next" Width="100" Height="30" Click="btnnext_Click"/>
</StackPanel>
and in XAML.cs file, on next button click I must play the next video:
media.Source = new Uri(@"D:\\Wpf project\\wpfSampleApp\\wpfSampleApp\\B.wmv");
media.Play();
Instead I am facing an error that says "Cannot set source on MediaElement driven by clock."
Please help me to get over this exception, I'd prefer to modify the XAML rather than writing code in XAML.cs file.
Any help will be appreciated, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在已经很晚了,但如果有人需要类似的东西,我找到了一个不是 MVVM 模式的解决方案,但对于这种情况,它应该是可以原谅的。
Xaml部分
和CodeBehind
It's very late to the party, but if anyone needs something similar I found a solution not in an MVVM pattern, but for this case it should be forgivable.
Xaml part
And CodeBehind
我遇到了同样的问题。终于得到了解决方案。这是我实施的解决方案。
要在为 MediaElement 分配时钟时设置源 [使用 MediaTimeLine],我们应该首先检查当前线程是否有权访问 MediaElement。这可以通过布尔值 - MediaElement.CheckAccess 或 MediaElement.Clock.CheckAccess 来完成。
I met with same issue. Finally got a solution. Here is the solution I implemented.
To Set Source for MediaElement while Clock is assigned to it [using MediaTimeLine] we should first check whether current thread has access to MediaElement. This can be done by boolean values - MediaElement.CheckAccess or MediaElement.Clock.CheckAccess.
正如错误消息所示:如果
MediaElement
的源由时钟(即您的MediaTimeline
)控制,则无法设置它。您应该将MediaTimeline
的源(仅)绑定到DataContext
中的URI
。As the error message says: you cannot set source of
MediaElement
if it is controlled by a clock (i.e. yourMediaTimeline
). You should bind the source of theMediaTimeline
(only) to anURI
in yourDataContext
.