2011-12-06 13 views
8

私はまだMVVMとWPFの縄を学んでいますが、現時点ではMVVMを使ってMediaplayerを作成しようとしています。集中的な検索を行った後、私はCommanParameterを使用することがコードの背後にあるのを避ける最良の方法であると判断しました。私はコードを考えて、XAMLはうまく見えますが、何も起こっていない魔法はありません。RelayCommandでCommandParameterを使用する方法?

私のコードを見て助けてもらえますか?いつものように、私は本当にあなたの答えを評価します。 、それは後半:)

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

:あなたのXAMLスニペットは、このプロパティを設定しますOpenFileDialogCommandのいずれかの使用を含んでいませんでしたか? – kenny

+1

はい私はそれをしました:)それはウィンドウに設定されています –

答えて

1

あなたの例のコードが設定されているVideoToPlayプロパティいったん正常に動作なっていたRelayCommandsに私の複数形を無視してください。あなたはこれを設定していますか?私はあなたきたセットアップビューのデータコンテキストを前提と

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

私はここでコードで表示するのを忘れて、トップメニューでそのコマンドを使用しました:)しかし、そこにあります。私はまだ再生ボタンをビデオを再生することができません。 –

+3

ああああ、私はちょうど私が間違っていたことを実現しました....コマンドは間違っています。それは<<ボタンの上にあります。誰かが私にバーチャル・スラップを送ってください。 –

+0

SLAP :) SLAP :) – SvenG

関連する問題