将 IsScanning 属性从 False 更改为 True 后 C# Xamarin ZXing 无法扫描条形码
Posted
技术标签:
【中文标题】将 IsScanning 属性从 False 更改为 True 后 C# Xamarin ZXing 无法扫描条形码【英文标题】:C# Xamarin ZXing Unable to Scan Barcode after Change IsScanning Property from False to True 【发布时间】:2021-10-03 07:29:58 【问题描述】:我有一个条形码扫描仪页面,即 BarcodeScannerView 和 BarcodeScannerViewModel。这样做的主要目的是在读取条形码后导航到另一个页面。我所做的是扫描条形码后应用程序成功导航到其他页面。在导航到下一页之前,我设置了 IsScanning = false 以防止下一次扫描。但是,当我尝试返回条形码扫描仪页面时,我设置了属性 IsScanning = true 但扫描仪不再工作。关于如何解决这个问题的任何想法?
条形码扫描仪视图
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MultiPosApp.InventorySystem.Views.BarcodeScannerView"
NavigationPage.HasNavigationBar="False"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable">
<Grid RowSpacing="0" ColumnSpacing="0" BackgroundColor="Transparent" Margin="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<zxing:ZXingScannerView x:Name="scanner" Grid.RowSpan="3" Grid.ColumnSpan="3"
ScanResultCommand="Binding ScanResultCommand"
Result="Binding BarcodeResult, Mode=TwoWay"
IsScanning="Binding IsScanning, Mode=TwoWay"
IsAnalyzing="Binding IsAnalyzing, Mode=TwoWay"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"/>
<zxing:ZXingDefaultOverlay Grid.RowSpan="3"
Grid.ColumnSpan="3"
BottomText="Place the red line over the barcode you'd like to scan." />
</Grid>
</ContentPage>
BarcodeScannerViewModel
public class BarcodeScannerViewModel : ViewModelBase
#region Variables
private INavigationService m_NavigationService;
private IEventAggregator m_EventAggregator;
private SQLDbConnection Conn;
public DelegateCommand ScanResultCommand get; set;
private ZXing.Result m_BarcodeResult;
public ZXing.Result BarcodeResult
get return m_BarcodeResult;
set SetProperty(ref m_BarcodeResult, value);
private bool m_IsScanning = true;
public bool IsScanning
get return m_IsScanning;
set SetProperty(ref m_IsScanning, value);
private bool m_IsAnalyzing = true;
public bool IsAnalyzing
get return m_IsAnalyzing;
set SetProperty(ref m_IsAnalyzing, value);
private string m_Barcode;
public string Barcode
get return m_Barcode;
set SetProperty(ref m_Barcode, value);
#endregion
#region Constructor
public BarcodeScannerViewModel(INavigationService navigationService, IEventAggregator eventAggregator, SQLDbConnection sql) : base(navigationService)
m_NavigationService = navigationService;
m_EventAggregator = eventAggregator;
Conn = sql;
ScanResultCommand = new DelegateCommand(OnScanResult);
#endregion
#region Method
private void OnScanResult()
IsAnalyzing = false;
IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
string barcode = BarcodeResult.Text;
await m_NavigationService.NavigateAsync("RegisterGoodsView");
IsAnalyzing = true;
IsScanning = true;
);
public override void OnNavigatedTo(INavigationParameters parameters)
Device.BeginInvokeOnMainThread(async () =>
IsAnalyzing = true;
IsScanning = true;
);
#endregion
【问题讨论】:
Mods 不会让我发布作为答案,我不能将您的问题标记为重复,因为 question 这个答案来自没有被赞成或接受的答案,所以给你:***.com/a/63397456/8395242 【参考方案1】:但是,当我尝试返回条形码扫描仪页面时,我设置了属性 IsScanning = true 但扫描仪不再工作。
首先,您需要确认您在导航到页面之前或之后使用相同的 BarcodeScannerViewModel 实例。
那么你需要实现INotifyPropertyChanged接口来通知IsScanning
值改变了。
public class ViewModelBase : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
private bool m_IsScanning = true;
public bool IsScanning
get return m_IsScanning;
set
m_IsScanning = value;
RaisePropertyChanged("IsScanning");
【讨论】:
以上是关于将 IsScanning 属性从 False 更改为 True 后 C# Xamarin ZXing 无法扫描条形码的主要内容,如果未能解决你的问题,请参考以下文章