从绑定动态加载视频 [英] Dynamically Loading Video from Binding
问题描述
大家好
我正在尝试在MediaElement / VisualBrush中加载视频,如果我在Xaml中定义或直接从后面的代码中定义它,它就可以工作。如果我尝试相同的东西并通过绑定访问它,视频永远不会出现。
I am trying to load video in a MediaElement / VisualBrush and it works if I define it in Xaml or directly from the code behind. If I try the same thing and access it via a binding the video never shows up.
我已经在几个线程中读过freezable对象不是元素树的一部分因此无法绑定。
I have read in several threads that freezable objects are not part of the element tree and therefore cannot be bound.
有没有办法通过绑定动态加载视频(而不仅仅是更改源代码)。
Is there any way to dynamically load video via a binding (not just changing the source).
即使非常简单的例子,视频也不会出现。
Even really simple examples the video never shows up.
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
x:Name="MyWindow"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid x:Name="MyGrid" Background="{Binding MyBrush}">
</Grid>
</Window>
和ViewModel
And the ViewModel
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApp1
{
class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private VisualBrush myBrush = null;
public VisualBrush MyBrush
{
get
{
return this.myBrush;
}
set
{
this.myBrush = value;
OnPropertyChanged("MyBrush");
}
}
public MainViewModel()
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.Source = new Uri("C:\\Work\\test.avi", UriKind.Absolute);
myMediaElement.IsMuted = true;
// myMediaElement.Loaded += Me_Loaded;
VisualBrush myVisualBrush = new VisualBrush();
myVisualBrush.Visual = myMediaElement;
this.MyBrush = myVisualBrush;
}
private void Me_Loaded(object sender, RoutedEventArgs e)
{
MediaElement me = sender as MediaElement;
if (me != null)
{
me.Play();
}
}
}
}
推荐答案
你想在Uri中使用正斜杠你的viewmodel不应该对像MediaElement这样的UI元素有任何引用。
You want forward slashes in Uri and your viewmodel should not have any sort of a reference to a UI element like MediaElement.
这是我扔在一起的东西:
Here's something I threw together:
<Window x:Class="wpf_MediaElement_URL.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"
xmlns:local="clr-namespace:wpf_MediaElement_URL"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Play" Command="{Binding PlayCommand}"/>
<MediaElement Source="{Binding MeUri, NotifyOnTargetUpdated=True}"
TargetUpdated="MediaElement_TargetUpdated"
LoadedBehavior="Manual"
Grid.Row="1"/>
</Grid>
</Window>
代码背后:
namespace wpf_MediaElement_URL
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MediaElement_TargetUpdated(object sender, DataTransferEventArgs e)
{
MediaElement _me = e.TargetObject as MediaElement;
if(_me.Source !=null)
{
_me.Play();
}
}
}
}
Viewmodel
Viewmodel
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
namespace wpf_MediaElement_URL
{
public class MainWindowViewModel : ViewModelBase
{
private Uri meUri;
public Uri MeUri
{
get { return meUri; }
set { meUri = value; RaisePropertyChanged(); }
}
private RelayCommand _playCommand;
public RelayCommand PlayCommand
{
get
{
return _playCommand
?? (_playCommand = new RelayCommand(
() =>
{
MeUri = new Uri(@"C://Code/wpf_MediaElement/wpf_MediaElement/bin/Debug/Bootstrap.wmv", UriKind.Absolute);
}));
}
}
}
}
vid来自我的另一个代码示例。
The vid is from another sample piece of code I had.
请注意,当源绑定更改时播放视频是一个视图责任,因此后面的代码不会破坏mvvvm。
Note that playing a video when the source binding changes is a view responsibility so the code behind doesn't break with mvvvm.
如果你有一大堆媒体元素您可以将其更改为resourcedictionary中的样式,并使用eventsetter设置事件处理程序。
If you had a load of mediaelements you could change that into a style in a resourcedictionary and have an eventsetter to set up the event handler.
这篇关于从绑定动态加载视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!