[英]Xamarin.Forms setting IsEnable to true not working
我正在使用 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 应用程序的。
您可以尝试将调用事件的对象强制转换为“按钮”,然后禁用它。 在你的代码的最后,你可能有这样的东西:
// 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
确保您没有任何类型的按钮绑定,它可能会忽略后面代码中的属性分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.