【问题标题】:XAML binding does not work in Windows Phone 8.1XAML 绑定在 Windows Phone 8.1 中不起作用
【发布时间】:2014-06-25 18:02:11
【问题描述】:

我有一个简单的 XAML 页面:

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

页面代码如下所示:

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

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

每当 App.nowplaying 中的属性发生更改时,该类都会触发 notifychanged 事件。当 XAML 收到此消息时,我的应用程序崩溃并出现错误:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in SYSTEM.NI.DLL

有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: c# windows-phone-8 data-binding binding windows-phone-8.1


    【解决方案1】:

    此外,可能必须在 UI 线程中引发 NotifyChanged 事件。

    类似这样的:

    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        PropertyChanged(this, new sc.PropertyChangedEventArgs(propertyName));
    });
    

    【讨论】:

    • 是的,我发现这也是解决这个问题的方法
    【解决方案2】:

    第一次机会异常是由 Visual Studio 而不是应用程序触发的。仔细查看错误。键入 e,in 到 Watch list 或查看 Locals 列表。 我建议你阅读 mvvm 模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      相关资源
      最近更新 更多