【问题标题】:Xamarin forms: System.NullReferenceException when using ZXing.Net.Mobile for scanning?Xamarin 形式:使用 ZXing.Net.Mobile 进行扫描时出现 System.NullReferenceException?
【发布时间】:2021-05-26 23:26:53
【问题描述】:

我正在使用ZXing.Net.Mobile NuGet 包来扫描条形码编号。我已经按照这个blog做了所有事情。

我的代码

//Main Project Interface
public interface IQrScanningService  
{  
   Task<string> ScanAsync();  
}

//Android part implementation
[assembly: Dependency(typeof(XFBarcode.Droid.Services.QrScanningService))]  
namespace projectname.Droid.Services  
{  
    public class QrScanningService : IQrScanningService  
    {  
        public async Task<string> ScanAsync()  
        {  
            var optionsDefault = new MobileBarcodeScanningOptions();  
            var optionsCustom = new MobileBarcodeScanningOptions();  
  
            var scanner = new MobileBarcodeScanner()  
            {  
                TopText = "Scan the QR Code",  
                BottomText = "Please Wait",  
            };  
  
            var scanResult = await scanner.Scan(optionsCustom);  
            return scanResult.Text;  
        }  
    }  
}  

但是当我执行时,我得到了System.NullReferenceException: 'Object reference not set to an instance of an object.'。我在这里还缺少什么?我看到了一个类似的线程here,但不知道如何从 GitHub 下载使用包。

【问题讨论】:

标签: xamarin.forms zxing.net.mobile


【解决方案1】:

您错过了初始化步骤。

试试这个:

public async Task<string> ScanAsync()
    {
        //Initialize the scanner first so it can track the current context
        MobileBarcodeScanner.Initialize(MainActivity.Instance.Application);

        var optionsDefault = new MobileBarcodeScanningOptions();
        var optionsCustom = new MobileBarcodeScanningOptions();

        var scanner = new MobileBarcodeScanner()
        {
            TopText = "Scan the QR Code",
            BottomText = "Please Wait",
        };

        var scanResult = await scanner.Scan(optionsCustom);
        return scanResult.Text;
    }  

在您的 MainActivity 中:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        Instance = this;
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }

}

您也可以直接在 MainActivity OnCreate() 方法中初始化。

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    MobileBarcodeScanner.Initialize(Application); //initialize  here
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2014-05-26
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
相关资源
最近更新 更多