【问题标题】:Mediaelement repeatbehavior fails when binding to xpath绑定到 xpath 时,Mediaelement 重复行为失败
【发布时间】:2011-01-25 02:22:46
【问题描述】:

我正在尝试使用 MediaElement 让视频永远重复。我在http://msdn.microsoft.com/en-us/library/ms741866.aspx 找到了下面的代码,它工作正常。

<!-- The MediaElement control plays the sound. -->
        <MediaElement Name="myMediaElement" >
            <MediaElement.Triggers>
                <EventTrigger RoutedEvent="MediaElement.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>

                                <!-- The MediaTimeline has a RepeatBehavior="Forever" which makes the media play
                 over and over indefinitely.-->
                                <MediaTimeline Source="C:\MyVideo1.wmv" Storyboard.TargetName="myMediaElement"  
                                                RepeatBehavior="Forever" />

                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </MediaElement.Triggers>
        </MediaElement>

我遇到的问题是,当我尝试将 MediaTimeLine 绑定到 xml 源时,我收到错误 - “必须指定 URI”。

<MediaTimeline Source="{Binding XPath=MyVideos}" 
     Storyboard.TargetName="myMediaElement" RepeatBehavior="Forever" />

是否有可以替代 xaml 的 C# 解决方案?

【问题讨论】:

  • 我删除了触发器和 MediaTimeLine 并为“myMediaElement”创建了一个 xpath 绑定源。然后我将此代码添加到 myMediaElement_MediaEnded: this.myMediaElement.Position = TimeSpan.FromSeconds(0); this.myMediaElement.Play();它可以工作,但由于某种原因加载缓慢
  • 我也想知道如何使用 XAML 做到这一点。问题似乎是 Source 需要一个 URI,而不是字符串。不过,绑定到字符串并自动将其转换为 URI 似乎有点棘手,如下所示:stackoverflow.com/questions/20586/…
  • 我的名声中有 50 个问题仍未得到解答。 :(

标签: c# wpf mediaelement


【解决方案1】:

为什么不使用值转换器?我知道它不完全是 WPF,即需要一些 C# 编码,但这似乎完全符合您的需求。 WPF 教程有一个相当不错的解释,所以如果您不介意我将您推荐到该页面,那么:http://www.wpftutorial.net/ValueConverters.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多