简体   繁体   中英

XAML binding does not work in Windows Phone 8.1

I have a simple XAML page:

<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>

With the Page code looking like this:

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

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

Whenever a property in App.nowplaying is changed the class fires a notifychanged event. When the XAML receives this my app crashes with error:

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

Anyone know how I could solve this?

Also, the NotifyChanged event may have to be raised in the UI thread.

Something like this:

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

A first chance exception, is trigger by Visual Studio not the application. Look closely at the error. Type e,in to Watch list or look into Locals list. And I would advise you to read about mvvm pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM