2010-03-08 63 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