簡體   English   中英

Xamarin.Forms 將 IsEnable 設置為 true 不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM