WPF настройка видеоплеера из ViewModel - PullRequest
1 голос
/ 03 апреля 2020

Я работаю над приложением WPF. Мне нужен проигрыватель для воспроизведения файла, но мне нужен проигрыватель во ViewModel, так как он мне нужен для создания снимка на проигрывателе из ViewModel. Это не работает, так как я слышу звук, но видео пустое.

Что мне не хватает.

<ContentControl HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="{DynamicResource IconSnapShot}" />

Полный XAML

 <Window x:Class="TestPlayer.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:TestPlayer"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
     <Grid>
           <Grid>
               <ContentControl Content="{Binding PlayerFrameworkElement, NotifyOnSourceUpdated=True}" Visibility="{Binding PlayerVisibility}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Red"/>
          </Grid>
     </Grid>
</Window>

ViewModel

  using Gu.Wpf.Media;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  public class PlayerModelView : NotifyPropertyChanged
  {
    private MediaElementWrapper playerFrameworkElement = new MediaElementWrapper();
    private Uri sourcePath;

    public PlayerModelView()
    {
        this.SourcePath = new Uri(@"J:\file.mkv", UriKind.Absolute);
    }
    public Uri SourcePath
    {
        get
        {
            return this.sourcePath;
        }

        set
        {
            this.sourcePath = value;
            this.PlayerFrameworkElement.Source = null;
            this.PlayerFrameworkElement.Source = value;
        }
    }
    public MediaElementWrapper PlayerFrameworkElement
    {
        get
        {
            return this.playerFrameworkElement;
        }

        set
        {
            this.playerFrameworkElement = value;
            this.RaisePropertyChanged(() => this.PlayerFrameworkElement);
        }
    }

 }
}

// ----------------------------------------- -------------------------------------------------- ------------------------- // //
// // -------------- -------------------------------------------------- -------------------------------------------------- -

#region

using System;
using System.ComponentModel;
using System.Linq.Expressions;

#endregion

/// <summary>
///     The notify property changed.
/// </summary>
public abstract class NotifyPropertyChanged : INotifyPropertyChanged
{
    #region Public Events

    /// <summary>
    ///     The property changed.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Methods

    /// <summary>
    ///     The raise property changed.
    /// </summary>
    /// <param name="propertyExpression">
    ///     The property expression.
    /// </param>
    /// <typeparam name="TProperty">
    /// </typeparam>
    protected void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> propertyExpression)
    {
        var memberExpression = (MemberExpression)propertyExpression.Body;
        var propertyName = memberExpression.Member.Name;
        RaisePropertyChanged(propertyName);
    }

    /// <summary>
    ///     The raise property changed.
    /// </summary>
    /// <param name="propertyName">
    ///     The property name.
    /// </param>
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}
...