简体   繁体   中英

How to bind a property on App.xaml.cs on all pages of universal app / Windows 8.1 and Windows Phone?

I need to check the internet status of the app without interfering the user interface. I followed the steps in this http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj835820.asp tutorial. I applied it to my App.xaml.cs. My implementation works, it checks for internet connection on every page whenever my internet connection changes, however, I cannot seem to update a text on every page that tells the status of the internet. The value of the text is updated in the app.xaml.cs. I defined a property in app.xaml.cs (InternetStatus) and Bind it to every page's TextBlock Text={Binding InternetStatus}. I did a breakpoint and upon checking the .xaml of the pages, the TextBlock holds the correct value but it was not displayed in the UI. Any help is appreciated. Thank you.

I had to solve similar problem and made solution like this:

App.xaml.cs:

public static string MyTextVariable = "xxxxx";

MyPage.xaml:

<TextBlock Name="MyTextBlock" Text="{Binding}"/>

MyPage.xaml.cs:

public MyPage()
{
    this.InitializeComponent();
    MyTextBlock.DataContext = App.MyTextVariable;
}

I made it for ListView and shared collection of items, so this example is not really tested, but it should work the same way as the list did.

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