iOS 使用 ZXing Xamarin 的条码扫描器应用程序在应用程序 Class 中遇到空引用错误

Barcode Scanner App Using ZXing Xamarin for iOS hits null reference error in Application Class

我的公司需要一个基于智能手机的 Barcode/QRcode 扫描仪应用程序,它必须在 iPhone 和 Android 上运行,但主要是 iPhone。我决定试一试 Xamarin Forms,它在 Android 上运行得非常好,但为了让应用程序出现在 iPhone 上,我花了一些认真的工作。我终于让它在 iPhone 上打开了,但是当我点击扫描按钮时它抛出了
未处理的异常:

System.NullReferenceException: Object reference not set to an instance of an 
object`

在应用程序 class 上 UIApplication.Main(args, null, "AppDelegate");

using System;
using System.Collections.Generic;
using System.Linq;

using Foundation;
using UIKit;

namespace QRNatives.iOS
{
    public class Application
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        UIApplication.Main(args, null, "AppDelegate");
    }
}
}

我编程的时间不长,但似乎空引用错误通常很容易追踪。但是,我看不出这个问题实际发生在哪里。我怀疑问题出在 AppDelegate.cs,但我没有发现任何问题。

using Foundation;
using UIKit;

namespace QRNatives.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the 
// User Interface of the application, as well as listening (and optionally responding) to 
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();

        global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();

        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }
}
}

只是为了了解更多信息,这是我在 QrScanningService.cs 中用于将 iOS 链接到共享接口数据的内容。

using System.Threading.Tasks;
using QRNatives.Services;
using Xamarin.Forms;
using ZXing.Mobile;

[assembly: Dependency(typeof(QRNatives.iOS.Services.QrScanningService))]

namespace QRNatives.iOS.Services
{
class QrScanningService
{
    public class QrCodeScanningService : IQrScanningServies
    {
        public async Task<string> ScanAsync()
        {
            var scanner = new MobileBarcodeScanner();
            var scanResults = await scanner.Scan();

            return scanResults.Text;
        }
    }
}
}

我搜索了一些旧帖子是否出现了这个问题,但是 none 我读过的帖子中有任何真正的问题答案。有没有人有成功使用 ZXing 和 Xamarin for iOS 的经验?或者有人对我还能如何追踪错误的实际来源有任何建议吗?我已经坚持了一段时间,所以任何帮助将不胜感激。谢谢。

访问iPhone中的camera,您应该在info.plist中添加权限:

 <key>NSCameraUsageDescription</key>      
 <string>Camera acess to scan barcodes</string>

编辑 info.plist inside Visual Studio:

右击info.plist并选择open with,您可以选择Generic PList EditorXML editor(任意)进行编辑。

更多权限:

参考:infoplist-reference