8

I am still learning the ropes of MVVM and WPF, and at the moment I am trying to create a Mediaplayer using MVVM. After intensive googling I decided that using CommanParameter would be the best way to avoid code behind. I reckon the code and XAML looks fine, but there is no magic- AKA nothing is happening.

Is there any kind soul out there who would mind have a look at my code and give me some advice? As always, I truly value yours answers. Please ignore my plurals in RelayCommands, it was getting late :)

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);
        }
    }

}
2
  • I assume you've setup the data context on the view? Commented Dec 7, 2011 at 0:00
  • 1
    yes i did :) it is set to the window Commented Dec 7, 2011 at 0:06

1 Answer 1

1

Your example code works fine once the property VideoToPlay has been set. Are you sure you are setting this? Your XAML snippet did not include any usage of the OpenFileDialogCommand which sets this property:

<Button Content="Select File" Command="{Binding OpenFileDialogCommand}" />
Sign up to request clarification or add additional context in comments.

3 Comments

I used that command in the Top menu, which I forgot to show in code here :) but it's there. I still can't get the play button to play the video.
Oh dear, I just realised what I did wrong.... The command is ON THE WRONG BUTTON. it's on the << button. Somebody please send me a virtual slap.
Happy to hear you fixed your issue. If the answer above helped you get there, please accept it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.