In the project development need to use to barcode scanning, because previously tested zxing, feeling recognition speed and function are good, so direct reference. However, in the actual development process, but encountered a continuous scanning problem, each scan recognition is completed, the scan form automatically closed.
Find the solution in the Xamarin forum and just find the solution for the iOS version. The solution for referring to iOS is to re-open the scan after the scan is complete. In this way, the idea of using intent for result is implemented. The implementation method is the following code:
Main form:
1 usingSystem;2 usingAndroid.app;3 usingandroid.content;4 usingAndroid.runtime;5 usingandroid.views;6 usingAndroid.widget;7 usingAndroid.os;8 usingZxing.mobile;9 Ten namespaceImstudio.qrcodelife One { A[Activity (Label ="Imstudio.qrcodelife", Mainlauncher =true)] - Public classmainactivity:activity - { the intCount =1; - - protected Override voidOnCreate (Bundle bundle) - { + Base. OnCreate (bundle); - + //Set Our view from the "main" layout resource A Setcontentview (Resource.Layout.Main); at - //Get Our buttons from the layout resource, - //And attach an event to it - varbutton = findviewbyid<button>(Resource.Id.myButton); - varTV = Findviewbyid<textview>(Resource.Id.textView1); - inbutton. Click + =Async Delegate - { toStartactivityforresult (typeof(CodeActivity),1); + }; - } the * protected Override voidOnactivityresult (intRequestcode, Result ResultCode, Intent data) $ {Panax Notoginseng if(Requestcode = =1) - { the if(ResultCode = =Result.ok) + { AFindviewbyid<textview> (Resource.Id.textView1). Text + = data. Getstringextra ("Code") +System.Environment.NewLine; theStartactivityforresult (typeof(CodeActivity),1); + } - } $ } $ } -}
Subform, add a "finish" or "Cancel" button to close the Sweep code form, as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingAndroid.app;usingandroid.content;usingAndroid.os;usingAndroid.runtime;usingandroid.views;usingAndroid.widget;namespaceimstudio.qrcodelife{[Activity (Label="CodeActivity")] Public classcodeactivity:activity {protected Override Async voidOnCreate (Bundle bundle) {Base. OnCreate (bundle); //Create your application here varScanner =NewZXing.Mobile.MobileBarcodeScanner ( This); Scanner. Usecustomoverlay=true; varZxingoverlay = Layoutinflater.fromcontext ( This). Inflate (Resource.Layout.Code,NULL); varDonebutton = zxingoverlay.findviewbyid<button>(Resource.Id.buttonZxingDone); Donebutton.click+ = (sender, e) = ={scanner. Cancel (); Setresult (result.canceled); Finish (); }; Scanner. Customoverlay=Zxingoverlay; varresult =awaitscanner. Scan (); Handlescanresult (result); } Private voidHandlescanresult (Zxing.result Result) {if(Result! =NULL) {Intent Intent=NewIntent (); Intent. PutExtra ("Code", result. Text); Setresult (result.ok,intent); Finish (); } } }}
Implementation code Although some rough, but functional OK, first use, back to think there is no good way.
Xamarin develops Android notes: continuous scanning with zxing