2010-03-08 50 views
7

我试图让视频的使用MediaElement永远重复。我发现下面的代码在http://msdn.microsoft.com/en-us/library/ms741866.aspx,它工作正常。Mediaelement repeatbehavior在绑定到xpath时失败

<!-- 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#解决方案?

+0

我删除了触发器和MediaTimeLine并为“myMediaElement”创建了一个xpath绑定源。然后,我将此代码添加到myMediaElement_MediaEnded: this.myMediaElement.Position = TimeSpan.FromSeconds(0); this.myMediaElement.Play(); 它的工作原理,但由于某种原因加载缓慢 – KenS 2010-03-08 20:13:19

+0

我也想知道如何使用XAML来做到这一点。问题似乎是Source期待一个URI,而不是一个字符串。绑定到一个字符串并自动将其转换为URI似乎有点棘手,但是,如下所示:http://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding – Jakob 2010-09-04 23:37:35

+0

还有我对这个问题的50个声望仍未得到答复。 :( – Jakob 2010-09-11 23:38:51

回答

1

为什么不使用值转换器?我知道它并不完全是WPF,即一些C#编码是必需的,但这似乎正是你想要和需要的。 WPF教程有一个相当不错的解释,所以如果你不介意我把你引向那个页面,那么:http://www.wpftutorial.net/ValueConverters.html