2011-12-06 45 views
8

Tôi vẫn đang học các dây giềng của MVVM và WPF, và tại thời điểm này tôi đang cố gắng tạo một Mediaplayer bằng MVVM. Sau khi googling chuyên sâu tôi quyết định rằng bằng cách sử dụng CommanParameter sẽ là cách tốt nhất để tránh mã phía sau. Tôi nghĩ rằng mã và XAML có vẻ ổn, nhưng không có phép thuật - AKA không có gì xảy ra.Làm thế nào để sử dụng CommandParameter với một RelayCommand?

Có linh hồn nào ngoài kia có thể quan tâm đến mã của tôi và cho tôi một số lời khuyên không? Như mọi khi, tôi thực sự đánh giá cao câu trả lời của bạn. Hãy bỏ qua số nhiều của tôi trong RelayCommands, nó đã muộn rồi :)

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

Tôi cho rằng bạn đã thiết lập ngữ cảnh dữ liệu trên chế độ xem? – kenny

+1

có tôi đã làm :) nó được đặt thành cửa sổ –

Trả lời

1

mã ví dụ của bạn hoạt động tốt khi sở hữu VideoToPlay đã được thiết lập. Bạn có chắc là bạn đang cài đặt cái này không? Đoạn mã XAML của bạn không bao gồm bất kỳ cách sử dụng nào của số OpenFileDialogCommand đặt thuộc tính này:

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

Tôi đã sử dụng lệnh đó trong menu Trên cùng, mà tôi quên hiển thị trong mã ở đây :) nhưng nó ở đó. Tôi vẫn không thể phát nút phát để phát video. –

+3

Oh thân yêu, tôi chỉ nhận ra những gì tôi đã làm sai .... Lệnh là ON BÔNG BÔNG. nó trên nút <<. Ai đó hãy gửi cho tôi một cái tát ảo. –

+0

SLAP :) SLAP :) – SvenG

Các vấn đề liên quan