【问题标题】:All Fields are null when OnServiceConnected() is called调用 OnServiceConnected() 时所有字段为空
【发布时间】:2017-05-29 09:02:20
【问题描述】:

我有一个使用 Android 服务的 Xamarin.forms 应用程序。 MainPage 通过依赖服务调用 Android 部分中的特定类 并且这个类绑定了服务。应用程序上下文由 MainPage 传递。

public bool BindService() {
  Intent intent = new Intent("com.plustek.service.plkscan.IPlkScanService");
  this._scanServiceConnection = new ServiceConnection(this, this._scanCallBack, this._systemCallBack);
  bool serviceBoundSuccess = this.Context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate);

  if (serviceBoundSuccess) {
    return true;
  }
  return false;
}


一切都很好,但是当调用 OnServiceConnected() 方法时,我的所有成员变量都为空。

任何人的想法?

     internal class ServiceConnection : Java.Lang.Object, IServiceConnection {

       public IPlkScanService PlcScanService { get; private set; }

       private PlkSystemCallBack _plkSystemCallBack;
       private PlkScanCallBack _plkScanCallBack;
       private PlustecDocumentScanner _plustecDocumentScanner;

       public ServiceConnection(PlustecDocumentScanner plustecDocumentScanner, PlkScanCallBack plkScanCallBack, PlkSystemCallBack plkSystemCallBack) : this(){
         this._plkScanCallBack = plkScanCallBack;
         this._plkSystemCallBack = plkSystemCallBack;
         this._plustecDocumentScanner = plustecDocumentScanner;
       }

       public ServiceConnection(IntPtr dfref, JniHandleOwnership asdf) {

       }

    public void OnServiceConnected(ComponentName name, IBinder service) {
         this.PlcScanService = PlkScanServiceStub.AsInterface(service);
         try {
           this.PlcScanService.InitService(this._plkScanCallBack);
           this.PlcScanService.RegisterSystemCallBack(this._plkSystemCallBack);
           this.PlcScanService.MountScannerDevice();
           this._plustecDocumentScanner.PlcScanService = this.PlcScanService;
         } catch (RemoteException e) {
           e.PrintStackTrace();
         }
       }

}

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    Stackoverflow 上的另一个帖子中发现了该问题。
    有两件事是错误的。第一件事是,如果需要这个 ctor,那就大错特错了:

    public ServiceConnection(IntPtr dfref, JniHandleOwnership asdf) {
    }
    

    第二个,我有一个覆盖 IntPtrHandle 基本版本的本地副本:public IntPtr Handle { get; }。删除该属性可使地图正常工作。
    我找到的更多信息here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多