首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >INotifyPropertyChanged不工作

INotifyPropertyChanged不工作
EN

Stack Overflow用户
提问于 2014-05-08 18:33:41
回答 2查看 1.4K关注 0票数 0

我有一个简单的对象(在App.xaml.cs中全局初始化):

代码语言:javascript
复制
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;
}

每当我更改这些值时,该类都会通知(我看到了调试)。所以我猜问题出在我的XAML或后面的代码中。

页面代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

当我更改值时,为什么UI不更新?

堆栈跟踪:

代码语言:javascript
复制
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#

更新:问题解决了!在调用propertychanged事件时,我必须使用调度程序:

代码语言:javascript
复制
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
if (PropertyChanged != null)
{
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(p));
    });
}
EN

回答 2

Stack Overflow用户

发布于 2014-05-08 18:39:45

您可以在Page的属性中“松绑”更改通知,因为这些属性没有任何更改通知操作。

尝试直接使用now_playing

代码语言:javascript
复制
public sealed partial class nowplaying : Page
{
    public now_playing NowPlaying { get { return App.nowplaying; } }

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

代码语言:javascript
复制
<Run Text="{Binding NowPlaying.artist}"/>

否则,您需要在nowplaying中实现INotifiyPropertyChanged,并从now_playing转发事件。

票数 2
EN

Stack Overflow用户

发布于 2014-05-08 18:42:33

您实际上绑定到了实现INotifyPropertyChanged的nowplaying类的艺术家、专辑和曲目标题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23539278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档