Xamarin的独特之处在于它只提供了一种语言——c#、类库和运行时,这些语言可以在iOS、Android和Windows Phone这三个移动平台上运行(Windows Phone的原生语言已经是c#了),同时仍然可以编译本地(非解释的)应用程序,这些应用程序的性能甚至可以满足高要求的游戏。
这些平台都有不同的特性集,并且在编写本机应用程序的能力上也各不相同——也就是说,将应用程序编译为本机代码并与底层Java子系统流畅地进行互操作。例如,有些平台只允许用HTML和JavaScript构建应用程序,而有些平台非常低级,只允许使用C/ c++代码。有些平台甚至不使用本机控件工具包。
Xamarin的独特之处在于,它结合了本地平台的所有功能,并添加了自己的一些强大功能,包括:
Xamarin提供两种商业产品:Xamarin.iOS和Xamarin.Android。它们都建在屋顶上单声道,一个基于已发布的。net ECMA标准的。net框架的开源版本。Mono出现的时间几乎和。net框架本身一样长,可以在几乎所有可以想象的平台上运行,包括Linux、Unix、FreeBSD和Mac OS X。
在iOS, Xamarin的提前(AOT)编译器将Xamarin.iOS应用程序直接编译为本机ARM汇编代码。在Android上,Xamarin的编译器编译到中间语言(伊尔),然后即时(准时制)在应用程序启动时编译到本机程序集。
在这两种情况下,Xamarin应用程序都利用了一个运行时来自动处理诸如内存分配、垃圾收集、底层平台互操作等事情。
Forms是一个允许开发人员快速创建跨平台用户界面的框架。它为将使用iOS、Android、Windows或Windows Phone上的本机控件呈现的用户界面提供了自己的抽象。这意味着应用程序可以共享大部分用户界面代码,并且仍然保留目标平台的本地外观。
Xamarin的。形式allows for rapid prototyping of applications that can evolve over time to complex applications. Because Xamarin.Forms applications are native applications, they don't have the limitations of other toolkits such as browser sandboxing, limited APIs, or poor performance. Applications are written using Xamarin.Forms are able to utilize any of the API’s or features of the underlying platform, such as (but not limited to) CoreMotion, PassKit, and StoreKit on iOS; NFC and Google Play Services on Android; and Tiles on Windows. In addition, it's possible to create applications that will have parts of their user interface created with Xamarin. Forms while other parts are created using the native UI toolkit.
Xamarin的。形式applications are architected in the same way as traditional cross-platform applications. The most common approach is to use便携式图书馆或共享项目存放共享代码并创建将使用共享代码的特定于平台的应用程序。
在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部分。
设置您的Manifest文件(应用程序名称,应用程序图标,最小和最大android版本),确保启用此应用程序的摄像头权限。
在设置了一些必要的属性之后,我们可以创建贫乏哪一个是最重要的MainLauncher并继承一些接口。
[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元素和变量,稍后将在活动中使用:
连接状态-text查看当前连接状态的UI元素;
tvResult- TextView一个UI元素的结果,将读取扫描条形码;
btnScan按钮一个将触发的UI元素readerDevice。StartScanning或readerDevice。StopScanning;
rlPreviewContainerViewGroup (RelativeLayout)容器用于相机预览;
ivPreview-ImageView控件,其匹配大小作为其父控件,用于显示预览或扫描会话的最后一帧;
读设备cmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置;
每次活动开始时,我们都调用initDevice在这里,我们配置读取器设备对象。
如果我们想使用MX设备进行扫描,我们可以使用:
readerDevice=GetMXDevice(此设备);如果(!listeningForUSB){readerDevice.StartAvailabilityListening();listeningForUSB=true;}
MX设备的可用性可以在设备转动时改变在或从,或者如果USB电缆已连接,或者断开连接,由IReaderDeviceListener接口。
如果我们想将读取设备配置为手机摄像头,我们可以使用:
readerDevice = GetPhoneCameraDevice(this, CameraMode。NoAimer PreviewOption。违约,rlPreviewContainer);
CameraMode参数为typeCameraMode(定义在CameraMode.java),它接受下列值之一:
PASSIVE_AIMER:初始化读卡器,使其使用被动瞄准器。被动瞄准器是附在移动设备或移动设备外壳上的附件,使用移动设备的内置LED闪光灯作为光源投射瞄准模式。在此模式下,设备屏幕上不会显示直播预览,因为会有一个瞄准模式被投射。
前摄像头初始化读取器以使用设备的移动前置摄像头,如果可用(不是所有移动设备都有前置摄像头)。这是一种不寻常但可能的配置。大多数前置摄像头没有自动对焦和照明,提供的图像分辨率明显较低。这个选项应该小心使用。在此模式下,照明不可用。
以上所有模式都为读者提供了以下默认设置:
根据所选模式,设置以下附加选项和行为:
的PreviewOption的类型PreviewOption(定义在PreviewOption.java),并用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode. 传递参数时,可以通过指定或取消指定多个选项。可供选择的方案有:
配置后读设备我们需要连接到设备上。
在我们接通之前ReaderDeviceListener对象的设置以接收事件:
readerDevice.SetReaderDeviceListener(这个);
此外,您可以通过以下方式从阅读器发送最后触发的图像:
readerDevice.EnableImage(true);
然后我们可以连接到:
readerDevice.Connect(这个);
将调用的事件包括:
public void OnConnectionStateChanged(ReaderDevice reader)
如果在尝试连接时出错,该错误将作为OnConnectionCompleted方法,否则,如果未发生错误,则错误参数将为零.
如果连接成功,则语句读者。ConnectionState = = ConnectionState。连接会是真的。
有两个API方法用于更改一些公共属性以配置连接的设备,您应该在ConnectionState是连接。
例如,如果移动相机被用作读设备有默认情况下没有启用符号. 必须启用要与一起使用的符号SetSymbologyEnabledAPI方法:
readerDevice.SetSymbologyEnabled(象征。C128,真的,null);readerDevice.SetSymbologyEnabled(象征。Datamatrix,真的,null);readerDevice.SetSymbologyEnabled(象征。UpcEan,真的,null);readerDevice.SetSymbologyEnabled(象征。Qr,真的,null);
你可以通过以下方式直接向连接的设备发送命令:
readerDevice.DataManSystem。SendCommand(“设置标志。MICROPDF417”);
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startScanning方法从你读设备对象。
接下来发生的事情是基于读设备以及它是如何配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults中的iterable结果集合对象读设备侦听器方法。
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(); }
结果。图像扫描过程中的最后一帧是否会显示在ivPreviewImageView,和结果。图像Graphics是在图像上定位条形码的SVG图像。
在贫乏我们覆盖OnPause和原这样我们就可以断开和StopAvailabilityListening当我们从这个活动中导航或破坏这个活动时,释放所有的连接。
受保护的覆盖void OnPause(){base.OnPause();if(readerDevice!=null){readerDevice.Disconnect();}}}受保护的覆盖void OnStop(){if(readerDevice!=null)尝试{readerDevice.stopAvailabilityListing();}catch(System.Exception e){}listeningForUSB=false;base.OnStop();}
请记住,可能存在由于电池电量不足或手动断开电缆而导致设备断开的情况。
如果你打算使用cmbSDK要使用智能手机或平板电脑(不使用MX移动终端)进行移动扫描,SDK需要安装许可证密钥。如果没有许可证密钥,SDK仍将运行,尽管扫描结果会模糊(SDK将随机将扫描结果中的字符替换为星号字符)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
获得许可密钥后,有两种方式在应用程序中添加许可密钥。
第一个是将它作为一个元标签添加到manifest文件中的application标签中:
<元数据android:name=“MX\u MOBILE\u许可证”android:value=“您的\u MX\u MOBILE\u许可证”/>
第二种方法是在连接到设备之前直接从代码中实现激活:
.../ / ReaderDevice.SetCameraRegistrationKey(“YOUR_MX_MOBILE_LICENSE”);readerDevice.Connect(这个);
扫描成功后,请使用以下代码添加“哔”声:
它在MX设备上播放音频信号。
连接和断开MX设备的过程是通过Connect/Disconnect方法完成的
将尝试连接到设备。当Connect ()执行时,我们得到行动的状态OnConnectionCompleted听众
OnConnectionCompleted(ReaderDevice reader,Throwable error){if(error != null){//处理错误readerDisconnected();} }
将从MX设备断开连接。
例子:
readerDevice = ReaderDevice.GetMXDevice (mContext);readerDevice.StartAvailabilityListening ();readerDevice.SetReaderDeviceListener(这个);readerDevice.EnableImage(真正的);readerDevice.Connect(这个);...
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);}……
成功扫描的结果可以返回一个图像。这是在一次成功的扫描中解析的最后一帧。要启用/禁用这个功能,我们可以使用API方法:
readerDevice = readerDevice。GetPhoneCameraDevice (mContext CameraMode。NoAimer PreviewOption。违约,rlMainContainer); readerDevice.EnableImage(true); readerDevice.Connect(this);
成功扫描的结果可以返回一个SVG图像图形。这是在一次成功的扫描中解析的最后一帧。要启用/禁用这个功能,我们可以使用API方法:
readerDevice = readerDevice。GetPhoneCameraDevice (mContext CameraMode。NoAimer PreviewOption。违约,rlMainContainer); readerDevice.EnableImageGraphics(true); readerDevice.Connect(this);
在连接到MX设备之前,我们需要知道是否有一个设备可用于该任务。
它回来了可用性对象可以是可用的,不可用的或未知的
当有USB连接到我们的智能手机时,MX设备是可用的。
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);}……
如果我们想检查MX设备的电池电量,我们可以使用
中的输入参数检索阅读器设备的当前电池百分比水平OnDeviceBatteryLevelReceived侦听器方法
public void OnConnectionStateChanged(ReaderDevice reader) {if (ReaderDevice reader);ConnectionState = = ConnectionState。连接){ reader.GetDeviceBatteryLevel(this); ......... } } public void OnDeviceBatteryLevelReceived(ReaderDevice p0, int p1, Throwable p2) { int batteryLevel = p1; }
如果我们要检查MX设备的所有灯是否都开着或关着
检索读数装置的灯是否作为输入参数打开或关闭OnLightsOnCompleted侦听器方法
我们用它来打开或关闭MX设备的灯
连接状态已更改(ReaderDevice reader){if(reader.ConnectionState==ConnectionState.Connected){reader.IsLightsOn(this);…}}公共无效OnLightsOnCompleted(ReaderDevice p0,Java.Lang.Boolean p1,Throwable p2){bool lightsON=p1.BooleanValue();}
如果我们想要检查一些符号是否被启用或禁用,我们可以使用
检索符号是否作为输入参数启用或禁用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, Throwable p1) {if (p1 !WriteLine(“重启成功。Error: " + 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) {}}
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {if (result . result, result . result);Count > 0) {ReadResult result = results.GetResultAt(0);if (result.IsGoodRead){…
例子
readerDevice=readerDevice.GetPhoneCameraDevice(mContext、CameraMode.NoAimer、PreviewOption.Defaults、rlMainContainer);SetReaderDeviceListener(此文件);readerDevice.Connect(此);
我们打电话之后readerDevice。SetReaderDeviceListener (IReaderDeviceListener listenerObject)方法和注册OnAvailabilityChanged侦听器功能我们可以启动/停止可用性侦听
将启动侦听阅读器设备可用性,并将触发侦听器功能时,每次可用性更改
将停止侦听读卡器设备的可用性
例子:
readerDevice = ReaderDevice.GetMXDevice (mContext);readerDevice.StartAvailabilityListening ();readerDevice.SetReaderDeviceListener(这个);readerDevice.Connect(这个);
{if (readerDevice != null && readerDevice. value = null, readerDevice. value = null;ConnectionState = = ConnectionState。连接){ readerDevice.SetReaderDeviceListener(null); readerDevice.Disconnect(); } if (readerDevice != null) try { readerDevice.StopAvailabilityListening(); } catch (System.Exception e) { } base.Dispose(disposing); }
在我们连接和配置我们的阅读器和设置所有我们需要的设置之后,我们可以开始扫描过程。
开始扫描时,我们使用方法
当我们的阅读器成功扫描某些条形码时,扫描过程将停止,或者我们可以使用此方法手动停止扫描
停止扫描时,无论是使用成功扫描还是使用stopScanning()方法OnReadResultReceived将调用侦听器函数,我们可以在其中检查扫描结果
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {if (result . result, result . result);Count > 0) {ReadResult result = results.GetResultAt(0);if (result.IsGoodRead) {symbol sym = result.Symbology;if (sym != null){电视符号。文本= sym.Name;} else{电视符号。Text = "未知符号";} tvCode。文本= result.ReadString;} else{电视符号。Text = "NO READ";tvCode。文本= " "; } ivPreview.SetImageBitmap(result.Image); } }
在下面的部分中,我们将逐步解释我们的示例应用程序是如何开发的。
打开Visual Studio并遵循以下步骤:
1.去文件->新建->项目。
2.创造空白应用程序(iPhone)。
加载新项目时,将引用添加到XamarinDataManLibrary.dll文件。
然后打开项目属性并添加我们用作额外mtouch参数:
你的下一个开放Info.plist根据你的需要文件和设置一些项目属性(应用程序名称,部署目标,主界面等)。
这里重要的是为这个应用程序添加摄像头权限。在Visual Studio中没有从这里添加这个权限的选项。你得打开你的Info.plist在某些文本编辑器中创建文件并添加以下行:
NSCameraUsageDescription 用于扫描的摄像机
同样,如果你使用MX Device作为读取器,请在Info.plist中添加以下协议:
UISupportDexternalAccessoryProtocols com.cognex.dmcc com.demo.data
的ViewController将是我们的第一个控制器Main.storyboard.这里我们创建了一些UI元素和变量,稍后将在这个控制器中使用。
public partial class ViewController: UIViewController, ICMBReaderDeviceDelegate {protected ViewController(IntPtr句柄):base(句柄){//注意:这个.ctor不应该包含任何初始化逻辑。} CMBReaderDevice readerDevice;public bool isScanning = false;public override void ViewDidLoad() {base.ViewDidLoad();...
lblConnection-为当前连接状态标记UI元素。
txtResult-将从扫描的条形码中读取的结果的TextView UI元素。
btnScan-按钮UI元素将触发StartScanning或StopScanning.
ivPreview-ImageView元素,用于显示预览或扫描会话的最后一帧。
CMBReaderDevicecmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置。
这里我们重写视图将出现方法来配置读取器设备对象。
如果我们想使用MX设备进行扫描,我们正在使用
readerDevice = CMBReaderDevice.ReaderOfMXDevice ();
MX Device的可用性可以在设备打开或关闭时改变,或者如果USB线连接或断开,并由ICMBReaderDeviceDelegate接口。我们将此接口设置为读取器设备的属性
readerDevice。WeakDelegate =;
让我们来听听这三个事件:
public void DidReceiveReadResultFromReader(CMBReaderDevice reader, CMBReadResults readResults) public void AvailabilityDidChangeOfReader(CMBReaderDevice reader) public void ConnectionStateDidChangeOfReader(CMBReaderDevice reader)
如果我们想将读卡器设备配置为移动摄像机。
readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode (CDMCameraMode。NoAimer CDMPreviewOption。违约,ivPreview);
的CameraMode参数的类型Cdmameramode,它接受下列值之一:
以上所有模式都为读者提供了以下默认设置:
根据所选模式,设置以下附加选项和行为:
的预览选项类型的参数(CDMPreviewOption)用于更改读取器的默认值或覆盖从选定对象派生的默认值CameraMode. 传递参数时,可以通过指定或取消指定多个选项。可用选项如下:
类型的最后一个参数UIView是可选的,用作摄影机预览的容器。如果参数保留为零,则将使用全屏预览。
配置后读设备我们需要连接到设备上。
readerDevice.ConnectWithCompletion((error) => {if (error != null) {new UIAlertView("Failed to connect", error. ");Description, null, "OK", null).Show();} });
若在尝试连接时出现错误,则错误将作为回调函数中的参数抛出。若一切正常,错误参数将为空。
此函数将触发ConnectionStateDidChangeOfReader方法。如果连接成功读者。ConnectionState = = ConnectionState.Connected。
连接成功后,我们可以为ReaderDevice设置一些设置。ReaderDevice设置可以通过已经封装的函数来设置,也可以直接通过向配置的设备发送命令来设置。
例如,如果移动相机被用作reader设备有默认情况下没有启用符号. 必须启用要与一起使用的符号SetSymbology包装函数。
在这个例子中,我们启用了一些符号,并设置了设置来获取扫描进来的最后一帧预览图像视图.
readerDevice.SetSymbology (CMBSymbology。DataMatrix, true, (error) => {if (error != null) {System.Diagnostics.Debug. if (error !WriteLine("FALIED TO ENABLE [DataMatrix], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。Qr, true, (error) => {if (error != null) {System.Diagnostics.Debug. if (error !WriteLine("FALIED TO ENABLE [Qr], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。C128, true, (error) => {if (error != null) {System.Diagnostics.Debug. C128, true, (error) => {WriteLine("FALIED TO ENABLE [C128], ", error.LocalizedDescription);} });readerDevice.SetSymbology (CMBSymbology。UpcEan, true, (error) => { if (error != null) { System.Diagnostics.Debug.WriteLine("FALIED TO ENABLE [UpcEan], ", error.LocalizedDescription); } }); readerDevice.ImageResultEnabled = true; readerDevice.SVGResultEnabled = true; readerDevice.DataManSystem.SendCommand("SET IMAGE.SIZE 0");
有了正确配置的阅读器,现在就可以扫描条形码了。可以通过调用startScanning方法从你读设备对象。
接下来发生的事情是基于阅读器设备的类型和它是如何被配置的,但一般来说:
在下列情况之一时,扫描停止:
当条形码被成功解码时(第一种情况),您将收到一个ReadResults中可迭代的结果集合对象读设备侦听器方法。
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回调的ICMBReaderDeviceDelegate.
注意:的AvailabilityDidChangeOfReader方法在设备物理上不可用时也会被调用。这意味着(重新)连接是不可能的。的可用性属性读设备对象之前,尝试调用ConnectWithCompletion方法。
如果您计划使用cmbSDK来使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,那么该SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,尽管扫描结果会被混淆(SDK会用星号字符随机替换扫描结果中的字符)。
请与您的康耐视销售代表联系,了解如何获取许可密钥,包括可用于评估SDK 30天的试用许可。
在获得许可密钥后,有两种方式在应用中添加许可密钥。
第一个方法是将它作为一个键添加到特定于项目的值中Info.plist文件:
MX_MOBILE_LICENSE 您的许可密钥 . txt
第二种实现激活的方法是直接从你连接到设备之前的代码:
.... readerDevice.WeakDelegate=this//CMBReaderDevice.SetCamerarRegistrationKey(“您的移动许可证”);连接到读卡器设备();
在可移植项目中,我们创建一个页面(主页),我们创建一个布局扫描页面和一个类(CameraPreview.cs),它将继承从视图控件,并有一些属性和事件。
摄影评论控制主页是从后面的代码中添加的OnAppearing方法,以便设置一些公共属性并结果收到和ConnectionStateChanged事件。
protected override void onappearance () {base. onappearance ();if (cameraPreview == null) {cameraPreview = new cameraPreview ();//你可以从这里选择设备,或者你可以选择在这个页面上出现的每一个。默认是SelectFromList cameraPreview。SelectedDevice = ScanningDevice.MobileCamera;/ /预览启用。默认为true。ScanningPreviewEnable = false;//当收到result时触发的事件ResultReceived + = CameraPreview_ResultReceived;//当连接状态改变时触发的事件ConnectionStateChanged + = CameraPreview_ConnectionStateChanged;//在内容gridCamera.Children中添加这个控件。插入(0,cameraPreview); /*If you use scanner in navigation page and you add another page in navigation stack(where you will use scanner control again) * from this page like Navigation.PushAsync(new AnotherPage()) please use this code before you navigate */ //if (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平台特定项目中:
OnElementChanged(ElementChangedEventArgs e) {base.OnElementChanged(e);if (e.o oldelement != null || Element == null) {return;} rlMainContainer = new RelativeLayout(Context);...MainActivity.selfActivity。setActiveReader(控制元素);}
在MainActivity在Android平台特定的项目中,我们处理的是reader设备对象(几乎是一样的贫乏对于Xamarin的。安卓),从自定义渲染器,我们只需调用setActiveReader方法,并传递Control(原生RelativeLayout元素)和element(从MainPage初始化的CameraPreview对象)。
ViewRenderer在iOS平台特定项目中:
OnElementChanged(ElementChangedEventArgs e) {base.OnElementChanged(e);if (e.o oldelement != null || Element == null) {return;} container = new UIView();ivPreview = new UIImageView();ivPreview。内容模式= UIViewContentMode.ScaleToFill;......AppDelegate.selfDelegate。setActiveReader(控制元素);}
在AppDelegate在iOS平台特定的项目中,我们处理的是reader设备对象(几乎是一样的视图控制器对于Xamarin.iOS),从自定义渲染器,我们只是调用setActiveReader方法,并传递Control(原生UIImageView元素)和element(从MainPage初始化的CameraPreview对象)。
配置ReaderDevice、连接到设备、扫描条形码和断开从设备是一样的,你可以在Xamarin的。安卓和Xamarin.iOS部分。