【问题标题】:Wpf Visual Brush Visual Binding to MediaElementWpf 视觉画笔视觉绑定到 MediaElement
【发布时间】:2017-08-01 06:30:32
【问题描述】:

我有一个关于绑定到视觉画笔视觉的问题。如果我在 XAML 中定义它,它就可以工作。如果我以编程方式定义相同的东西,那么它不起作用。这是一个例子。

 <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 property Explorer 中。它只是没有显示出来。

有人知道为什么吗?

【问题讨论】:

  • 在窗口上设置x:Name="MainWindow"
  • 你是对的。我会更新我的代码。但是视频仍然没有显示。

标签: c# wpf binding visualbrush


【解决方案1】:

绑定 Visuals 和 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();            
    }

【讨论】:

  • 感谢 avamir10 的回答,您描述的该方法将起作用,但在我尝试构建的应用程序中对我不起作用。实际上,我正在将 VisualMedia3D 动态加载到包含视频的 Viewport3D 中。上面的示例只是一个简化的示例。
【解决方案2】:

这似乎是因为您的 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();
}

这是属性参与绑定游戏的两种可用方法

【讨论】:

  • 我已经尝试了 1 和 3,但由于某种神秘的原因它仍然无法正常工作。明天试试第二个。
  • 试过 2 并检查它现在被绑定了。这是。已更新我的问题。
【解决方案3】:

不可能对包含媒体元素的任何内容使用绑定并让它播放。

解决方法是为包含媒体元素的对象订阅视图模型属性更改事件。当它发生变化时,您以编程方式添加对象。然后媒体元素将显示并播放。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多