简体   繁体   English

我调用Scan()方法时ZXing.Net.Mobile条形码扫描器错误

[英]ZXing.Net.Mobile Barcode scanner error when I call Scan() method

I am trying to implement the barcode scanner into my windows universal app. 我正在尝试将条形码扫描仪应用到我的Windows通用应用程序中。 I am using ZXing.Net.Mobile 我正在使用ZXing.Net.Mobile

var _scanner = new MobileBarcodeScanner(this.Dispatcher);                    
var result = await _scanner.Scan();

Then, the Visual Studio stops at this line 然后,Visual Studio停在此行

if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

So, how to solve this problem? 那么,如何解决这个问题呢?

I don't get an exception, the VS just highlights the line mentioned above. 我没有例外,VS只是突出了上面提到的那条线。 I believe the problem is that I don't have ZXing.Net.Mobile.WindowsUniversal in the references. 我相信问题是我在参考文献中没有ZXing.Net.Mobile.WindowsUniversal。 but I can't find it in NuGet. 但我在NuGet中找不到它。

You can click the "Tools" of vs and choose the label "NuGet Package Manager" then select the "Manage NuGet Package for Solution", in the "Browse" label, search for "ZXing.Net.Mobile", if you are working on an Uwp app, choose "ZXing.Net.Mobile", if you are using Xamarin for developing, choose "ZXing.Net.Mobile.Forms", in the right of the window, select your project and click Install. 您可以单击vs的“工具”并选择标签“NuGet Package Manager”,然后选择“管理NuGet Package for Solution”,在“Browse”标签中搜索“ZXing.Net.Mobile”,如果您正在工作在Uwp应用程序中,选择“ZXing.Net.Mobile”,如果您使用Xamarin进行开发,请选择“ZXing.Net.Mobile.Forms”,在窗口右侧,选择您的项目并单击“安装”。

Here is a picture I complete installed this package: 这是我完成安装此包的图片: 在此输入图像描述

I've tested your code in a button click event, your posted code can be debugged after installed this package. 我已经在按钮单击事件中测试了您的代码,安装此软件包后可以调试您发布的代码。

But if I debug it on the local machine, the same error occurred, this is possible because there is no scanner can be initialized on my computer, so when I test it on mobile emulator, this error won't happen. 但是,如果我在本地计算机上调试它,同样的错误发生,这是可能的,因为没有扫描仪可以在我的计算机上初始化,所以当我在移动模拟器上测试它时,不会发生此错误。

if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

Some other problems can also cause it, if my answer can not solve your problem, you can add a comment. 其他一些问题也可能导致它,如果我的答案无法解决您的问题,您可以添加评论。

I think the exception is "Access is denied" 我认为例外是“访问被拒绝”

This is your newest comment about your problem, when you want to use ZXing in your app, you will need to open your project's manifest file, choose the "Capabilities" label, and in the left, enable "Internet(Client)", "Microphone" and "Webcam" capabilities. 这是您对问题的最新评论,当您想在应用程序中使用ZXing时,您需要打开项目的清单文件,选择“功能”标签,然后在左侧启用“Internet(客户端)”,“麦克风“和”网络摄像头“功能。 Just tested, if you didn't enable this capabilities, the same error will also occurred on mobile emulator. 刚刚测试过,如果您没有启用此功能,移动模拟器上也会出现相同的错误。 This is also a possible cause of this problem. 这也是导致此问题的可能原因。

You have to set in mainpage of UWP. 你必须在UWP的主页中设置。

        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Enabled;            ZXing.Net.Mobile.Forms.WindowsUniversal.ZXingScannerViewRenderer.Init();

Then on button click in Xamarin Form or Native app page 然后在按钮上单击Xamarin表单或本机应用程序页面

        var options = new ZXing.Mobile.MobileBarcodeScanningOptions();           
        options.TryHarder=true;                        
        var scanner = new ZXing.Mobile.MobileBarcodeScanner();             
        scanner.TopText="Hold For Scanning";
        scanner.BottomText="Tally Scan";
        var result = await scanner.Scan(options);                             
        if (result != null) {
            updateListWithText (result.Text);
       }    

@hatim You need to initialize the scanner before you use it. @hatim您需要在使用之前初始化扫描仪。

add this line to your code before you start playing with the scanner. 在开始使用扫描仪之前,请将此行添加到代码中。 you only need to do this once. 你只需要这样做一次。 so put it in your main activity 所以把它放在你的主要活动中

     `   MobileBarcodeScanner.Initialize(Application);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM