异常:System.NullReferenceException:'Object reference not set to an instance of an object.'
Exception : System.NullReferenceException: 'Object reference not set to an instance of an object.'
我正在尝试创建一个简单的 Qr 码扫描器应用程序,它有一个像这样的单一视图:
但是当我尝试按“点击”按钮启动扫描仪时,我得到了这个异常:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
这是 main.xaml 页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="scanner.MainPage">
<StackLayout Spacing="10">
<Button Text="Click"
x:Name="btnScan"
Clicked="btnScan_Clicked"/>
<Entry x:Name="txtBarcode"
Placeholder="Text Do scan"/>
</StackLayout>
</ContentPage>
main.xaml.cs:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void btnScan_Clicked(object sender, EventArgs e)
{
try
{
var scanner = DependencyService.Get<Interface1>();
var result = await scanner.ScanAsync();
if (result != null)
{
txtBarcode.Text = result;
}
}
catch (Exception ex)
{ throw;
}
服务:
internal class QrScanningService : Interface1
{
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;
}
}
我做了一个简单的,遇到了同样的问题。于是在网上搜索了一下,发现使用ZXing.Net.Mobile包时需要初始化插件
所以可以在MainActivity的OnCreate方法中加入初始化代码。如:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
MobileBarcodeScanner.Initialize(Application);// this line initialize the plugin
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
你可以查看文档:https://github.com/Redth/ZXing.Net.Mobile#android
然后在 ios 上检查这个 link:https://github.com/Redth/ZXing.Net.Mobile#ios
我正在尝试创建一个简单的 Qr 码扫描器应用程序,它有一个像这样的单一视图:
但是当我尝试按“点击”按钮启动扫描仪时,我得到了这个异常:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
这是 main.xaml 页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="scanner.MainPage">
<StackLayout Spacing="10">
<Button Text="Click"
x:Name="btnScan"
Clicked="btnScan_Clicked"/>
<Entry x:Name="txtBarcode"
Placeholder="Text Do scan"/>
</StackLayout>
</ContentPage>
main.xaml.cs:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void btnScan_Clicked(object sender, EventArgs e)
{
try
{
var scanner = DependencyService.Get<Interface1>();
var result = await scanner.ScanAsync();
if (result != null)
{
txtBarcode.Text = result;
}
}
catch (Exception ex)
{ throw;
}
服务:
internal class QrScanningService : Interface1
{
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;
}
}
我做了一个简单的,遇到了同样的问题。于是在网上搜索了一下,发现使用ZXing.Net.Mobile包时需要初始化插件
所以可以在MainActivity的OnCreate方法中加入初始化代码。如:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
MobileBarcodeScanner.Initialize(Application);// this line initialize the plugin
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
你可以查看文档:https://github.com/Redth/ZXing.Net.Mobile#android
然后在 ios 上检查这个 link:https://github.com/Redth/ZXing.Net.Mobile#ios