我正在使用Xamarin.Forms,我尝试使用禁用按钮,然后在按钮操作完成时启用它,当我禁用它时,该按钮实际上会变为禁用状态,但当我启用它时,它仍然被禁用,我做错了什么?下面是我的完整方法:
private void OnCameraScan(object sender, EventArgs e)
{
ScanLicence.IsEnabled = false;
var barcodeScanView = new ZXing.Net.Mobile.Forms.ZXingScannerView
{
HeightRequest = 200
};
ScannerWrapper.HeightRequest = 200;
ScannerWrapper.IsVisible = true;
var options = new MobileBarcodeScanningOptions
{
TryHarder = true,
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
};
barcodeScanView.OnScanResult += (result) =>
{
barcodeScanView.IsScanning = false;
Console.WriteLine(result);
var data = result.Text.Split('\n');
foreach (var line in data)
{
if (line.Length > 3)
{
var code = line.Substring(0, 3);
var value = line.Substring(3);
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
switch (code)
{
case "DCT":
userClass.Customer_Name = value.Trim();
Customer_Name.Text = value.Trim();
break;
case "DCS":
userClass.Customer_LName = value.Trim();
Customer_LName.Text = value.Trim();
break;
case "DAI":
userClass.City = value.Trim();
City.Text = value.Trim();
break;
case "DAG":
userClass.Address1 = value.Trim();
Address1.Text = value.Trim();
break;
case "DAK":
userClass.Zip = value.Trim();
Zip.Text = value.Trim();
break;
}
});
}
}
ScanLicence.IsEnabled = true;
};
barcodeScanView.Options = options;
barcodeScanView.IsScanning = true;
ScannerWrapper.Children.Add(barcodeScanView);
}
有问题的按钮是ScanLicence
。
<Button x:Name="ScanLicence" Text="Scan Licence" Clicked="OnCameraScan" Style="{StaticResource ButtonStyle}" WidthRequest="50" />
这是针对iOS应用程序的。
发布于 2019-12-18 23:44:47
您可以尝试将调用事件的对象强制转换为"Button“,然后禁用它。在你的代码的末尾,你可能会得到类似这样的东西:
// Safe casting to Button instance.
Button button = sender as Button;
// Make sure the cast didn't return a null value
if(button == null) return;
// Set enable to true
button.IsEnabled = true; // You can also use button.IsEnabled = !button.IsEnabled
确保你没有任何类型的按钮绑定,它可能会忽略后面代码中的属性赋值。
https://stackoverflow.com/questions/59380199
复制