[英]ZXing.Net.Mobile does not start scanning immediately
I have a page with ScannerView as MainPage.我有一个带有 ScannerView 作为 MainPage 的页面。 When I start the app it can't scan a barcode.
当我启动应用程序时,它无法扫描条形码。 I have to set another page as mainpage then navigate to the scanner page, before it can scan a barcode.
我必须将另一个页面设置为主页,然后导航到扫描仪页面,然后才能扫描条形码。 Or lock and then unlock the phone, then it will start scanning.
或者锁定然后解锁手机,然后它就会开始扫描。
App.xaml.cs:应用程序.xaml.cs:
MainPage = new NavigationPage(new ScannerPage());
ScannerPage.xaml:扫描器页面.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Pages.ScannerPage"
xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms">
<ContentPage.Content>
<Grid>
<zxing:ZXingScannerView x:Name="ScannerView"
IsScanning="True"
IsAnalyzing="True" />
<zxing:ZXingDefaultOverlay x:Name="ScannerOverlay"
TopText="Hold your phone up to the QR code"
BottomText="Scanning will happen automatically"
ShowFlashButton="True"/>
</Grid>
</ContentPage.Content>
</ContentPage>
ScannerPage.xaml.cs: ScannerPage.xaml.cs:
public partial class ScannerPage : ContentPage
{
public ScannerPage ()
{
InitializeComponent ();
ScannerView.Options = new MobileBarcodeScanningOptions
{
PossibleFormats = new List<BarcodeFormat>
{
BarcodeFormat.DATA_MATRIX,
},
TryHarder = true
};
ScannerView.OnScanResult += (result) => Device.BeginInvokeOnMainThread(async () =>
{
ScannerView.IsAnalyzing = false;
await DisplayAlert("Scanned", result.Text, "Ok");
ScannerView.IsAnalyzing = true;
});
}
}
I ended up using the following code programmatically:我最终以编程方式使用了以下代码:
public static async Task ScanConnection()
{
MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
TryHarder = true,
PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE }
};
MobileBarcodeScanner scanner = new ZXing.Mobile.MobileBarcodeScanner();
ZXing.Result result = await scanner.Scan(options);
if (result != null && !string.IsNullOrEmpty(result.Text))
{
...
}
}
You could call that code from an overridden OnAppearing() method from within your derived page.您可以从派生页面中的重写 OnAppearing() 方法调用该代码。
I had a similar issue.我有一个类似的问题。 At the first invocation of the scanner page, it would attempt to scan but wouldn't recoqnise any barcodes.
在第一次调用扫描仪页面时,它会尝试扫描但不会识别任何条形码。 After exiting the page and re-opening it, the problem disappeared.
退出页面并重新打开后,问题消失。
Solution that worked in my case was adding the following in two places:在我的案例中有效的解决方案是在两个地方添加以下内容:
ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
</manifest>
MainActivity.cs
(yours may be different)MainActivity.cs
(你的可能不同) public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
//needed for zxing scanner..
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.