如何在RelayCommand中使用CommandParameter

本文关键字:CommandParameter RelayCommand | 更新日期: 2023-09-27 18:17:13

我仍在学习MVVM和WPF的绳索,目前我正试图使用MVVM创建一个媒体播放器。经过密集的谷歌搜索,我决定使用commandparameter将是避免代码滞后的最佳方法。我认为代码和XAML看起来很好,但没有魔法-也就是说什么都没有发生。

有没有人会介意看一下我的代码并给我一些建议?一如既往,我很重视你的回答。请忽略我在RelayCommands中的复数,已经很晚了:)

XAML

<MediaElement Name="MediaElement" 
    Source="{Binding VideoToPlay}" 
    Width="400" Height="180" Stretch="Fill"
    LoadedBehavior="Manual" UnloadedBehavior="Manual"/>
<Slider Name="timelineSlider" Margin="5" Width="250" 
    HorizontalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button 
    Command="{Binding PlayMediaCommand}" 
    CommandParameter="{Binding ElementName=MediaElement, Mode=OneWay}">&lt;&lt;</Button>
c#

class MediaPlayerViewModel: INotifyPropertyChanged
{
    private MediaElement MyMediaElement;
    private Uri _videoToPlay;
    public Uri VideoToPlay
    {
        get { return _videoToPlay; }
        set
        {
            _videoToPlay = value;
            OnPropertyChanged("VideoToPlay");
        }
    }
    void SetMedia()
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = "c:''";
        dlg.Filter = "Media files (*.wmv)|*.wmv|All Files (*.*)|*.*";
        dlg.RestoreDirectory = true;
        if (dlg.ShowDialog() == true)
        {
            VideoToPlay = new Uri(dlg.FileName);
        }
    }
    RelayCommands _openFileDialogCommand;
    public ICommand OpenFileDialogCommand
    {
        get
        {
            if (_openFileDialogCommand == null)
            {
                _openFileDialogCommand = new RelayCommands(p => SetMedia(),
                    p => true);
            }
            return _openFileDialogCommand;
        }
    }
    RelayCommands _playMediaCommand;
    public ICommand PlayMediaCommand
    {
        get
        {
            if (_playMediaCommand == null)
            {
                _playMediaCommand = new RelayCommands(p => PlayMedia(p),
                    p => true);
            }
            return _playMediaCommand;
        }
    }
    void PlayMedia(object param)
    {
        var paramMediaElement = (MediaElement)param;
        MyMediaElement = paramMediaElement;
        MyMediaElement.Source = VideoToPlay;
        MyMediaElement.Play();
    }

    protected void OnPropertyChanged(string propertyname)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyname));
    }
    public event PropertyChangedEventHandler PropertyChanged;


class RelayCommands: ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;
    public event EventHandler CanExecuteChanged;
    public RelayCommands(Action<object> execute)
        : this(execute, null)
    {}
    public RelayCommands(Action<object> execute,
                   Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }
    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }
        return _canExecute(parameter);
    }
    public void Execute(object parameter)
    {
        _execute(parameter);
    }
    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}

如何在RelayCommand中使用CommandParameter

一旦设置了属性VideoToPlay,您的示例代码就可以正常工作。你确定要设置这个吗?您的XAML代码段没有使用OpenFileDialogCommand设置以下属性:

<Button Content="Select File" Command="{Binding OpenFileDialogCommand}" />
相关文章:
  • 没有找到相关文章