2011-12-06 10 views
8

Todavía estoy aprendiendo las cuerdas de MVVM y WPF, y por el momento estoy tratando de crear un Mediaplayer usando MVVM. Después de hacer búsquedas intensivas en Google, decidí que usar CommanParameter sería la mejor manera de evitar el código. Creo que el código y XAML se ven bien, pero no hay magia. Alias ​​no sucede nada.¿Cómo se usa CommandParameter con un RelayCommand?

¿Hay algún alma amable que quiera echar un vistazo a mi código y darme algún consejo? Como siempre, realmente valoro tus respuestas. Por favor, ignore mis plurales en RelayCommands, se estaba haciendo tarde :)

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

Asumo que he configuración del contexto de datos en la vista? – kenny

+1

sí lo hice :) está configurado en la ventana –

Respuesta

1

Su código de ejemplo funciona bien una vez que la propiedad VideoToPlay se ha establecido. ¿Estás seguro de que estás configurando esto? El fragmento de XAML no incluye cualquier uso de la OpenFileDialogCommand que establece esta propiedad:

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

Utilicé ese comando en el menú Superior, que olvidé mostrar en el código aquí :) pero está allí. Todavía no puedo obtener el botón reproducir para reproducir el video. –

+3

Oh cariño, me acabo de dar cuenta de lo que hice mal .... El comando es EN EL BOTÓN INCORRECTO. está en el botón <<. Alguien por favor envíeme una bofetada virtual. –

+0

SLAP :) SLAP :) – SvenG