我们在使用 Zxing Barcodescanner for Xamarin.Forms 时遇到了问题。 扫描仪在Android上运行良好,但在IOS上我看不到相机图像(预览)。如果我将条形码放在相机前,扫描仪会扫描IOS上的条形码,但相机预览只是白色背景。
我尝试使用这些选项,但没有运气。 我们正在使用 Prism.Forms for MVVM。
正如我所提到的,我的代码在安卓上运行良好。 以下是一些详细信息:
- 在两个平台上都正确设置了权限。 NuGets ZXing.Net.Mobile
- 和ZXing.Net.Mobile.Forms也被添加 所有三个项目(Android,IOS和便携式)
- 我们正在使用 .NET 标准 2.0
- Xamarin.Forms 是版本 3.4.0
ScannerView.xaml
<forms:ZXingScannerPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
x:Class="App.Portable.View.ScannerView">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<forms:ZXingScannerView x:Name="scanner" Grid.Column="0" Grid.Row="0" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"
IsScanning="{Binding IsScanning}"
IsAnalyzing="{Binding IsAnalyzing}"
Result="{Binding Result, Mode=TwoWay}"
ScanResultCommand="{Binding CmdScanResult}"
Options="{Binding ScannerOptions}"
/>
<forms:ZXingDefaultOverlay Grid.Column="0" Grid.Row="0"
TopText="Some title"
ShowFlashButton="False"
BottomText="Some bottom text"
Opacity="0.9"/>
</Grid>
</ContentPage.Content>
扫描仪视图模型.cs
public class ScannerViewModel : ViewModelBase
{
//Initializing variables
public ScannerViewModel()
{
var options = new MobileBarcodeScanningOptions();
options.TryHarder = true;
options.InitialDelayBeforeAnalyzingFrames = 300;
options.DelayBetweenContinuousScans = 100;
options.DelayBetweenAnalyzingFrames = 200;
options.AutoRotate = false;
ScanningOptions = options;
Title = "Barcode-Scanner";
CmdScanResult = new DelegateCommand(OnCmdScanResult);
IsScanning = true;
IsAnalyzing = true;
}
public MobileBarcodeScanningOptions ScanningOptions
{
get => _scanningOptions;
set => SetProperty(ref _scanningOptions, value);
}
public bool IsScanning
{
get => _isScanning;
set => SetProperty(ref _isScanning, value);
}
public bool IsAnalyzing
{
get => _isAnalyzing;
set => SetProperty(ref _isAnalyzing, value);
}
public Result Result
{
get => _result;
set => SetProperty(ref _result, value);
}
public DelegateCommand CmdScanResult { get; }
private void OnCmdScanResult()
{
IsAnalyzing = false;
IsScanning = false;
Device.BeginInvokeOnMainThread(
async () =>
{
IsAnalyzing = false;
var parameters = new NavigationParameters();
parameters.Add(CodeConstants.BARCODE, Result);
await NavigationService.GoBackAsync(parameters);
});
}
}
有没有人在我的代码中看到问题,或者对如何做得更好或至少让它工作有一些建议?
编辑: 我将测试项目上传到我的存储库以重现错误。扫描仪在 iPhone 上工作正常,但不显示相机预览: https://gitlab.com/mitti2000/zxingtest
原因:您将ZXingScannerView和ZXingDefaultOverlay放在网格的同一单元格中。然后,您将ZXingScannerView的HorizontalOptions
设置为EndAndExpand
。
溶液:
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"