混合Action和API调用时Xamarin崩溃(线程问题?)

我遇到了问题 – “断言崩溃” – 在Xamarin中混合Action和API调用。

MainPage.xaml.cs中

private void Button_clicked(...) { serialLoader.Load(targetID, OnLoadSuccessful): } private void OnLoadSuccessful(TargetResult result) { // Do something } 

SerialLoader.cs

 public void Load(string targetID, Action OnLoadSuccessful) { // API service call that "forces" me to use the following client.LoadCompleted += (sender, e) => OnSerialLoadCompleted(sender, e, targetID, OnLoadSuccessful); client.LoadAsync(...) // I don't think this call is "really async" as return type is void. } public void OnSerialLoadCompleted(object sender, LoadCompletedEventArgs e, string targetID, Action OnLoadSuccessful) { if (...) { // If loaded successfully... // .. call the Action passing the result so that I can handle it in the MainPage.xaml.cs OnLoadSuccessful(e.Result); } } 

这会使应用程序崩溃,并出现以下错误

仿真器

 03-28 19:12:01.169 W/ (22852): Thread 0xcddbacd0 may have been prematurely finalized 03-28 19:12:01.169 W/ (22852): Thread 0xcddbacd0 may have been prematurely finalized 03-28 19:12:01.169 F/ (22852): * Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mono/utils/mono-threads.c:563, condition `info' not met 

智能手机(API 21)

 03-28 21:29:27.467 E/mono-rt (28909): ================================================================= 03-28 21:29:27.467 E/mono-rt (28909): Got a SIGSEGV while executing native code. This usually indicates 03-28 21:29:27.467 E/mono-rt (28909): a fatal error in the mono runtime or one of the native libraries 03-28 21:29:27.467 E/mono-rt (28909): used by your application. 03-28 21:29:27.467 E/mono-rt (28909): ================================================================= 03-28 21:29:27.467 E/mono-rt (28909): 03-28 21:29:27.467 F/libc (28909): Fatal signal 11 (SIGSEGV), code 2, fault addr 0x9b691fd8 in tid 29095 (Threadpool work) 

我假设这与multithreading的执行有关; 我尝试添加“Device.BeginInvokeOnMainThread”(没有用)并试图用Func替换我的Action,但没有运气。 此外,我很惊讶模拟器和智能手机都崩溃了!

任何帮助表示赞赏。

其他详细信息:Windows 10上的VS2017社区15.6.4 Xamarin Forms 2.5.0.280555目标Android SDK 8.1(API 27 – Oreo)

好的,道歉。 上面的症状是正确的,应用程序确实崩溃而没有提供任何反馈。 但是关注似乎与目标页面有关,我将PushingAsync引入MainPage.xaml.cs中的OnLoadSuccessful方法。

为什么会发生这种情况仍然完全不清楚,但请忽略这个问题。