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 Framework本身并在几乎每个可想象的平台上运行,包括Linux,Unix,FreeBSD和Mac OS X.
在iOS上,Xamarin的提前时间(AOT编译器编译Xamarin。iOS.applications 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.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.forms.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版本),确保启用此应用程序的摄像头权限。
在我们设置了一些必要的属性之后,我们可以创建ScannerActivity也就是MainLauncher,并继承一些接口。
[活性(标签= “@串/ APP_NAME”,MainLauncher =真,图标= “@绘制/ ic_launcher”)] public类ScannerActivity:活动,IOnConnectionCompletedListener,IReaderDeviceListener,Android.Support.V4.App.ActivityCompat.IOnRequestPermissionsResultCallback {私有静态INT REQUEST_PERMISSION_CODE = 12322;私人TextView的tvConnectionStatus,tvResult;个人按钮btnScan;私人的RelativeLayout rlPreviewContainer;私人ImageView的ivPreview;私人布尔isScanning = FALSE;私有静态ReaderDevice readerDevice;私人枚举设备类型{MX,PHONE_CAMERA}私人静态布尔isDevicePicked = FALSE;私有静态设备类型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;
rlPreviewContainer- 用于相机预览的ViewGroup(RelativeLayout)容器;
ivPreview- ImageView控件,匹配大小作为其父控件,显示预览或扫描会话的最后一帧;
ReaderDevicecmbSDK对象,它将呈现MX设备或手机摄像头取决于我们的配置;
每次活动开始时,我们都调用initDevice在这里,我们配置读取器设备对象。
如果我们想使用MX设备进行扫描,我们可以使用:
readerDevice = GetMXDevice(这个);if (!listeningForUSB) {readerDevice.StartAvailabilityListening();listeningForUSB = true;}
MX设备的可用性可以在设备转动时改变在或离开,或者如果USB线连接或断开连接,这是由处理IReaderDeviceListener接口。
如果我们想读取设备配置为手机相机,我们可以使用:
readerdevice = getphonecameradevice(此,cameramode.noaimer,previewOption.defaults,rlpreviewContainer);
CameraMode参数为type摄影师(定义在CameraMode.java)它是接受以下值之一:
passive_aimer.:初始化读卡器,使其使用被动瞄准器。被动瞄准器是附在移动设备或移动设备外壳上的附件,使用移动设备的内置LED闪光灯作为光源投射瞄准模式。在此模式下,设备屏幕上不会显示直播预览,因为会有一个瞄准模式被投射。
FRONT_CAMERA:初始化读取器以使用设备的移动前置摄像头,如果可用(并非所有移动设备都有前置摄像头)。这是一个不寻常但可能的配置。大多数前面的摄像机没有自动对焦和照明,并提供显着的分辨率图像。应小心使用此选项。在此模式下,照明不可用。
以上所有模式都为读者提供了以下默认设置:
基于所选模式,设置了以下附加选项和行为:
这预览参数是一种类型预览(定义在previewOption.java.),并用于更改读取器的默认值或覆盖从选定对象派生的默认值摄影师.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项有:
配置后ReaderDevice我们需要连接到设备。
在我们建立联系之前ReaderDeviceListener设置对象以便接收事件:
readerDevice.SetReaderDeviceListener(本);
此外,您可以通过以下方式启用从读取器发送最后触发的图像:
readerDevice.EnableImage(真正的);
然后我们可以联系:
ReaderDevice.connect(此);
将被调用的事件有:
公共无效OnConnectionStateChanged(ReaderDevice读者)公共无效OnConnectionCompleted(ReaderDevice读者,Throwable的错误)
如果在尝试连接时出现错误,则该错误将作为参数抛出OnConnectionCompleted方法,否则,如果没有发生错误,则错误参数为零.
如果连接成功,则语句读者。ConnectionState = = ConnectionState。连接会是真的。
有几个API方法改变一些公共属性配置连接的设备,并且当你应该调用它们ConnectionState已连接。
例如,如果移动相机用作ReaderDevice有默认情况下没有启用符号.属性中使用的符号必须启用SetSymbologyEnabledAPI方法:
ReaderDevice.setsymbologyEnabled(symbology.c128,true,null);ReaderDevice.setsymbologyEnabled(symbology.datamatrix,true,null);ReaderDevice.setsymbologyEnabled(symbology.upcean,true,null);ReaderDevice.setsymbologyEnabled(symbology.qr,true,null);
您可以通过向连接的设备发送命令来直接执行相同的操作:
readerDevice.DataManSystem。SendCommand(“设置标志。MICROPDF417”);
有了正确配置的读者,你现在就可以扫描条形码。这可以通过调用来完成startScanning来自你的方法ReaderDevice对象。
接下来会发生什么是基于类型ReaderDevice以及它的配置方式,但通常是:
符合下列条件之一的,停止扫描:
当成功解码条形码(第一个案例)时,您将收到一个ReadResults的可迭代结果集合对象ReaderDevice监听方法。
public void OnReadResultReceived(ReaderDevice reader, ReadResults results) {listViewResultSelectedItem = -1;resultList.Clear ();resultListData.Clear ();ivPreview.SetImageBitmap(空);如果结果。= null && results.SubResults. count > 0) {foreach (ReadResult subResult in results.SubResults) {if (subResult. isgoodread) {resultList.Add(subResult);JavaDictionary(); / /创建新的JavaDictionary项。Add(“resultText subResult.ReadString);symbol = 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,和结果.ImageGraphics.是用于在图像上定位条形码的SVG图像。
在ScannerActivity我们覆盖了这一点oonpause.和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的试用许可证。
获得许可证密钥后,有两种方法来增加您的许可密钥在应用程序中。
第一个是在清单文件中将其添加为Application标记中的元标记:
< android应用程序:label = " XamarinDataManSample " >… . android:value="YOUR_MX_MOBILE_LICENSE"/>
第二种方法是连接到您的设备之前从代码直接实现激活:
... //readerdevice.setcameraregistrationKey ("your_mx_mobile_license“);ReaderDevice.connect(此);
要在成功扫描后添加声音“蜂鸣声”请使用以下代码:
它在MX设备上播放音频信号。
连接和断开到MX设备的过程是通过连接/断开方法进行
将尝试连接到设备。当。。。的时候连接()被执行我们获得了行动的状态OnConnectionCompleted侦听器
OnConnectionCompleted(ReaderDevice Reader,Rockable错误){if(错误!= 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.Defaults,rlMainContainer);readerDevice.EnableImage(真正的);ReaderDevice.connect(此);
成功扫描的结果可以返回SVG图像图形。这是在成功扫描中解决的最后一个帧。要启用/禁用此功能,我们可以使用API方法:
readerDevice = ReaderDevice.GetPhoneCameraDevice(mContext,CameraMode.NoAimer,PreviewOption.Defaults,rlMainContainer);readerDevice.EnableImageGraphics(真);ReaderDevice.connect(此);
在我们可以连接到MX设备之前,我们需要知道是否有一个可用的任务。
它返回可用性对象可以是可用,不可用或未知的
当与我们的智能手机有USB连接时,MX设备可用。
public void OnAvailabilityChanged(ReaderDevice reader) {if (reader. getavailability () == availabilityavailable) {ReaderDevice . connect (this);}……
如果我们想检查MX设备的电池水平,我们可以使用
中的输入参数检索阅读器设备的当前电池百分比水平OnDeviceBatteryLevelReceived侦听器方法
公共void onconnectionstateChanged(ReaderDevice Reader){if(reader.connectionstate == connectionstate.connected){Reader.getDeviceBatterylevel(此);公共void ondevicebatterylevelreceveed(ReaderDevice P0,INT P1,ROLLABLE P2){INT BALKYVEL = P1;}
如果我们要检查的MX设备的所有灯是否开启或关闭
如果阅读器装置的灯被接通或关断作为输入参数中检索OnLightsOnCompleted侦听器方法
打开或关闭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(); }
如果我们想查一些符号被启用或禁用,我们可以使用
检索符号是否作为输入参数启用或禁用OnSymbologyEnabled侦听器方法
为了启用我们使用的特定符号
公共void onconectionstateChanged(ReaderDevice Reader){if(reader.connectionstate == connectionstate.connected){reader.issymbologyEnabled(symbology.azteccode,此);.........}} 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) {}}
public void onreadresultreceived(ReaderDevice Reader,ReadResults结果){if(结果.count> 0){ReadResult结果=结果.getResultat(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.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); }
在我们连接和配置读者设备后,并设置我们需要的所有设置,我们可以开始扫描过程。
开始扫描我们使用方法
当我们的阅读器成功扫描某些条码时,扫描过程将停止,或者我们可以用这种方法手动停止扫描
当扫描停止时,无论是成功扫描还是使用stopscan()方法OnReadResultReceived在我们可以检查扫描结果的地方将调用Listener函数
public void onreadresultreceived(ReaderDevice Reader,ReadResults结果){if(结果.count> 0){ReadResult结果=结果.getResultat(0);if(结果.ISGoodRead){symbology sym = result.symbology;if(sym!= null){townybology.text = sym.name;} else {tvsymbology.text =“未知的符号系统”;tvcode.text =结果.ReadString;} else {tvsymbology.text =“无读”;tvcode.text =“”;ivpreview.setimageBitMap(结果.Image);}}
在下面的小节中,我们将一步步地解释我们的示例应用程序是如何开发的。
打开Visual Studio,并按照此步骤:
1.去文件->新建->项目。
2.创建空白应用程序(iPhone)。
加载新项目时添加参考XamarinDataManLibrary.dll文件。
然后打开Project属性并添加我们使用的框架额外的mtouch参数:
接下来打开你的info.plist.根据你的需要文件和设置一些项目属性(应用程序名称,部署目标,主界面等)。
重要的是,这里为此应用添加相机权限。在Visual Studio中,没有选项可以从此处添加此权限。你需要打开你的info.plist.文件,并添加以下行:
NSCameraUsageDescription 用于扫描的摄像头。
此外,如果你使用MX设备作为阅读设备在Info.plist中添加此协议:
<键> UISupportedExternalAccessoryProtocols < /关键> <数组> <字符串> 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用于显示预览或扫描会话的最后帧的UI元素。
CMBReaderDevice- cmbSDK对象将呈现MX设备或手机相机取决于我们的配置中。
在这里,我们覆盖那些有点方法来配置读取器设备对象时该视图将出现。
如果我们想使用MX设备进行扫描
readerdevice = cmbreaderdevice.readerofmxdevice();
MX Device的可用性可以在设备打开或关闭时改变,或者如果USB线连接或断开,并由IcmbreaderdevictionElegate接口。我们将此接口设置为读取器设备的属性
readerdevice.weakdelegate =这个;
让我们来听听这三个事件:
public void didreceivereadresultfreader(cmbreaderdevice reader,cmbreadresults readresults)public void可用性didchangeofreader(cmbreaderdevice reader)public void connectionstationdofreader(cmbreaderdevice阅读器)
如果我们想将读取器配置为移动相机。
readerDevice = CMBReaderDevice.ReaderOfDeviceCameraWithCameraMode (CDMCameraMode。NoAimer CDMPreviewOption。违约,ivPreview);
这摄影师参数的类型CDMCameraMode,它接受下列值之一:
以上所有模式都为读者提供了以下默认设置:
基于所选模式,设置了以下附加选项和行为:
这previewOptions参数(的类型CDMPreviewOption)用于更改读取器的默认值或从所选的覆盖默认值摄影师.在传递参数时,可以通过OR-ing来指定多个选项。可供选择的选项如下:
类型的最后一个参数的UIView是可选的,用作相机预览的容器。如果参数为空,则将使用全屏预览。
配置后ReaderDevice我们需要连接到设备。
readerDevice.ConnectWithCompletion((错误)=> {如果(误差= NULL){新的UIAlertView中( “无法连接”,error.Description,空, “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. 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来自你的方法ReaderDevice对象。
接下来发生的事情是根据读取器设备的类型,它是如何配置的,但一般:
符合下列条件之一的,停止扫描:
当成功解码条形码(第一个案例)时,您将收到一个ReadResults可迭代的结果集合对象ReaderDevice监听方法。
公共void didreceivereadresultfromReader(cmbreaderdevice reader,cmbreadresults readresults){btnscan.settitle(“开始扫描”,UIControlstate.normal);Isscanning = false;tabledata.removeallobjects();if(readResults.subReadResults!= null && ReadResults.subReadResults.Length> 0){tabledata.addobjects(ReadResults.subReadResults);tvresults.reloaddata();}否则if(readresults.readresults.length> 0){tabledata.add(ReadResults.readResults [0]);tablesource.setItems(tabledata);tablesource.displayResult(0);tvresults.reloaddata();tvresults.selectrow(nsindexpath.fromrowsection(0,0),false,UigityViewscrollPosition.none); }
可能会出现设备因电池电量不足或手动断开电缆而断开的情况。这些病例可以被ConnectionStateDidChangeOfReader回调IcmbreaderdevictionElegate.
注意:这可用性DIDCHANGEOFREADER.当设备变得身体不可用时,还调用方法。这意味着(重新)连接是不可能的。始终检查可用性的属性ReaderDevice对象之前,尝试调用ConnectWithCompletion方法。
如果您计划使用cmbSDK来使用智能手机或平板电脑(没有MX移动终端)进行移动扫描,那么该SDK需要安装许可密钥。没有许可密钥,SDK仍然可以运行,尽管扫描结果会被混淆(SDK会用星号字符随机替换扫描结果中的字符)。
有关如何获取许可证密钥的信息,请联系您的康耐眼销售代表,包括可用于评估SDK的试用许可证。
获取许可证密钥后,有两种方法可以在应用程序中添加许可证密钥。
第一个是将其添加为具有特定项目的值的键info.plist.文件:
<键> MX_MOBILE_LICENSE 键> <字符串>许可密钥串>
要建设一个激活的第二种方式是直接从代码连接到您的设备之前:
....readerdevice.weakdelegate =这个;/ / CMBReaderDevice.SetCameraRegistrationKey(“YOUR_MX_MOBILE_LICENSE”);connectToReaderDevice ();
在可移植项目中,我们创建一个页面(主页)我们为扫描页面和一个类(camerapreview.cs)创建了一个playout,它将继承自视图控件并具有一些属性和事件。
CameraPreview控制主页从后面的代码添加OnAppearing方法,这样我们就可以设置一些公共属性ResultReceived和connectionstatechanged.事件。
受保护的覆盖void onappearing(){base.onappearing();if(camerapreview == null){camerapreview = new camerapreview();//您可以从此处选择设备,或者您可以在此页面上的每一个出现选择。默认为SelectFromlist CamerApreview.selecteddevice = scanningdevice.mobilecamera;//预览启用。默认为true //camerapreview.scanningpreviewenable = false;//在收到结果时将触发的事件Camerapreview.resultreceived + = camerapreview_resultreceived;//当连接状态将更改Camerapreview.ConnectionStateChanged + = CamerApreview_ConnectionStateChanged时,将触发的事件//在此内容gridcamera.children.insert中添加此控件(0,CamerApreview);/ *如果在导航页面中使用扫描仪,并且在导航堆栈(再次使用扫描仪控件的地方添加另一个页面)*从此页面vight.pushasync(new aplitePage())请在导航之前使用此代码* / // 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平台具体项目:
受保护的覆盖void onelementChanged(ElementChangedEventargs E){Base.OneLementChanged(E);if(e.olleElement!= null ||元素== null){return;} rlMainContainer =新的RelativeLayout(上下文);... MainActivity.selfActivity.setActiveReader(对照,元素);}
在主要活动在Android平台特定的项目中,我们处理的是reader设备对象(几乎是一样的ScannerActivity为Xamarin的。安卓),从自定义渲染器,我们只需调用setActiveReader方法,并传递Control(原生RelativeLayout元素)和element(从MainPage初始化的CameraPreview对象)。
viewRenderer.在iOS平台具体项目:
受保护的覆盖void onelementChanged(ElementChangedEventargs E){Base.OneLementChanged(E);if(e.olleElement!= null ||元素== null){return;container = new uiview();ivpreview = new uiimageview();IVPREVIEW.CONTENTMODE = UIVIEWCONTENTMODE.SCALETOFILL;...... appdelegate.selfdelegate.setactiveReader(控制,元素);}
在AppDelegate中在iOS平台特定项目中,我们使用读者设备对象处理(几乎相同视图控制器为Xamarin.iOS),从自定义渲染器,我们只是调用setActiveReader方法,并传递Control(原生UIImageView元素)和element(从MainPage初始化的CameraPreview对象)。
配置读取设备、连接设备、扫描条码和断开从设备的连接是一样的,你可以读到他们Xamarin的。安卓和Xamarin.iOS部分。