windows phone 8 UI异步更新

本文关键字:异步 更新 UI phone windows | 更新日期: 2023-09-27 17:50:33

我在网上了解了如何异步更新页面UI,并得到了我们可以通过调用dispatcher和调用方法来更新页面UI,如

Dispatcher.BeginInvoke(() =>
{
   MessageBox.Show("some message");
});

但是,它不工作。我想我遗漏了一些基本概念。我想更新状态文本框与手机的互联网连接。

async void HomePage_Loaded(object sender, RoutedEventArgs e)
{
    if (App.Connectivity == -1)
    {
        await ConnectionInitialization();
    }
}
private async Task ConnectionInitialization()
{
    if(NetworkInterface.GetIsNetworkAvailable())
    {
        SystemTray.ProgressIndicator = new ProgressIndicator();
        Deployment.Current.Dispatcher.BeginInvoke(() => SetProgressIndicator(true));
        Deployment.Current.Dispatcher.BeginInvoke(() => TextBoxAppStatus.Text = "Connecting...");
    } 
    else
    {
        Deployment.Current.Dispatcher.BeginInvoke(() => TextBoxAppStatus.Text = "Disconnected");
        await ConnectionInitialization();
    }
}

这里,它进入了无限循环,没有在屏幕上显示任何东西,既没有进度条也没有文本框。请告诉我,我在概念上是错误的。我将非常感谢,如果你也可以为我提供资源,使用户友好的stud应用程序。谢谢

windows phone 8 UI异步更新

在你的代码中,如果没有可用的连接,你有无限递归

关于线程:如果你在UI上使用async,代码继续在UI线程上执行,所以你不需要使用Dispatcher

使用DeviceNetworkInformation.NetworkAvailabilityChanged事件跟踪连接状态的变化。