2011-12-06 252 views
8

我仍在学习MVVM和WPF的知识,此刻我正在尝试使用MVVM创建Mediaplayer。经过大量的搜索后,我决定使用CommanParameter是避免代码隐藏的最好方法。我认为代码和XAML看起来很好,但没有魔术 - AKA什么也没有发生。如何将CommandParameter与RelayCommand一起使用?

有没有什么好的灵魂在那里谁会介意看看我的代码,并给我一些建议?一如既往,我真正珍惜你的答案。一旦物业VideoToPlay已设置请忽略我在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); 
     } 
    } 

} 
+0

我假设您已经在视图上设置了数据上下文? – kenny

+1

是的,我做到了:)它被设置为窗口 –

回答

1

你的示例代码工作正常。你确定你在设置吗?您的XAML代码段不包含任何使用此属性的OpenFileDialogCommand

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

我在Top菜单中使用了这个命令,我忘记了在这里的代码中显示:)但它在那里。我仍然无法播放按钮播放视频。 –

+3

哦亲爱的,我刚刚意识到我做错了什么....该命令是在错误的按钮。它在<<按钮上。有人请给我一个虚拟耳光。 –

+0

SLAP :) SLAP :) – SvenG

相关问题