简体   繁体   English

Xamarin.Forms 将 IsEnable 设置为 true 不起作用

[英]Xamarin.Forms setting IsEnable to true not working

I am using Xamarin.Forms and I am trying to use disable a button and then enable it when the button action is finished, when I disable it, the button does in fact become disabled, but when I enable it, its still disabled, what am I doing wrong?我正在使用 Xamarin.Forms 并且我正在尝试使用禁用按钮,然后在按钮操作完成时启用它,当我禁用它时,该按钮实际上被禁用,但是当我启用它时,它仍然被禁用,什么我做错了吗? Here is my full method:这是我的完整方法:

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);

}

The button in question is ScanLicence .有问题的按钮是ScanLicence

<Button x:Name="ScanLicence" Text="Scan Licence" Clicked="OnCameraScan" Style="{StaticResource ButtonStyle}" WidthRequest="50" />

This is for an iOS application.这是针对 iOS 应用程序的。

You can try cast the object which invokes the event to "Button" and then disable it.您可以尝试将调用事件的对象强制转换为“按钮”,然后禁用它。 At the end of your code you could have something like this:在你的代码的最后,你可能有这样的东西:

// 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 

Make sure that you don't have any kind of binding of the button, it may be ignoring property assigment in code behind.确保您没有任何类型的按钮绑定,它可能会忽略后面代码中的属性分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM