Xamarin.forms 在Xamarin表单中调用DisplayALert()时获取异常
我试图用以下代码在xamarin表单中启动一个警报框,但调用失败,出现以下异常Xamarin.forms 在Xamarin表单中调用DisplayALert()时获取异常,xamarin.forms,Xamarin.forms,我试图用以下代码在xamarin表单中启动一个警报框,但调用失败,出现以下异常 private async void ProfileEmailAddressViewModel_UpdatedPersonalDetailsEvent(object source, UpdateEmployeePersonalDetailsCompletedEventArgs e) { try { if (e.Result) {
private async void ProfileEmailAddressViewModel_UpdatedPersonalDetailsEvent(object source, UpdateEmployeePersonalDetailsCompletedEventArgs e)
{
try
{
if (e.Result)
{
await DisplayAlert("Alert", "You have been alerted", "OK");
}
}
catch (Exception ex)
{
throw ex;
}
}
异常详细信息:System.Reflection.TargetInvocationException:调用的目标已引发异常。-->UIKit.UIKitThreadAccessException:UIKit一致性错误:您正在调用只能从UI线程调用的UIKit方法。
在/Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:88中的UIKit.UIApplication.EnsureUIThread()[0x0001a]
在/Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIView.g.cs:2817中的UIKit.UIView.set_BackgroundColor(UIKit.UIColor值)[0x00000]
位于Xamarin.Forms.Platform.iOS.Platform.PresentAlert(Xamarin.Forms.Internals.AlertArguments)[0x0000d]
如果有人因为某种原因遇到过类似问题,请告诉我,您不是从UI线程调用此问题的 在
BeginInvokeOnMainThread
方法中包装调用
Device.BeginInvokeOnMainThread (async () => {
await DisplayAlert("Alert", "You have been alerted", "OK");
});
当您在后台线程上执行某些操作并且希望与UI交互时,您将需要
BeginInvokeOnMainThread
方法在负责UI的主线程上调用它。由于某些原因,您不是从UI线程调用它
在BeginInvokeOnMainThread
方法中包装调用
Device.BeginInvokeOnMainThread (async () => {
await DisplayAlert("Alert", "You have been alerted", "OK");
});
当您在后台线程上执行某些操作并希望与UI交互时,需要使用
BeginInvokeOnMainThread
方法在负责UI的主线程上调用它。谢谢Gerald。很好,谢谢你,杰拉尔德。工作得很好。