2017-08-01 67 views
-1

我有一個關於綁定到視覺畫筆視覺的問題。如果我在XAML中定義它,它會起作用。如果我以編程方式定義相同的東西,那麼它不起作用。這是一個例子。Wpf可視化畫筆視覺綁定到MediaElement

<Window x:Class="WpfApp1.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      x:Name="MyWindow" 
      Title="MainWindow" Height="350" Width="525"> 
    <Grid > 
     <Rectangle Height="299" Width="400" Stroke="Red" StrokeThickness="20" > 
      <Rectangle.Fill> 
       <VisualBrush TileMode="None" Stretch="UniformToFill" Visual="{Binding ElementName=MyWindow, Path=Stuff}"> 
        <!--<VisualBrush.Visual> 
          <MediaElement Source="MovingImages_2017-03-10-05-02-22.wmv" LoadedBehavior="Play" /> 
         </VisualBrush.Visual>--> 
       </VisualBrush> 
      </Rectangle.Fill> 
     </Rectangle> 
    </Grid> 
</Window> 

我試過以下屬性作爲視覺和媒體元素。

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace WpfApp1 
{ 
    public partial class MainWindow : Window 
    { 
     public Visual Stuff { get; set; } 

     public MainWindow() 
     { 
      InitializeComponent(); 

      MediaElement me = new MediaElement(); 
      me.Source = new Uri("MovingImages_2017-03-10-05-02-22.wmv", UriKind.Relative); 
      me.LoadedBehavior = MediaState.Play; 


      this.Stuff = (Visual) me; 
     } 
    } 
} 

編輯3.我已經調整了綁定錯誤,它被綁定。它出現在Live Visual Tree/Live屬性資源管理器中。它只是不顯示。

有誰知道爲什麼?

+0

在窗口上設置'x:Name =「MainWindow」'。 – Clemens

+0

你說得對。我會更新我的代碼。但視頻仍然沒有出現。 – Haydn

回答

0

看起來這是因爲你的Stuff屬性沒有通知它已更改。 您有三個選擇來解決這個問題:

1)在MainWindow類中實現INotifyPropertyChanged接口,並在設置Stuff屬性時引發PropertyChanged事件。

2)定義Stuff屬性作爲一個DependencyProperty具有固有特性的變更通知(因爲你的類是爲DependencyObject)

3)調用的InitializeComponent之前指定的屬性值。這在初始化過程中顯然只會工作一次。

public MainWindow() 
{ 
    Stuff = new MediaElement 
    { 
     Source = new Uri("MovingImages_2017-03-10-05-02-22.wmv", UriKind.Relative), 
     LoadedBehavior = MediaState.Play 
    }; 

    InitializeComponent(); 
} 

這些都是一個屬性來參加比賽綁定

+0

我已經嘗試過1和3,但它仍然不能用於某些神祕的原因。明天再試第二次。 – Haydn

+0

嘗試2並檢查它現在被綁定。它是。已更新我的問題。 – Haydn

1

似乎有一個問題,結合視覺效果和VisualBrushes當兩個可用的方法。 在這種情況下,我會建議以一種也適合更好的MVVM分離的方式來支持您的問題。 不是綁定Visual,而是在XAML中創建視覺並綁定到Visual的Source屬性。 你的XAML是:

<Rectangle Height="299" Width="400" Stroke="Red" StrokeThickness="20"> 
     <Rectangle.Fill> 
      <VisualBrush TileMode="None" Stretch="UniformToFill"> 
       <VisualBrush.Visual> 
         <MediaElement Source="{Binding Path=MovieUri, ElementName=MyWindow}" LoadedBehavior="Play" /> 
        </VisualBrush.Visual> 
      </VisualBrush> 
     </Rectangle.Fill> 
    </Rectangle> 

而後面的代碼:

public Uri MovieUri { get; set; } 

    public MainWindow() 
    { 
     MovieUri = new Uri("movie.mp4", UriKind.Relative); 

     InitializeComponent();    
    } 
+0

感謝avamir10提供的答案,您描述的方法可行,但在我嘗試構建的應用程序中,這對我無效。實際上,我將VisualMedia3D加載到包含視頻的Viewport3D中。上面的例子只是一個簡單的例子。 – Haydn

0

這不是可以使用綁定到任何包含一個媒體元素,並把它玩。

解決方法是訂閱包含媒體元素的對象的視圖模型屬性已更改事件。當它更改時,以編程方式添加對象。然後媒體元素將顯示並播放。