2011-12-06 10 views
8

Nadal uczę się lin z MVVM i WPF, aw tej chwili próbuję stworzyć Mediaplayer'a używając MVVM. Po intensywnym googlowaniu zdecydowałem, że używanie CommanParameter będzie najlepszym sposobem na uniknięcie kodu. Uważam, że kod i XAML wygląda dobrze, ale nie ma żadnej magii - AKA nic się nie dzieje.Jak korzystać z CommandParameter z RelayCommand?

Czy jest jakaś dusza, która mogłaby spojrzeć na mój kod i udzielić mi porady? Jak zawsze, naprawdę cenię twoje odpowiedzi. Proszę ignorować moje liczbę mnogą RelayCommands, robiło się późno :)

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

Zakładam, że konfiguracja kontekst danych na widoku? – kenny

+1

tak, zrobiłem :) jest ustawione na okno –

Odpowiedz

1

Twój przykładowy kod działa poprawnie, gdy nieruchomość VideoToPlay został ustawiony. Czy na pewno to ustawiasz? Twój XAML fragment nie zawiera żadnych korzystanie z OpenFileDialogCommand który ustawia tę właściwość:

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

Użyłem tego polecenia w górnym menu, które zapomniałem pokazać w kodzie tutaj :) ale jest tam. Nadal nie mogę uzyskać przycisku odtwarzania, aby odtworzyć wideo. –

+3

Och kochanie, właśnie zdałem sobie sprawę, co zrobiłem źle .... Polecenie jest na niewłaściwym PRZYCISK. jest na przycisku <<. Niech ktoś mi przyśle wirtualne uderzenie. –

+0

SLAP :) SLAP :) – SvenG