【发布时间】:2015-10-29 12:28:06
【问题描述】:
我试图在失去互联网连接时向用户显示一条消息
我的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 吗?
【问题讨论】:
-
听起来像是希望您在 UI 线程中执行此操作。你试过这个吗:stackoverflow.com/a/17315187/1565402
-
啊,不错,没找到那个帖子,谢谢:)
标签: c# xaml windows-runtime windows-store-apps