Xamarin 表单:当代码执行到 displayalert 行时,UWP 应用程序中断
Xamarin forms: UWP app breaks when the code execution comes to displayalert line
我在我的项目中使用 displayalert
,它在 android
和 ios
设备上运行良好。但对于 UWP
部分,它在某些页面中不起作用。当代码执行到 displayalert
行时,UWP
应用中断并重定向到 App.g.i.cs
。代码控制来到下面的if block.
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
如果我将鼠标悬停在 e
上显示 {Windows.UI.Xaml.UnhandledExceptionEventArgs}
我正在使用以下代码显示警报:
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
如果我像下面这样更改上面的行,就不会出现问题。
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
});
有没有不加Device.BeginInvokeOnMainThread
解决这个问题的方法?这个问题背后的原因是什么?
示例代码: 下面的代码是一个按钮点击的代码。当代码执行到任何显示警告行时,出现上述错误。
async void ButtonClicked(Object sender, EventArgs e)
{
UserDialogs.Instance.ShowLoading("Signing in...");
var name = usernameEntry.Text;
var password = passwordEntry.Text;
if (Utility.IsInternet())
{
if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(password))
{
UserLogin(name, password);
}
else
{
UserDialogs.Instance.HideLoading();
await DisplayAlert("Alert", "Please enter all details", "Ok");
}
}
else
{
UserDialogs.Instance.HideLoading();
await DisplayAlert("Alert", "No internet connection", "Ok");
}
}
取决于这条线的位置:
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
不用Device.BeginInvokeOnMainThread
也可以。但我认为首先了解 Device.BeginInvokeOnMainThread
做什么很重要。
只要您在与主线程不同的线程上执行某些操作,它就会在后台 运行。主线程是唯一可以与 UI 交互的线程,因为否则它会因竞争条件和不可预测的 UI 行为而变得一团糟。据我所知,这对每一种编程语言都是正确的。
这意味着,您的警报周围的任何代码都在后台线程中 运行ning。这就是您收到此异常的原因。要在主线程上执行某些操作(从而可以访问 UI),您可以将其包装在 Device.BeginInvokeOnMainThread
调用中。
由于我们不知道您的其余代码是什么样的,因此很难判断您是否可以在 Device.BeginInvokeOnMainThread
调用中显示不换行的警报。但希望通过这些信息,您可以自己解决这个问题。
我在我的项目中使用 displayalert
,它在 android
和 ios
设备上运行良好。但对于 UWP
部分,它在某些页面中不起作用。当代码执行到 displayalert
行时,UWP
应用中断并重定向到 App.g.i.cs
。代码控制来到下面的if block.
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
如果我将鼠标悬停在 e
上显示 {Windows.UI.Xaml.UnhandledExceptionEventArgs}
我正在使用以下代码显示警报:
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
如果我像下面这样更改上面的行,就不会出现问题。
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
});
有没有不加Device.BeginInvokeOnMainThread
解决这个问题的方法?这个问题背后的原因是什么?
示例代码: 下面的代码是一个按钮点击的代码。当代码执行到任何显示警告行时,出现上述错误。
async void ButtonClicked(Object sender, EventArgs e)
{
UserDialogs.Instance.ShowLoading("Signing in...");
var name = usernameEntry.Text;
var password = passwordEntry.Text;
if (Utility.IsInternet())
{
if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(password))
{
UserLogin(name, password);
}
else
{
UserDialogs.Instance.HideLoading();
await DisplayAlert("Alert", "Please enter all details", "Ok");
}
}
else
{
UserDialogs.Instance.HideLoading();
await DisplayAlert("Alert", "No internet connection", "Ok");
}
}
取决于这条线的位置:
await DisplayAlert("Alert", "A verification code has been sent to your email.", "OK");
不用Device.BeginInvokeOnMainThread
也可以。但我认为首先了解 Device.BeginInvokeOnMainThread
做什么很重要。
只要您在与主线程不同的线程上执行某些操作,它就会在后台 运行。主线程是唯一可以与 UI 交互的线程,因为否则它会因竞争条件和不可预测的 UI 行为而变得一团糟。据我所知,这对每一种编程语言都是正确的。
这意味着,您的警报周围的任何代码都在后台线程中 运行ning。这就是您收到此异常的原因。要在主线程上执行某些操作(从而可以访问 UI),您可以将其包装在 Device.BeginInvokeOnMainThread
调用中。
由于我们不知道您的其余代码是什么样的,因此很难判断您是否可以在 Device.BeginInvokeOnMainThread
调用中显示不换行的警报。但希望通过这些信息,您可以自己解决这个问题。