[英]Show MessageDialog from app.xaml.cs in Windows Store app
我试图在失去互联网连接时向用户显示消息
我在App.xaml.cs
上有此方法
static async void NetworkInformation_NetworkStatusChanged(object sender)
{
//Get the Internet connection profile
//ConnectionProfile connectionProfileInfo = null;
try
{
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
ApplicationData.Current.LocalSettings.Values["INTERNET"] = false;
ShowBox("Internet Lost");
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(LoginPage));
}
else
{
ApplicationData.Current.LocalSettings.Values["INTERNET"] = true;
}
}
catch (Exception ex)
{
Debug.WriteLine("Unexpected exception occurred: " + ex.ToString());
}
}
还有另外一个
public async static void ShowBox(string msg)
{
try
{
await new MessageDialog(msg, "No Internet").ShowAsync();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
当我导致连接丢失(禁用我的互联网连接)时,将调用ShowBox方法,并且我在其上收到此异常:
无效的窗口句柄。
必须从具有CoreWindow的线程中调用此API,或者必须已显式设置了窗口。
有什么办法可以显示该事件的MessageDialog吗?
Manuel Patrone的答案几乎是正确的。 只是他的答案的小修正。
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async() =>
{
MessageDialog msg= new MessageDialog("No Internet");
await msg.ShowAsync();
});
如果您正在运行Desktop Bridge,请确保正确初始化商店上下文。 有关更多信息,请参阅将StoreContext类与Desktop Bridge一起使用 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.