Xamarin的is unique by offering a single language – C#, class library, and runtime that works across all three mobile platforms of iOS, Android, and Windows Phone (Windows Phone’s native language is already C#), while still compiling native (non-interpreted) applications that are performant enough even for demanding games.
每个平台都有不同的功能集,并在其编写的本地应用程序的能力各不同 - 即,向下编译为本地代码,并与底层Java子系统互操作流畅应用。例如,一些平台只允许应用程序要建在HTML和JavaScript,而有些是非常低的水平,并只允许C / C ++代码。有些平台甚至不利用本机控制工具。
Xamarin是独一无二的,它结合了本机平台的所有权力,并增加了自己的许多强大功能,包括:
Xamarin提供两种商业产品:Xamarin。iOS和Xamarin.Android。它们都建在莫诺,基于已发布的.NET ECMA标准的.NET框架的开源版本。Mono几乎只有.NET Framework本身并在几乎每个可想象的平台上运行,包括Linux,Unix,FreeBSD和Mac OS X.
在iOS, Xamarin的提前时间(AOT编译器编译Xamarin。iOSapplications directly to native ARM assembly code. On Android, Xamarin’s compiler compiles down to中间语言(IL.),然后即时(JIT)在应用程序启动时编译为本机程序集。
在这两种情况下,Xamarin应用程序都利用一个运行时来自动处理诸如内存分配、垃圾收集、底层平台互操作等事情。
xamarin.forms.is a framework that allows developers to rapidly create cross-platform user interfaces. It provides it's own abstraction for the user interface that will be rendered using native controls on iOS, Android, Windows, or Windows Phone. This means that applications can share a large portion of their user interface code and still retain the native look and feel of the target platform.
Xamarin.Forms允许快速原型的应用程序,这些应用程序可以随着时间的推移而发展到复杂的应用程序。由于Xamarin.Forms应用程序是本机应用程序,因此他们没有其他工具包的限制,例如浏览器沙箱,有限的API或性能不佳。应用程序是使用Xamarin.Forms编写的,能够利用底层平台的任何API或功能,例如(但不限于)IOS上的CoreMotion,Passkit和StoreKit;NFC和Android上的Google Play服务;和瓷砖在窗户上。此外,可以创建将具有与Xamarin创建的用户界面的部分的应用程序。使用本机UI Toolkit创建其他部分而表格。
表单应用程序的架构与传统的跨平台应用程序相同。最常见的方法是使用便携式图书馆或共享项目要容纳共享代码并创建将使用共享代码的特定于平台的应用程序。
有两种技术可以在Xamarin.Forms中创建用户界面。第一种技术是完全使用C#源代码创建UI。第二种技术是使用可扩展应用程序标记语言(XAML),一种用于描述用户界面的声明性标记语言。有关XAML的更多信息,请参见XAML基础知识.
要首先开始开发Xamarin应用程序,您需要安装Visual Studio或Xamarin Studio,并确保包含所有必要的Xamarin组件。在我们的示例中,我们将使用Visual Studio向您展示如何开发Xamarin应用程序和使用我们的SDK。导航到此链接逐步阅读如何下载和安装用于Xamarin应用程序的Visual Studio。
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio并遵循以下步骤:
1.去文件->新建->项目。
2.创建空白应用程序(Android)。
加载新项目时,添加对XamarinDataManLibrary.dll文件的引用。
创建一个空白应用程序后,创建所有你将使用的资源(图标,图像,样式,布局等)。必威随行版官网你可以从我们的样品中复制。
接下来右键单击你的项目文件,然后单击属性,然后去Android Manifest部分。
设置清单文件(应用程序名称,应用程序图标,最小和最大Android版本),请务必为此应用程序启用相机权限。
在我们设置了一些必要的属性之后,我们可以创建ScannerActivity也就是主发射器,并继承一些接口。
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/ic_launcher")]onrequestpermissionsresultcallback {private static int REQUEST_PERMISSION_CODE = 12322;private TextView tvConnectionStatus, tvResult;私人按钮btnScan;私人RelativeLayout rlPreviewContainer;私人ImageView ivPreview;private bool isScanning = false;private static ReaderDevice ReaderDevice;private enum DeviceType {MX, PHONE_CAMERA} private static bool isdevicepselected = false;private static DeviceType param_deviceType = DeviceType. phone_camera;private static bool dialogAppeared = false; private static string selectedDevice = ""; private bool listeningForUSB = false; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_scanner); ......
在这里,我们将定义一些UI元素和变量,稍后将在活动中使用:
tvConnectionStatusTextView是一个用于显示当前连接状态的UI元素;
tvResult- TextView一个UI元素的结果,将读取扫描条形码;
btnScan按钮一个将触发的UI元素ReaderDevice.StartScanning.或readerDevice。StopScanning;
rlPreviewContainerViewGroup (RelativeLayout)容器用于相机预览;
ivPreview- ImageView控件,匹配大小作为其父控件,显示预览或扫描会话的最后一帧;
ReaderDevice-显示MX设备或手机摄像头的cmbSDK对象取决于我们的配置;
每次活动开始时,我们都会致电initDevice在这里,我们配置读取器设备对象。
如果我们想使用MX设备进行扫描,我们可以使用:
readerDevice = GetMXDevice(这个);if (!listeningForUSB) {readerDevice.StartAvailabilityListening();listeningForUSB = true;}
MX设备的可用性可以在设备转弯时更改在…上或离开,或者如果USB线连接或断开,由IReaderDeviceListener界面。
如果我们想将读取设备配置为手机摄像头,我们可以使用:
readerDevice = GetPhoneCameraDevice(此,CameraMode.NoAimer,PreviewOption.Defaults,rlPreviewContainer);
摄影机参数是类型的CameraMode(定义在CameraMode.java),它接受下列值之一:
passive_aimer.:初始化读卡器,使其使用被动瞄准器。被动瞄准器是附在移动设备或移动设备外壳上的附件,使用移动设备的内置LED闪光灯作为光源投射瞄准模式。在此模式下,设备屏幕上不会显示直播预览,因为会有一个瞄准模式被投射。
FRONT_CAMERA初始化读取器以使用设备的移动前置摄像头,如果可用(不是所有移动设备都有前置摄像头)。这是一种不寻常但可能的配置。大多数前置摄像头没有自动对焦和照明,提供的图像分辨率明显较低。这个选项应该小心使用。在此模式下,照明不可用。
所有上述模式都为读者提供了以下默认设置:
基于所选模式,设置了以下附加选项和行为:
的PreviewOption的类型PreviewOption(定义在PreviewOption.java),并用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项有:
配置后ReaderDevice我们需要连接到设备。
在我们建立联系之前ReaderDeviceListener对象被设置为接收事件:
ReaderDevice.setReaderDeviceListener(此);
此外,还可以使从读写器发送最后一个触发的图像:
readerDevice.EnableImage(真正的);
然后我们可以联系:
readerDevice.Connect(这个);
将被调用的事件有:
public void OnConnectionStateChanged(ReaderDevice reader)
如果在尝试连接时出现错误,则该错误将作为参数抛出OnConnection已完成方法,否则,如果没有发生错误,则错误参数为空值.
如果连接成功,则语句读者。ConnectionState = = ConnectionState。连接这是真的。
有两个API方法用于更改一些公共属性以配置连接的设备,您应该在ConnectionState是连接。
例如,如果移动相机被用作ReaderDevice有无符号启用默认.属性中使用的符号必须启用SetyymbologyEnabled.API方法:
readerDevice.SetSymbologyEnabled(Symbology.C128,真实,NULL);readerDevice.SetSymbologyEnabled(Symbology.Datamatrix,真实,NULL);readerDevice.SetSymbologyEnabled(Symbology.UpcEan,真实,NULL);readerDevice.SetSymbologyEnabled(Symbology.Qr,真实,NULL);
您可以通过向连接的设备发送命令来直接执行相同的操作:
readerDevice.DataManSystem.SendCommand(“将SYMBOL.MICROPDF417设置为ON”);
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startscanning.方法从你ReaderDevice对象
接下来会发生什么是基于类型ReaderDevice以及它如何被配置,但在一般情况:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults.的可迭代结果集合对象ReaderDevice侦听器方法。
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {listViewResultSelectedItem = -1;resultList.Clear ();resultListData.Clear ();ivPreview.SetImageBitmap(空);如果结果。SubResults!= null && results.SubResults.Count > 0) { foreach (ReadResult subResult in results.SubResults) { if (subResult.IsGoodRead) { resultList.Add(subResult); JavaDictionary item = new JavaDictionary(); item.Add("resultText", subResult.ReadString); Symbology sym = subResult.Symbology; if (sym != null) item.Add("resultType", subResult.Symbology.Name); else item.Add("resultType", "UNKNOWN SYMBOLOGY"); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } else { resultList.Add(subResult); JavaDictionary item = new JavaDictionary(); item.Add("resultText", "NO READ"); item.Add("resultType", ""); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } if (subResult.Image != null) { ivPreview.SetImageBitmap(renderSvg(subResult.ImageGraphics, subResult.Image)); } else { if (subResult.ImageGraphics != null) { ivPreview.SetImageBitmap(renderSvg(subResult.ImageGraphics, ivPreview.Width, ivPreview.Height)); } else ivPreview.SetImageBitmap(null); } } } else if (results.Count > 0) { ReadResult result = results.GetResultAt(0); if (result.IsGoodRead) { resultList.Add(result); JavaDictionary item = new JavaDictionary(); item.Add("resultText", result.ReadString); Symbology sym = result.Symbology; if (sym != null) item.Add("resultType", result.Symbology.Name); else item.Add("resultType", "UNKNOWN SYMBOLOGY"); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } else { resultList.Add(result); JavaDictionary item = new JavaDictionary(); item.Add("resultText", "NO READ"); item.Add("resultType", ""); resultListData.Add(item); listViewResultSelectedItem = resultListData.Count - 1; } if (result.Image != null) { ivPreview.SetImageBitmap(renderSvg(result.ImageGraphics, result.Image)); } else { if (result.ImageGraphics != null) { ivPreview.SetImageBitmap(renderSvg(result.ImageGraphics, ivPreview.Width, ivPreview.Height)); } else ivPreview.SetImageBitmap(null); } } isScanning = false; btnScan.Text = "START SCANNING"; resultListAdapter.NotifyDataSetChanged(); }
结果.Image.是扫描过程中的最后一帧,它将显示在ivPreviewImageView,和result.ImageGraphics是用于在图像上定位条形码的SVG图像。
在里面ScannerActivity我们覆盖了OnPause和调用OnStop事件,这样我们就可以断开连接和StopAvailabilityListening当我们从这个活动中导航或破坏这个活动时,释放所有的连接。
protected override void OnPause() {base.OnPause();if (readerDevice != null) {readerDevice. disconnect ();}} protected override void OnStop() {if (readerDevice != null) try {readerDevice. stopavailabilitylistening ();}(系统。异常e) {} listeningForUSB = false;base.OnStop ();}
请记住,在某些情况下,设备可能会由于电池电量不足或手动断开电缆而断开。
如果您计划使用CMBSDK.要使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,但扫描结果会被模糊化(SDK会用星号字符随机替换扫描结果中的字符)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
获得许可密钥后,有两种方式在应用程序中添加许可密钥。
第一个是在清单文件中将其添加为Application标记中的元标记:
< android应用程序:label = " XamarinDataManSample " >… . android:value="YOUR_MX_MOBILE_LICENSE"/>
第二种方法是在连接到设备之前直接从代码中实现激活:
... //readerdevice.setcameraregistrationKey ("your_mx_mobile_license“);readerDevice.Connect(这个);
扫描成功后,请使用以下代码添加“哔”声:
它在MX设备上播放音频信号。
连接和断开MX设备的过程是通过Connect/Disconnect方法完成的
将尝试连接到设备。当。。。的时候连接()执行我们得到的动作状态OnConnection已完成侦听器
OnConnectionCompleted(ReaderDevice reader,Throwable error){if(error != null){//处理错误readerDisconnected();} }
将从MX设备断开。
例子:
readerDevice=readerDevice.GetMXDevice(mContext);readerDevice.StartAvailabilityListening();readerDevice.SetReaderDeviceListener(此);readerDevice.EnableImage(真);readerDevice.Connect(此)。。。
AvailabilityChanged(ReaderDevice reader){if(reader.GetAvailability()==Availability.Availability){ReaderDevice.Connect(this);}。。。
从一个成功的扫描结果可以返回一个图像。这是在成功扫描中解决的最后一个帧。要启用/禁用此功能,我们可以使用API方法:
ReaderDevice = ReaderDevice.getPhoneCamerAdevice(Mcontext,CamerAmode.NoAimer,PreviewOption.Defaults,RLMainContainer);readerDevice.EnableImage(真正的);readerDevice.Connect(这个);
成功扫描的结果可以返回SVG图像图形。这是在成功扫描中解决的最后一个帧。要启用/禁用此功能,我们可以使用API方法:
ReaderDevice = ReaderDevice.getPhoneCamerAdevice(Mcontext,CamerAmode.NoAimer,PreviewOption.Defaults,RLMainContainer);readerDevice.EnableImageGraphics(真正的);readerDevice.Connect(这个);
在我们可以连接到MX设备之前,我们需要知道是否有一个可用的任务。
它返回可用性对象可以是可用,不可用或未知的
一个MX设备可用时,有一个USB连接到我们的智能手机。
AvailabilityChanged(ReaderDevice reader){if(reader.GetAvailability()==Availability.Availability){ReaderDevice.Connect(this);}。。。
如果我们想检查MX设备的电池水平,我们可以使用
中的输入参数检索阅读器设备的当前电池百分比水平OnDeviceBatteryLevelReceived侦听器方法
公共无效OnConnectionStateChanged(ReaderDevice读取器){如果(reader.ConnectionState == ConnectionState.Connected){reader.GetDeviceBatteryLevel(本);.........}}公共无效OnDeviceBatteryLevelReceived(ReaderDevice P0,P1 INT,Throwable的P2){INT batteryLevel = P1;}
如果我们要检查MX设备的所有灯是否都开着或关着
检索读数装置的灯是否作为输入参数打开或关闭韩国符合要求侦听器方法
打开或关闭MX设备灯
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.IsLightsOn(this); ......... } } public void OnLightsOnCompleted(ReaderDevice p0, Java.Lang.Boolean p1, Throwable p2) { bool lightsON = p1.BooleanValue(); }
如果我们想要检查一些符号是否被启用或禁用,我们可以使用
检索Symbology是否已启用或禁用作为输入参数OnSymbologyEnabled侦听器方法
为了启用我们使用的特定符号
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.IsSymbologyEnabled(Symbology.Azteccode, this); ......... } } public void OnSymbologyEnabled(ReaderDevice p0, Symbology p1, Java.Lang.Boolean p2, Throwable p3) }
将MX Device配置设置重置为默认使用
在OnResetConfigCompleted我们可以通过检查Throwable输入参数error是否为null来检查重置是否成功
public void onresetconfigcompleted(ReaderDevice P0,Rockable P1){if(p1!= null)控制台.writeline(“重置不成功。错误:”+ p1.message);}
注册侦听器函数onavailabilityChanged,OnConnectionStateChanged和OnReadResultReceived我们需要在尝试连接到读取器设备之前调用此方法
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);} }
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);} else if (read_state == read_state . connected){/ /连接状态ConnectionState == ConnectionState. disconnected) {}}
已接收的ReadResultReceived(ReaderDevice reader,ReadResults results){if(results.Count>0){ReadResult result=results.GetResultStat(0);if(result.IsGoodRead){。。。
例子
ReaderDevice = ReaderDevice.getPhoneCamerAdevice(Mcontext,CamerAmode.NoAimer,PreviewOption.Defaults,RLMainContainer);readerDevice.SetReaderDeviceListener(这个);readerDevice.Connect(这个);
我们称之为后readerDevice。SetReaderDeviceListener (IReaderDeviceListener listenerObject)方法和注册onavailabilityChanged监听器函数,我们可以启动/停止可用性监听
将开始侦听读取器设备可用性,并每次更改可用性时触发侦听器功能
是否会停止收听阅读器设备的可用性
例子:
ReaderDevice = ReaderDevice.getMxDevice(Mcontext);ReaderDevice.StartaMailabilityListening();readerDevice.SetReaderDeviceListener(这个);readerDevice.Connect(这个);
受保护的重写void Dispose(bool disposing){if(readerDevice!=null&&readerDevice.ConnectionState==ConnectionState.Connected){readerDevice.SetReaderDeviceListener(null);readerDevice.Disconnect();}if(readerDevice!=null)请尝试{readerDevice.StopAvailabilityListening();}捕获(System.Exception e){}基本处理(disposing);}
在我们连接和配置读者设备后,并设置我们需要的所有设置,我们可以开始扫描过程。
开始扫描时,我们使用方法
当我们的阅读器成功扫描某些条码时,扫描过程将停止,或者我们可以用这种方法手动停止扫描
当扫描停止时,无论是成功扫描还是使用stopscan()方法OnReadResultReceived在我们可以检查扫描结果的地方将调用Listener函数
公共无效OnReadResultReceived(ReaderDevice读卡器,ReadResults结果){如果(results.Count> 0){ReadResult结果= results.GetResultAt(0);如果(result.IsGoodRead){符号符号= result.Symbology;如果(符号!= NULL){tvSymbology.Text = sym.Name;}否则{tvSymbology.Text = “UNKNOWN SYMBOLOGY”;} tvCode.Text = result.ReadString;}否则{tvSymbology.Text = “NO READ”;tvCode.Text = “”;} ivPreview.SetImageBitmap(result.Image);} }
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio并遵循以下步骤:
1.去文件->新建->项目。
2.创建空白应用程序(iPhone)。
当你的新项目加载加载参考XamarinDataManLibrary.dll文件。
然后打开Project属性并添加我们使用的框架额外mtouch参数:
接下来打开info.plist.根据您的需要(应用程序名称、部署目标、主界面等)归档并设置一些项目属性。
这里重要的是为这个应用程序添加摄像头权限。在Visual Studio中没有从这里添加这个权限的选项。你得打开你的info.plist.文件,并添加以下行:
NSCameraUsageDescription 用于扫描的摄像头。
同样,如果你使用MX Device作为读取器,请在Info.plist中添加以下协议:
<键> UISupportedExternalAccessoryProtocols < /关键> <数组> <字符串> com.cognex。dmcc <字符串> / <字符串> com.demo.data字符串> < / > < /数组
的ViewController将是我们的第一个控制器Main.storyboard.在这里,我们正在创建将在此控制器中用到的UI元素和变量。
公共部分类ViewController:UIViewController,ICMBreaderDevictiCeLegate {受保护的ViewController(Intptr Handl):基础(句柄){//注意:此.ctor不应包含任何初始化逻辑。CMBreaderDevice ReaderDevice;公共BOOL ISSCANNING = FALSE;公共覆盖void ViewDidLoad(){base.viewDidload();......
lblConnection-为当前连接状态标记UI元素。
txtResult- TextView UI元素的结果,将读取扫描条形码。
btnScan-按钮UI元素将触发startscanning.或StopScanning.
ivPreview-ImageView.元素,用于显示预览或扫描会话的最后一帧。
CMBReaderDevicecmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置。
这里我们重写那些有点方法来配置读取器设备对象。
如果我们想使用MX设备进行扫描
readerDevice = CMBReaderDevice.ReaderOfMXDevice ();
MX设备的可用性可能会在设备打开或关闭时发生变化,或者USB电缆连接或断开时发生变化,并由IcmbreaderdevictionElegate界面。我们将此接口设置为读取器设备的属性
readerdevice.weakdelegate =这个;
让我们来听听这三件事:
公共无效DidReceiveReadResultFromReader(CMBReaderDevice读者,CMBReadResults readResults)公共无效AvailabilityDidChangeOfReader(CMBReaderDevice读者)公共无效ConnectionStateDidChangeOfReader(CMBReaderDevice读者)
如果我们想将读取器配置为移动相机。
readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode (CDMCameraMode。NoAimer CDMPreviewOption。违约,ivPreview);
的CameraMode参数是类型CDMCameraMode,它接受下列值之一:
所有上述模式都为读者提供了以下默认设置:
基于所选模式,设置了以下附加选项和行为:
的previewOptions类型的参数(CDMPreviewOption)用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项如下:
类型的最后一个参数UIView是可选的,用作相机预览的容器。如果参数为空,则将使用全屏预览。
配置后ReaderDevice我们需要连接到设备。
readerDevice.ConnectWithCompletion((error) => {if (error != null) {new UIAlertView("Failed to connect", error. ");Description, null, "OK", null).Show();}});
如果在尝试连接时出现错误,则会在回调函数中抛出错误作为参数。如果一切正常,错误参数将为空。
这个函数将触发ConnectionStateDidChangeOfReader方法。如果连接成功Reader.connectionState == ConnectionState.Connected。
成功连接后,我们可以为ReaderDevice设置一些设置。ReaderDevice设置可以使用已包装的函数进行设置,也可以直接通过向配置的设备发送命令进行设置。
例如,如果手机相机用作ReaderDevice有无符号启用默认.属性中使用的符号必须启用SetSymbology包装的功能。
在这个例子中,我们启用一些符号和一套设置,从扫描得到的最后一帧ivPreview ImageView.
readerDevice.SetSymbology(CMBSymbology.DataMatrix,true,(error)=>{if(error!=null){System.Diagnostics.Debug.WriteLine(“FALIED启用[DataMatrix],”,error.LocalizedDescription);});readerDevice.SetSymbology(CMBSymbology.Qr,true,(error)=>{if(error!=null){System.Diagnostics.Debug.WriteLine(“FALIED启用[Qr],”,error.LocalizedDescription);});readerDevice.SetSymbology(CMBSymbology.C128,true,(error)=>{if(error!=null){System.Diagnostics.Debug.WriteLine(“FALIED启用[C128],”,error.LocalizedDescription);});readerDevice.SetSymbology(CMBSymbology.UpcEan,true,(error)=>{if(error!=null){System.Diagnostics.Debug.WriteLine(“FALIED启用[UpcEan],”,error.LocalizedDescription);});readerDevice.ImageResultEnabled=true;readerDevice.SVGResultEnabled=true;readerDevice.DataManSystem.SendCommand(“设置图像大小0”);
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startscanning.方法从你ReaderDevice对象
接下来发生的事情是基于阅读器设备的类型和它是如何被配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults.在迭代的结果集合对象ReaderDevice侦听器方法。
public void DidReceiveReadResultFromReader(CMBReaderDevice reader, CMBReadResults readResults) {btnScan。SetTitle(“开始扫描”,UIControlState.Normal);isScanning = false;tableData.RemoveAllObjects ();如果(readResults。= null && readResults.SubReadResults. length > 0) {tableData.AddObjects(readResults.SubReadResults);tvResults.ReloadData ();} else if (readResults.ReadResults.){tableData.Add(readResults.ReadResults[0]);} tableSource.SetItems (tableData);tableSource.displayResult (0); tvResults.ReloadData(); tvResults.SelectRow(NSIndexPath.FromRowSection(0,0), false, UITableViewScrollPosition.None); }
可能会出现设备因电池电量不足或手动断开电缆而断开的情况。这些病例可以被ConnectionStateDidChangeOfReader回调IcmbreaderdevictionElegate.
注:的AvailabilityDidChangeOfReader当设备变得身体不可用时,还调用方法。这意味着(重新)连接是不可能的。始终检查可用性的属性ReaderDevice在试图调用之前的对象connectwithcompletion.方法。
如果您计划使用CMBSDK使用智能手机或平板电脑进行移动扫描(没有MX Mobile终端),则SDK需要安装许可证密钥。如果没有许可证密钥,则SDK仍将运行,尽管扫描结果将被混淆(SDK将随机替换扫描结果中的字符与星号字符)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
获得许可证密钥后有两种方式来添加应用程序许可证密钥。
第一个方法是将它作为一个键添加到特定于项目的值中info.plist.文件:
MX_MOBILE_LICENSE 您的许可密钥 . txt
第二种实现激活的方法是直接从你连接到设备之前的代码:
....readerdevice.weakdelegate =这个;/ / CMBReaderDevice.SetCameraRegistrationKey(“YOUR_MX_MOBILE_LICENSE”);connectToReaderDevice ();
在portable项目中,我们创建一个页面(主页)我们为扫描页面和一个类(camerapreview.cs)创建了一个playout,它将继承自视图控件并具有一些属性和事件。
CameraPreview控制主页从后面的代码添加OnAppearing方法,这样我们就可以设置一些公共属性ResultReceived和connectionstatechanged.事件。
保护覆盖无效OnAppearing(){base.OnAppearing();如果(cameraPreview == NULL){cameraPreview =新CameraPreview();//你可以在这里选择设备,或者您也可以在每次出现这个页面上进行选择。缺省值是SelectFromList cameraPreview.SelectedDevice = ScanningDevice.MobileCamera;//预览启用。默认值为true //cameraPreview.ScanningPreviewEnable = FALSE;//事件接收到时的结果cameraPreview.ResultReceived + = CameraPreview_ResultReceived将被触发;//事件时的连接状态将被改变cameraPreview.ConnectionStateChanged + = CameraPreview_ConnectionStateChanged将被触发;在此内容gridCamera.Children.Insert(0,cameraPreview)//添加这种控制;/ *如果您在导航页面中使用扫描仪和您在导航堆栈从这个网页像Navigation.PushAsync添加另一个页面(在那里你会再次使用扫描仪控制)*(新AnotherPage()),请您浏览之前使用此代码* / //如果(cameraPreview!= NULL)// {// cameraPreview.ResultReceived - = cameraPreview_ResultReceived; // cameraPreview.ConnectionStateChanged -= cameraPreview_ConnectionStateChanged; // gridCamera.Children.Remove(cameraPreview); // cameraPreview = null; //} //Navigation.PushAsync(new AnotherPage()); } }
在安卓和iOS我们有具体的平台项目自定义渲染器(ScannerView.cs),我们可以在可移植项目中使用本地元素。
ViewRenderer在Android平台具体项目:
受保护的覆盖void onelementChanged(ElementChangedEventargs E){Base.OneLementChanged(E);if (e.o oldelement != null || Element == null) {return;} rlMainContainer = new RelativeLayout(Context);...MainActivity.selfActivity。setActiveReader(控制元素);}
在MainActivity在Android平台特定的项目中,我们处理的是reader设备对象(几乎是一样的ScannerActivity为Xamarin.android.)从自定义渲染器中,我们只需调用setActiveReader方法和传递控件(本机RelativeLayout元素)和元素(CameraPreview对象,该对象从主页初始化)。
ViewRenderer在iOS平台具体项目:
受保护的覆盖void onelementChanged(ElementChangedEventargs E){Base.OneLementChanged(E);if (e.o oldelement != null || Element == null) {return;container = new uiview();ivpreview = new uiimageview();IVPREVIEW.CONTENTMODE = UIVIEWCONTENTMODE.SCALETOFILL;...... appdelegate.selfdelegate.setactiveReader(控制,元素);}
在AppDelegate在iOS平台特定项目中,我们使用读者设备对象处理(几乎相同查看控制器为沙马林从“自定义渲染器”中,我们只需调用SetActivereader方法和传递控制(本机UIImageView元素)和元素(从主页初始化的CamerApreview对象)。
配置读取设备、连接设备、扫描条码和断开从设备是一样的,你可以在Xamarin.android.和沙马林部分。