Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在不违反MVVM模式的情况下,在mediaelement中拍摄视频快照_Wpf_Mvvm_Mediaelement - Fatal编程技术网

Wpf 在不违反MVVM模式的情况下,在mediaelement中拍摄视频快照

Wpf 在不违反MVVM模式的情况下,在mediaelement中拍摄视频快照,wpf,mvvm,mediaelement,Wpf,Mvvm,Mediaelement,我需要在MediaElement中拍摄视频播放的快照 下面的链接解释了我们如何实现这一点 但如何做到这一点而不违反MVVM模式 <Button Command="{Binding TakeSnapshotCommand}" CommandParameter="{Binding ElementName=media}"/> 其中,TakeSnapshotCommand是一个实现类似ICommand的类。另外,您发送参数T,在本例中,T是MediaElement,因此您将有以下声

我需要在MediaElement中拍摄视频播放的快照

下面的链接解释了我们如何实现这一点

但如何做到这一点而不违反MVVM模式


<Button Command="{Binding TakeSnapshotCommand}" 
CommandParameter="{Binding ElementName=media}"/>
其中,TakeSnapshotCommand是一个实现类似ICommand的类。另外,您发送参数T,在本例中,T是MediaElement,因此您将有以下声明

private RelayCommand<MediaElement> _takeSnapshotCommand;
public RelayCommand<MediaElement> TakeSnapshotCommand
{
get{ return _takeSnapshotCommand ??(_takeSnapshotCommand = new RelayCommand<MediaElement>(YourMethodTakingMediaElementAsParameter));}
}
private RelayCommand\u takesnapshot命令;
公共中继命令takesnapshot命令
{
获取{return{takeSnapshotCommand???({takeSnapshotCommand=newrelaycommand(YourMethodTakingMediaElementAsParameter));}
}

我想说链接中的代码没有违反MVVM模式。。。它使用代码隐藏,这在MVVM中是不建议的,但这并不是一种违反

也就是说,在可重用的UserControl或自定义控件中,该代码会更好,通过事件、命令或DependencyProperty返回快照,这样您就可以将其绑定到ViewModel

但总体而言,这更多的是一个良好实践的问题,而不是MVVM要求

<UserControl x:Class="SnapShots.SnapShotMediaViewer"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/...
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="MediaViewer">
  <StackPanel>
   <MediaElement x:Name="media" Stretch="Fill" Height="200" Width="300">
      <MediaElement.Triggers>
       <EventTrigger RoutedEvent="MediaElement.Loaded">
        <BeginStoryboard>
         <Storyboard>
          <MediaTimeline Source="thomasOnBoard.wmv"
                         RepeatBehavior="Forever"/>
         </Storyboard>
        </BeginStoryboard>
       </EventTrigger>
      </MediaElement.Triggers>
     </MediaElement>
     <Button Click="Button_Click" Content="Snapshot"/>
  </StackPanel>
</UserControl>

然后只需将此控件添加到视图中,并创建一个到
快照
属性的绑定。

我不想将mediaelement传递给viewmodel。我认为在MVVM模式中,不建议在viewmodel中操作UI组件。有没有办法让我只传递视频快照的位图图像作为命令参数。你可以传递任何你想要的。在CommandParameter中使用Converter来调整检索值。目前,它不是真正的MVVM。一个将元素转换成图像的绑定转换器,是的,这将是一个不错的设计。如果链接中断,您可能希望将该链接更多地总结为您的问题,以供将来的读者参考
public partial class SnapShotMediaViewer : UserControl
{
    public static readonly DependencyPropertyKey SnapshotPropertyKey =
        DependencyProperty.RegisterReadOnly("Snapshot", typeof(BitmapSource),
            typeof(SnapShotMediaViewer), new PropertyMetadata(null));

    public static readonly DependencyProperty SnapshotProperty =
        SnapshotPropertyKey.DependencyProperty;

    public BitmapSource Snapshot
    {
        get 
        {
            return (BitmapSource)GetValue(SnapshotProperty); 
        }
        private set
        {
            SetValue(SnapshotProperty, value);
        }
    }

    void Button_Click(object sender, RoutedEventArgs e)
    {
        Size dpi = new Size(96,96);
        RenderTargetBitmap bmp = 
            new RenderTargetBitmap(300, 200, 
            dpi.Width, dpi.Height, PixelFormats.Pbgra32);
        bmp.Render(media);

        Snapshot = bmp;
    }
}