我试图在连接到互联网时向用户显示消息
我在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?
答案 0 :(得分:2)
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
MessageDialog msg= new MessageDialog("No Internet");
msg.ShowAsync();
});
答案 1 :(得分:2)
Manuel Patrone的答案几乎是正确的。只是他的答案的小修正。
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async() =>
{
MessageDialog msg= new MessageDialog("No Internet");
await msg.ShowAsync();
});
答案 2 :(得分:0)
如果要通过Desktop Bridge运行,请确保正确初始化商店上下文。有关更多信息,请参见Using the StoreContext class with the Desktop Bridge。