调用外部API时,Xamarin崩溃

时间:2018-03-29 17:00:57

标签: android multithreading xamarin.forms

这是我的案例(我认为这很简单和常见)。

MainPage.xaml.cs中

private void Button_clicked(...) {
  // Rely on backgound thread
  MessagingCenter.Subscribe<SerialLoader, SerialRec>(this, "SerialLoader", OnSerialLoaded);
  // Trigger the load
  serialLoader.Load(targetID);
}

private void OnSerialLoaded(...) {
   Device.BeginInvokeOnMainThread(() => Navigation.PushAsync(...));
}

SerialLoader.cs

public void Load(string targetID,) {
  // API service call that "forces" me to use the following
  client.LoadCompleted += OnSerialLoadCompleted(sender, e);
  client.LoadAsync(...) // I don't think this call is "really async" as return type is void.
}

public void OnSerialLoadCompleted(object sender, LoadCompletedEventArgs e) {
  if (...) { // If loaded successfully...
    // Notify the receiver that load has completed.
    MessagingCenter.Send<SerialLoader, SerialRec>(this, "SerialLoader", e.Result); 
  }
}

成功调用OnSerialLoaded,然后Xamarin崩溃应用程序时出现以下错误。

仿真器 在没有任何警告或消息的情况下关闭应用程序!

SMARTPHONE(API 21)

03-29 18:47:52.617 E/mono-rt (13649): No native Android stacktrace (see debuggerd output).
03-29 18:47:52.617 E/mono-rt (13649): 
03-29 18:47:52.617 E/mono-rt (13649): 
03-29 18:47:52.617 E/mono-rt (13649): =================================================================
03-29 18:47:52.617 E/mono-rt (13649): Got a SIGSEGV while executing native code. This usually indicates
03-29 18:47:52.617 E/mono-rt (13649): a fatal error in the mono runtime or one of the native libraries 
03-29 18:47:52.617 E/mono-rt (13649): used by your application.
03-29 18:47:52.617 E/mono-rt (13649): =================================================================
03-29 18:47:52.617 E/mono-rt (13649): 
03-29 18:47:52.617 F/libc    (13649): Fatal signal 11 (SIGSEGV), code 2, fault addr 0xbe562fd0 in tid 13649 (com.s4gv.mobile)

我认为这与多线程的执行有关;我没有使用MessagingCenter试过(参见this post)。

另外,我对模拟器和智能手机的崩溃感到非常惊讶!

任何帮助都会受到赞赏,因为我现在完全陷入困境:(

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

0 个答案:

没有答案