Philippe Maes
Philippe Maes

Reputation: 510

INotifyPropertyChanged doesn't work

I have a simple object (which is globally initiated in App.xaml.cs):

public class now_playing : INotifyPropertyChanged
{
    // notify
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string p)
    {
        Debug.WriteLine(p + ": notify propertychanged");
        PropertyChangedEventHandler handler = PropertyChanged;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    // artist
    public string artist
    {
        get
        {
            return _artist;
        }
        set
        {
            _artist = value;
            NotifyPropertyChanged("artist");
        }
    }
    private string _artist;

    // album
    public string album
    {
        get
        {
            return _album;
        }
        set
        {
            _album = value;
            NotifyPropertyChanged("album");
        }
    }
    private string _album;

    // track title
    public string tracktitle
    {
        get
        {
            return _tracktitle;
        }
        set
        {
            _tracktitle = value;
            NotifyPropertyChanged("tracktitle");
        }
    }
    private string _tracktitle;
}

Whenever I change the values, the class does notify (I see the debug). So I guess the problems lies in my XAML or the code behind.

Page code:

public sealed partial class nowplaying : Page
{
    // artistdata
    public string artist { get { return App.nowplaying.artist; } }
    // albumdata
    public string album { get { return App.nowplaying.album; } }
    // trackdata
    public string tracktitle { get { return App.nowplaying.tracktitle; } }

    public nowplaying()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
}

XAML:

<Grid Margin="50">
    <TextBlock Text="{Binding tracktitle}" Foreground="White" FontSize="40"/>
    <TextBlock Foreground="#dcdcdc" FontSize="20" Margin="0,50,0,0">
        <Run Text="{Binding artist}"/>
        <Run Text=" - "/>
        <Run Text="{Binding album}"/>
    </TextBlock>
</Grid>

Why does the UI not update when I change values?

Stack trace:

Music.exe!Music.App.InitializeComponent.AnonymousMethod__6(object sender = {Music.App}, Windows.UI.Xaml.UnhandledExceptionEventArgs e = {Windows.UI.Xaml.UnhandledExceptionEventArgs}) Line 50  C#

Music.exe!play_music.MessageReceivedFromBackground(object sender = null, Windows.Media.Playback.MediaPlayerDataReceivedEventArgs e = {Windows.Media.Playback.MediaPlayerDataReceivedEventArgs}) Line 57 C#

UPDATE: problem solved! I had to use a dispatcher when calling the propertychanged event:

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
if (PropertyChanged != null)
{
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(p));
    });
}

Upvotes: 0

Views: 1457

Answers (2)

Dzior
Dzior

Reputation: 1495

You actually binding to artist, album and tracktitle Of nowplaying class which does implement INotifyPropertyChanged

Upvotes: 1

Christoph Fink
Christoph Fink

Reputation: 23113

You "loose" the change notification in the properties in the Page as these properties do not have any change notifiaction.

Try using now_playing directly:

public sealed partial class nowplaying : Page
{
    public now_playing NowPlaying { get { return App.nowplaying; } }

    public nowplaying()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
}

and

<Run Text="{Binding NowPlaying.artist}"/>

Otherwise you need to implement INotifiyPropertyChanged in nowplaying and forward the events from now_playing.

Upvotes: 2

Related Questions