我是 Xamarin 的新手,我尝试构建一个 Xamarin.Forms 应用程序,该应用程序使用特定扫描仪的 API。 这个 API 来自一个 Android java 项目,我创建了一个 Eclipse Library Project就像 Xamarin 上描述的那样。 所以我现在想做的是从 Eclipse 库项目绑定(bind)这个服务,但在这里我总是得到 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();
}
}
}
这是抛出的异常
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
调用时抛出异常
context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate);
有人知道我做错了什么吗?
最佳答案
经过几个小时的搜索和调试,我终于找到了问题所在。
我的 ServiceConnection 必须扩展 Java.Lang.Object
internal class ServiceConnection : Java.Lang.Object, IServiceConnection {
public IPlkScanService PlcScanService { get; private set; }
关于java - 来自 Eclipse 库项目的 Xamarin 绑定(bind)服务 -> InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069070/