2017-05-19 5 views
1

私はXamarinの新機能で、特定のScannerにAPIを使用するXamarin.Formsアプリケーションを作成しようとしています。 このAPIはAndroidのJavaプロジェクトからのもので、Xamarinに記述されているようにEclipse Library Projectを作成しました。 私が今やりたいことは、このサービスをEclipseライブラリプロジェクトからバインドすることですが、私はいつもinvalidCastExceptionを取得します。EclipseライブラリプロジェクトのXamarinバインドサービス - > InvalidCastException

[assembly: Xamarin.Forms.Dependency(typeof(PlustecDocumentScanner))] 
namespace DemoApplication.Droid.Plustec { 
    class PlustecDocumentScanner : IDocumentScanner { 
    private IPlkScanService _plcScanService; 
    private PlkScanCallBack _scanCallBack; 
    private PlkSystemCallBack _systemCallBack; 
    private IServiceConnection _scanServiceConnection; 

    public bool ScanServiceLoaded { get; private set; } 

    public PlustecDocumentScanner() { 
     InitializeScanner(); 
     //Application.Context.StartActivity(new Intent(Application.Context,typeof(ScanDemoActivity))); 
    } 

    private void RunOnUiThread(Func<object> p) { 
     throw new NotImplementedException(); 
    } 

    private void InitializeScanner() { 
     InitialObjects(); 
     BindService(); 
    } 

    private void InitialObjects() { 
     // init objects .. 
    } 

    private void InitialProfiles() { 
     // Init profiles .. 
    } 

    private bool BindService() { 
     Context context = Application.Context; 
     Intent intent = new Intent(context, typeof(IPlkScanService)); 
     this._scanServiceConnection = new ServiceConnection(this._scanCallBack, this._systemCallBack); 
     bool serviceBoundSuccess = context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate); 
     context.StartActivity(intent); 
     if (serviceBoundSuccess) { 
     ServiceConnection service = (ServiceConnection)this._scanServiceConnection; 
     this._plcScanService = service.PlcScanService; 
     return true; 
     } 
     return false; 
    } 

    public void StartScan() { 
     .. 
    } 
    } 
} 

そして、これはServiceConnectionが

namespace DemoApplication.Droid.Plustec { 
internal class ServiceConnection : IServiceConnection { 
    public IPlkScanService PlcScanService { get; private set; } 

    public IntPtr Handle { get; set; } 

    private PlkSystemCallBack _plkSystemCallBack; 
    private PlkScanCallBack _plkScanCallBack; 

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

    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(); 
    } catch (RemoteException e) { 
     e.PrintStackTrace(); 
    } 
    } 

    public void OnServiceDisconnected(ComponentName name) { 
    try { 
     if (this.PlcScanService != null) { 
     this.PlcScanService.UnmountScannerDevice(); 
     this.PlcScanService.UnregisterSystemCallBack(this._plkSystemCallBack); 
     this.PlcScanService.UnregisterCallBack(this._plkScanCallBack); 
     this.PlcScanService.UninitService(); 
     } 
    } catch (RemoteException e) { 
     e.PrintStackTrace(); 
    } 

    this.PlcScanService = null; 
    this._plkSystemCallBack = null; 
    this._plkScanCallBack = null; 
    } 

    public void Dispose() { 
    throw new NotImplementedException(); 
    } 
} 
} 

のように見え、これは

context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate); 

誰を呼び出す

05-19 12:41:56.028 E/mono(5112): 
05-19 12:41:56.028 E/mono(5112): Unhandled Exception: 
05-19 12:41:56.028 E/mono(5112): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Specified cast is not valid. 
05-19 12:41:56.028 E/mono (5112): at Android.Content.ContextWrapper.BindService(Android.Content.Intent service, Android.Content.IServiceConnection conn, Android.Content.Bind flags) [0x0002c] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Content.ContextWrapper.cs:810 
05-19 12:41:56.028 E/mono(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner.BindService() [0x0002f] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:136 
05-19 12:41:56.028 E/mono(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner.InitializeScanner() [0x00008] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:60 
05-19 12:41:56.028 E/mono(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner..ctor() [0x00008] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:50 
05-19 12:41:56.028 E/mono(5112): at(wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke(System.Reflection.MonoCMethod, object, object[], System.Exception&) 
05-19 12:41:56.028 E/mono(5112): at System.Reflection.MonoCMethod.InternalInvoke(System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
05-19 12:41:56.028 E/mono(5112): --- End of inner exception stack trace --- 
05-19 12:41:56.038 E/mono-rt(5112): [ERROR] 
FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Specified cast is not valid. 
05-19 12:41:56.038 E/mono-rt (5112): at Android.Content.ContextWrapper.BindService(Android.Content.Intent service, Android.Content.IServiceConnection conn, Android.Content.Bind flags) [0x0002c] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Content.ContextWrapper.cs:810 
05-19 12:41:56.038 E/mono-rt(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner.BindService() [0x0002f] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:136 
05-19 12:41:56.038 E/mono-rt(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner.InitializeScanner() [0x00008] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:60 
05-19 12:41:56.038 E/mono-rt(5112): at DemoApplication.Droid.Plustec.PlustecDocumentScanner..ctor() [0x00008] in C:\git\devOps\Quickzone\QuickzoneForScanner\DemoApplication\DemoApplication\DemoApplication.Android\Plustec\PlustecDocumentScanner.cs:50 
05-19 12:41:56.038 E/mono-rt(5112): at(wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke(System.Reflection.MonoCMethod, object, object[], System.Exception&) 
05-19 12:41:56.038 E/mono-rt(5112): at System.Reflection.MonoCMethod.InternalInvoke(System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
05-19 12:41:56.038 E/mono-rt(5112): --- End of inner exception stack trace --- 
InspectorDebugSession(7): HandleTargetEvent: TargetExited 

例外がスローされるスローされる例外である方法ですアイデア私は間違っていますか?

答えて

2

数時間の検索とデバッグの後、私はついにこの問題を発見しました。
私のServiceConnectionはJava.Lang.Objectを拡張する必要があります

internal class ServiceConnection : Java.Lang.Object, IServiceConnection { 
    public IPlkScanService PlcScanService { get; private set; } 
関連する問題