我一页上有3个标签。此选项卡中的一个(BasketVM.cs)具有Scannerview(带有Zxing),并且在首次调用此页面时可以完美地工作。但是,当我尝试更改选项卡时,又回到BasketVM.cs时,裸代码扫描器被冻结。
我尝试使用属性“ IsScanning”,该值有效,但是相机始终处于冻结状态。
卷轴问题是此属性IsScanning。当我设置为false时,相机冻结,但是当我尝试设置为true时,相机始终冻结。我认为他contentview不能正确刷新...
我的主页上有3个标签:
public SaleVM(BasketVM basketVM, ProductSearchVM productSearchVM,
CustomerVM customerVM) : base(customerHeaderVM)
{
BasketVM = basketVM;
ProductSearchVM = productSearchVM;
CustomerVM = customerVM;
}
public override void OnViewAppearing()
{
BasketVM.Navigation = Navigation;
ProductSearchVM.Navigation = Navigation;
CustomerVM.Navigation = Navigation;
CallOnViewAppearing(SelectedTabIndex);
}
public override void OnViewDisappearing()
{
CallOnViewDisappearing(SelectedTabIndex);
}
#region SelectedTabIndex property
int _SelectedTabIndex;
public int SelectedTabIndex
{
get { return _SelectedTabIndex; }
set { SetProperty(ref _SelectedTabIndex, value); }
}
#endregion
#region TabChange Command
Command _TabChangeCommand;
int previousTabIndex;
public Command TabChangeCommand
{
get { return GetProperty(ref _TabChangeCommand, () => new
Command(TabChange)); }
}
void TabChange()
{
Log.Debug("Tab change {previousTabIndex} => {SelectedTabIndex}",
previousTabIndex, SelectedTabIndex);
if (previousTabIndex == SelectedTabIndex)
return;
CallOnViewDisappearing(previousTabIndex);
CallOnViewAppearing(SelectedTabIndex);
previousTabIndex = SelectedTabIndex;
}
#endregion
#region Children VM
public BasketVM BasketVM { get; private set; }
public ProductSearchVM ProductSearchVM { get; private set; }
public CustomerVM CustomerVM { get; private set; }
#endregion
void CallOnViewDisappearing(int tabIndex)
{
switch (tabIndex)
{
case 0:
BasketVM.OnViewDisappearing();
break;
case 1:
ProductSearchVM.OnViewDisappearing();
break;
case 2:
CustomerVM.OnViewDisappearing();
break;
default:
break;
}
}
void CallOnViewAppearing(int tabIndex)
{
switch (tabIndex)
{
case 0:
BasketVM.OnViewAppearing();
break;
case 1:
ProductSearchVM.OnViewAppearing();
break;
case 2:
CustomerVM.OnViewAppearing();
break;
default:
break;
}
}
它运行完美。
我的BasketView.xaml:
<control:BarcodeScanner x:Name="scanner" Grid.Row="0" Text="{Binding ScanText}" ScanCommand="{Binding ScanCommand}" IsScanning="{Binding IsScanning}" IsTabStop="True" />
BasketVM.cs:
public override void OnViewAppearing()
{
Log.Debug("Enabling scanner");
IsScanning = true;
Device.BeginInvokeOnMainThread(
// We need to run this on UI Thread for iOS,
because it causes a scrolling on the view
() =>
{ foreach (var item in Items)
item.Navigation = Navigation;
}
);
// Set up child VM Navigation, necessary form VM initialized in
the constructor
}
public override void OnViewDisappearing()
{
Log.Debug("Disabling scanner");
IsScanning = false;
}
#region IsScanning property
private bool _IsScanning;
public bool IsScanning
{
get { return _IsScanning; }
set {
SetProperty(ref _IsScanning, value);
}
}
#endregion
#region ScanText property
private string _ScanText;
public string ScanText
{
get { return _ScanText; }
set { SetProperty(ref _ScanText, value); }
}
#endregion
我的控件BarecodeScanner.cs
public class BarcodeScanner : ContentView
{
ZXingScannerView scannerView;
Label label;
public BarcodeScanner()
{
var grid = new Grid();
scannerView = new ZXingScannerView
{
Options = new MobileBarcodeScanningOptions
{
PureBarcode = true,
TryHarder = true,
UseNativeScanning = true,
PossibleFormats = new List<BarcodeFormat>
{
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.CODE_128,
BarcodeFormat.QR_CODE
},
DelayBetweenContinuousScans = 2000,
},
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
IsAnalyzing = true,
AutomationId= "zxingScannerView",
IsScanning =true
};
scannerView.OnScanResult += ScannerView_OnScanResult;
label = new Label
{
TextColor = Color.White,
BackgroundColor = Color.FromRgba(0, 0, 0, 100),
FontSize = 20,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.FillAndExpand,
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center,
Opacity = 0,
};
grid.Children.Add(scannerView);
grid.Children.Add(label);
Content = grid;
}
static void OnTextChanged(BindableObject bindable, object oldValue,
object newValue)
{
BarcodeScanner scanner = (BarcodeScanner)bindable;
string text = (string)newValue;
scanner.label.Text = text;
Task.Run(() => scanner.ShowText());
}
#endregion
#region IsScanningProperty
public static readonly BindableProperty IsScanningProperty =
BindableProperty.Create("IsScanning", typeof(bool),
typeof(BarcodeScanner), defaultValue: false, propertyChanged:
OnIsScanningChanged);
public bool IsScanning
{
get { return (bool)GetValue(IsScanningProperty); }
set { SetValue(ScanCommandProperty, value); }
}
static void OnIsScanningChanged(BindableObject bindable, object
oldValue, object newValue)
{
BarcodeScanner scanner = (BarcodeScanner)bindable;
bool isScanning = (bool)newValue;
scanner.scannerView.IsScanning = isScanning;
}
#endregion
private void ScannerView_OnScanResult(Result result)
{
ScanCommand.Execute(result.Text);
}