我正在尝试从我的Xamarin.Forms应用程序扫描二维码,但直到我重新启动扫描仪才能检测到二维码。
我使用的是ZXing.Net.Mobile.Forms。一旦ScannerPage被推送到导航堆栈上,它就会请求相机权限。完成请求后,它可以访问摄像头,但不会扫描二维码。我尝试更改权限处理程序,因为它在授予权限后重新启动扫描程序后工作,但不起作用。
private async void createScanPageAsync()
{
#if __ANDROID__
// Initialize the scanner first so it can track the current context
MobileBarcodeScanner.Initialize (Application);
#endif
var options = new MobileBarcodeScanningOptions()
{
TryHarder = true,
};
var scanPage = new ZXingScannerPage()
{
Title = AppResources.scanPageTitle,
DefaultOverlayTopText = AppResources.scanPageTitle,
DefaultOverlayShowFlashButton = true
};
await Navigation.PushAsync(scanPage);
scanPage.OnScanResult += (scanResult) =>
{
scanPage.IsScanning = false;
Vibration.Vibrate();
QRCode scans = JsonConvert.DeserializeObject<QRCode>(scanResult.Text);
AppPreferences.Network.Ip = scans.ip;
AppPreferences.Network.Port = scans.port;
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
});
};
}
}预期的结果是在第一次尝试时扫描成功,但我只有在授予权限然后重新打开扫描程序页面后才能获得成功的扫描。
发布于 2021-02-10 05:02:25
对我来说,Xamarin.Essentials做到了这一点。它有一个权限插件,可以帮助你请求权限,然后你就可以毫无问题地启动扫描器了
https://stackoverflow.com/questions/57959484
复制相似问题