无法在由时钟驱动的 MediaElement 上设置源

发布于 2024-12-25 04:58:20 字数 1399 浏览 3 评论 0原文

我创建了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2025-01-01 04:58:20

现在已经很晚了,但如果有人需要类似的东西,我找到了一个不是 MVVM 模式的解决方案,但对于这种情况,它应该是可以原谅的。

Xaml部分

<MediaElement Name="Player">
    <MediaElement.Triggers>
        <EventTrigger RoutedEvent="MediaElement.Loaded">
            <EventTrigger.Actions>
                <Storyboard Name="MediaBoard">
                    <MediaTimeline Source="path" Storyboard.TargetName="Player" RepeatBehavior="Forever"/>
                </Storyboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </MediaElement.Triggers>
</MediaElement>

和CodeBehind

private void ChangeLoopedVideo(Uri url)
{
     MediaTimeline timeline = new MediaTimeline(url);
     timeline.RepeatBehavior = RepeatBehavior.Forever;
     Storyboard.SetTarget(timeline, Player);
     MediaBoard.Children.Clear();
     MediaBoard.Children.Add(timeline);
     MediaBoard.Begin();
}

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

<MediaElement Name="Player">
    <MediaElement.Triggers>
        <EventTrigger RoutedEvent="MediaElement.Loaded">
            <EventTrigger.Actions>
                <Storyboard Name="MediaBoard">
                    <MediaTimeline Source="path" Storyboard.TargetName="Player" RepeatBehavior="Forever"/>
                </Storyboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </MediaElement.Triggers>
</MediaElement>

And CodeBehind

private void ChangeLoopedVideo(Uri url)
{
     MediaTimeline timeline = new MediaTimeline(url);
     timeline.RepeatBehavior = RepeatBehavior.Forever;
     Storyboard.SetTarget(timeline, Player);
     MediaBoard.Children.Clear();
     MediaBoard.Children.Add(timeline);
     MediaBoard.Begin();
}
幻梦 2025-01-01 04:58:20

我遇到了同样的问题。终于得到了解决方案。这是我实施的解决方案。

要在为 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.

静若繁花 2025-01-01 04:58:20

正如错误消息所示:如果 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. your MediaTimeline). You should bind the source of the MediaTimeline (only) to an URI in your DataContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文