-1

I have a class written in this way

public class AudioPlayer : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    [...]
    private static AudioPlayer instance = new AudioPlayer();

    public static AudioPlayer Instance { get { return instance; } }

    private Track currentTrack = null;

    // the pointer to the current track selected
    // it is useful to retrieve its new position when playlist got updates
    public Track CurrentTrack { get { return currentTrack; } 
        private set 
        { 
            currentTrack = value;
            NotifyPropertyChanged();
        } 
    }
    public class Track : ICloneable
    {
            public string Title { get; set; }
    }

Here's the xaml:

    <StackPanel DataContext="{Binding Source={x:Static audiocontroller:AudioPlayer.Instance}}">
        <Label Name="lbl_bind" Content="{Binding CurrentTrack.Title}"></Label>
        <Button Name="btn" Click="btn_Click" Height="20" ></Button>
    </StackPanel>

and the code works!

Now i wish to scorporate AudioPlayer, using a ModelView controller. How to do this ?

user1649247
  • 47
  • 2
  • 7
  • 4
    This class isn't static. Why are you talking about static classes? And what do you mean by "scorporate"? – CodesInChaos Sep 17 '12 at 15:19
  • it is static because of this private static AudioPlayer instance = new AudioPlayer(); the class that is not static is Track, but at this point I'm a bit confused on how to handle the mvvm. I would write a class that should handle AudioPlayer, and remove all the NotifyPropertyChanged from that class, because "CurrentTrack" is meant to be the model, and not the ModelView – user1649247 Sep 17 '12 at 15:29
  • 2
    That's a static field, and not a static class. – CodesInChaos Sep 17 '12 at 15:31
  • 1
    You are setting your `DataContext` (data layer) to a static instance, but the data layer should be dynamic. Your application is your ViewModels, not your Views, so a static `DataContext` means a static application. You're better off simply creating a new instance of `AudioPlayer` when the application first starts up, and setting it to the application's `DataContext`. You can have static components inside your application, but don't make your entire application static. – Rachel Sep 17 '12 at 15:51
  • Rachel, are you meaning something like this? – user1649247 Sep 17 '12 at 21:07

1 Answers1

0

Assuming 'AudioPlayer' is your 'ViewModel' you can implement like this:

<UserControl x:Class="YourNamespace.AudioPlayerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:YourNamespace">

    <UserControl.DataContext>
        <local:AudioPlayer/>
    </UserControl.DataContext>

    <StackPanel>
        <Label Name="lbl_bind" Content="{Binding CurrentTrack.Title}"></Label>
        <Button Name="btn" Click="btn_Click" Height="20" ></Button>
    </StackPanel>
</UserControl>

I would suggest avoid naming controls unless you are actually write code-behind (for MVVM this should be mostly unnecessary). I would also recommend switching from 'Click' handlers to 'Command' handlers and using something like the 'DelegateCommand' for handling button events.

karmasponge
  • 1,169
  • 2
  • 12
  • 26