如何在RelayCommand中使用CommandParameter?

8
我是一名正在学习MVVM和WPF的助手,目前我正在尝试使用MVVM创建一个媒体播放器。经过大量搜索后,我决定使用CommanParameter来避免编写代码。我认为代码和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);
        }
    }

}

我假设你已经在视图上设置了数据上下文? - kenny
1
是的,我做了 :) 它设置为窗口。 - Iris Classon
1个回答

1

一旦设置了VideoToPlay属性,您的示例代码就可以正常工作。您确定已经设置了这个属性吗?您的XAML片段没有包括任何使用OpenFileDialogCommand来设置此属性的内容:

<Button Content="Select File" Command="{Binding OpenFileDialogCommand}" />

我在顶部菜单中使用了那个命令,这里忘记展示代码了 :) 但是它确实存在。我仍然无法让播放按钮播放视频。 - Iris Classon
3
哦,亲爱的,我刚刚意识到我做错了.... 命令在错误的按钮上。它在“<<”按钮上。请有人给我一个虚拟的耳光吧。 - Iris Classon
2
很高兴听到你解决了问题。如果上面的回答对您有所帮助,请接受它 :) - FunnyItWorkedLastTime

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接