Источник:
http://blogs.msdn.com/dynamicsmobile...c-devices.aspx
==============
This is an installment of
Barcode Scanning With Microsoft Dynamics Mobile series.
This article is targeted at developers who want to start using the barcode scanner capabilities of Microsoft Dynamics Mobile 1.5 framework. It supports
Motorola (formerly Symbol) and
Intermec devices. After you have read this article and followed its tutorial part you will be able to implement your own tasklets with barcode scanning capabilities. It might seem a long way on first glance but once done you will realize that the heart of the functionality is doable in just a few steps.
The following tutorial describes all steps necessary to create a simple Rolepad based application which enables barcode scanning on Motorola (Symbol) and Intermec devices. The process is the same for both device classes – there are only two differences: The first one is proprietary .NET CF scanning assemblies which you must download and reference within your project and the second one is the corresponding section in the App.config file. Tutorial is written for Symbol, differences are
highlighted for Intermec.
- Prerequisites: Microsoft Visual Studio 2008 SP1 (Professional edition or above), Microsoft Dynamics Mobile – Device Components with Device SDK feature installed
- We are going to create one VS solution with two projects based on Microsoft Dynamics Mobile 1.5 platform. The main purpose of the Rolepad project called BarcodeScannerSample15 is an easy deployment of all necessary binaries configuration files. The barcode scanning logic is being coded in the separated tasklet project called BarcodeScanner.
- Open Visual Studio 2008 (Professional edition or above).
- Create a Rolepad project called BarcodeScannerSample15: File > New > Project… > Project types > Visual C# > Dynamics Mobile > Rolepad project for Windows Mobile 6 (or Windows Mobile 5 if you wish).
- Add a new tasklet project called BarcodeScanner to the BarcodeScannerSample15 solution: Right-click on BarcodeScannerSample15 solution > Add > New Project… > Project types > Visual C# > Dynamics Mobile > Tasklet project for Windows Mobile 6 (or 5).
- BarcodeScanner assembly should be deployed automatically when deploying BarcodeScannerSample15 project: Add reference of the BarcodeScanner project to the BarcodeScannerSample15 project.
- Add following references to the BarcodeScannerSample15 project:
Microsoft.Dynamics.Mobile.Components Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol (Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.SymbolIntermec) Microsoft.Dynamics.Mobile.Components.Tasklets.Menu
- The scanning itself is a manufacturer-specific operation, so manufacturer-specific assemblies will be needed. You have to download and extract the following assemblies: Symbol and Symbol.Barcode for Motorola (Symbol) devices and Intermec.DataCollection.CF2 for Intermec. Read the article Where to get barcode drivers to figure out where and how to get these.
- Open Windows Explorer and locate BarcodeScannerSample15 project folder, create BinaryReferences subfolder and copy extracted assemblies under this folder. Reference these binaries from the BarcodeScannerSample15 project.
- The core of barcode reader functionality provided by Microsoft Dynamics Framework is located in Microsoft.Dynamics.Mobile.Runtime assembly. Add following reference to the BarcodeScanner (tasklet) project:
Microsoft.Dynamics.Mobile.Framework.Runtime
- Open BarcodeScannerTasklet.cs and add following using statements:
using Microsoft.Dynamics.Mobile.Framework.Entities; using Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning; using Microsoft.Dynamics.Mobile.Components.Services;
- We will need a BarcodeReader object to perform the actual scanning. This object will be created by BarcodeScanningService defined in the App.config file. The service assures that a correct version of BarcodeReader object will be newed-up.
- Add BarcodeReader private field called barcodeReader.
BarcodeReader barcodeReader;
- Add BarcodeScanningService public property called BarcodeScanningService and decorate it with [RolePadService] attribute.
[RolePadService] public BarcodeScanningService BarcodeScanningService { get; set; }
- Edit OnStarted method. You must ask BarcodeScanningService to create particular BarcodeReader object and implement two event handlers with self-explaining names. This code will be called only once within the tasklet lifetime.
protected override void OnStarted() { base.OnActivated(); barcodeReader = BarcodeScanningService.GetReader(this); barcodeReader.Scanned += new EventHandler(barcodeReader_Scanned); barcodeReader.Error += new EventHandler(barcodeReader_Error); }
- Override OnActivate method. The scanner must be opened (enabled). This code can be called repeatedly when entering already started tasklet.
protected override void OnActivated() { base.OnActivated(); barcodeReader.Open(); }
- Override OnDeactivated method. The scanner will be closed (disabled). Counterpart of the OnActivate method – can be called repeatedly.
protected override void OnDeactivated () { base. OnDeactivated (); barcodeReader.Close(); }
- Override OnClosing method. Closing the system resources and bit of cleaning.
protected override void OnClosing(ExitResult exitResult) { base.OnClosing(exitResult); barcodeReader.Error -= barcodeReader_Error; barcodeReader.Scanned -= barcodeReader_Scanned; barcodeReader.Dispose(); }
- Add barcodeReader_Error event handler.
void barcodeReader_Error(object sender, ErrorEventArgs e) { if (e != null && e.Exception != null) Container.Alert("Exception ocured while using barcode scanner:\n" + e.Exception.Message); }
- Add barcodeReader_Scanned event handler. In case the scanning was successful tasklet view class is going to present the scanned data. Some barcode readers return multiple lines of the scanned information. Typically we need to know only the first one – that is why we pass 0 index of e.Data array to the view.
void barcodeReader_Scanned(object sender, ScannedDataEventArgs e) { if (e != null && e.Data != null && e.Data.Length > 0) view.ShowScannedBarcode(e.Data[0]); }
- You are done with the tasklet itself but we have to extend its view.
- Double-click BarcodeScannerView.cs and add one TextBox control called txtScannedBarcodes.
- Make txtScannedBarcodes control ReadOnly, Multiline and change its size to your convenience.
- Open IBarcodeScannerView.cs and add extend the interface by:
void ShowScannedBarcode(string barcode);
- Right-click BarcodeScannerView.cs and select View Code.
- Implement IBarcodeScannerView by the following code:
public void ShowScannedBarcode(string barcode) { this.txtScannedBarcodes.Text += barcode + Environment.NewLine; }
- You should be able to build BarcodeScanner tasklet project by now so let us finish the BarcodeScannerSample15 project.
- Modify app.config by adding of the following code at the beginning of the node. The simple configuration informs Framework that it should create BarcodeScanningService of an appropriate type. Please notice that you must comment out/in the correct section for Intermec devices:
Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol.ScannerDevice, Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Symbol <span style="color: #008000"> Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Intermec.ScannerDevice, Microsoft.Dynamics.Mobile.Components.Services.BarcodeScanning.Intermec -->
- Modify UserRole.xml – replace its whole content by the following code. It is a very simple orchestration with two tasklets: Menu tasklet provided by the Framework as an invitation screen and our newly created BarcodeScanner tasklet.
</span span style="color: #ff0000"version/spanspan style="color: #0000ff"="1.0"/span span style="color: #ff0000"encoding/spanspan style="color: #0000ff"="utf-8"/span ?span style="color: #0000ff">
- You should be able to build the whole solution at this moment.
- Double check that BarcodeScannerSample15 is your startup project. If it is not the case then right-click on BarcodeScannerSample15 project node and select “Set as StartUp Project”.
- Right-click Properties subfolder of BarcodeScannerSample15 project and select Open.
- Go to Devices and select your target device.
- Connect the device and press F5.
Do not forget to check MSDN documentation:
BarcodeScanningService Class
By the way: The complete source code can be downloaded – see the bottom of this article. However I am sure you are glad you went through the whole example by yourself - it is the best way how to learn the stuff.
Please notice that the downloadable code is more complex and much better commented than the example you created based on the tutorial above. The difference is mainly given by more extensive error handling in the downloadable code. The tutorial code should be used for tutoring/learning only, the downloadable example on the other hand rather as a skeleton for building real applications based on 1.5 version of the Framework.
Download source code here:
BarcodeScannerSample15.zip
This code will run first after you copy
barcode drivers into the .\BarcodeScannerSample15\BinaryReferences folder.
Источник:
http://blogs.msdn.com/dynamicsmobile...c-devices.aspx