簡體   English   中英

.Net Maui Button.IsEnabled 在啟動時未禁用

[英].Net Maui Button.IsEnabled not disabled at startup

我的 MainPage.cs 中有這個按鈕,並且正在using CommunityToolkit.Maui.Markup;

new Button()
.Bind(Button.IsEnabledProperty, nameof(vm.CallButtonEnabled))
.Bind(Button.TextProperty, nameof(vm.CallButtonText))
.BindCommand(nameof(vm.CallCommand))

CallButtonEnabled = false在我的視圖模型的構造函數中,並由另一個按鈕命令切換。

當我按此順序使用擴展方法時,該按鈕在程序啟動時啟用。 我認為這是因為命令的按鈕啟用機制覆蓋了我手動設置的值。

如果我像這樣更改擴展方法的順序

new Button()
.BindCommand(nameof(vm.CallCommand))
.Bind(Button.IsEnabledProperty, nameof(vm.CallButtonEnabled))
.Bind(Button.TextProperty, nameof(vm.CallButtonText))

隨着BindCommand首先出現,該按鈕現在在程序啟動時被禁用; 證實了我的懷疑。

我的問題是; 這只是我必須注意的事情,確保首先調用.BindCommand ,還是有其他方法可以獲得我想要的結果?

編輯 6-14-22這是我要求的最小可重現示例。

MauiProgram.cs

using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Markup;

namespace MauiApp1;

public static class MauiProgram
{
   public static MauiApp CreateMauiApp()
   {
      var builder = MauiApp.CreateBuilder();
      builder
         .UseMauiApp<App>()
         .UseMauiCommunityToolkit()
         .UseMauiCommunityToolkitMarkup()
         .ConfigureFonts(fonts =>
         {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
         });

      builder.Services.AddSingleton<MainPage, MainPage>();
      builder.Services.AddSingleton<MainPageViewModel, MainPageViewModel>();

      return builder.Build();
   }
}

主頁.cs

using CommunityToolkit.Maui.Markup;

namespace MauiApp1;

public class MainPage : ContentPage
{
   public MainPage(MainPageViewModel vm)
   {
      BindingContext = vm;

      Content = new ScrollView
      {
         Content = new VerticalStackLayout
         {
            Children =
            {
               new Button
               {
                  Text = "Toggle ther button enabled"
               }
               .BindCommand(nameof(vm.ButtonClickedCommand)),

               // This button works as expected and is disabled at startup
               //new Button()
               //.BindCommand(nameof(vm.DisplayAlertCommand))   // <-----
               //.Bind(Button.TextProperty, nameof(vm.ButtonText))
               //.Bind(Button.IsEnabledProperty, nameof(vm.ButtonEnabled)),
                    
               // This button is enabled at startup but the text still reads "Disabled"
               // On first click of the toggle button, this text changes to "Enabled" and the Button is still enabled
               // Everything works as expected on subsequest pressess of the toggle button.
               new Button ()
               .Bind(Button.TextProperty, nameof(vm.ButtonText))
               .Bind(Button.IsEnabledProperty, nameof(vm.ButtonEnabled))
               .BindCommand(nameof(vm.DisplayAlertCommand)),   // <-----
            }
         }
      };
   }
}

MainPageViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace MauiApp1;

public partial class MainPageViewModel : ObservableObject
{
   [ObservableProperty]
   private bool _buttonEnabled = false;

   [ObservableProperty]
   private string _buttonText = "Disabled";

   [RelayCommand]
   private void OnButtonClicked()
   {
      ButtonEnabled = !ButtonEnabled;
      ButtonText = ButtonEnabled ? "Enabled" : "Diabled";
   }

   [RelayCommand]
   private async void DisplayAlert()
   {
      await Application.Current.MainPage.DisplayAlert("", "Other Button Clicked", "OK");
   }
}

這三個文件是我對默認 .NET Maui 模板所做的唯一更改,除了:刪除MainPage.xaml 、重命名MainPage.xaml.cs -> MainPage.cs以及安裝上述代碼中使用的 NuGet 包。

我測試了這個例子

Microsoft Visual Studio Community 2022(64 位)- 預覽版; 版本 17.3.0 預覽版 2.0

我仍然得到不受歡迎的行為。

根據我的測試,我們只需要在創建binding關系之前設置BindingContext ,這樣不管我們把BindCommand放在前面還是后面,都可以正常工作。

示例代碼

ViewModel vm = new ViewModel();
BindingContext = vm;    //look at this line 

Button btn = new Button();

btn.BindCommand(nameof(vm.CallCommand))
   .Bind(Button.IsEnabledProperty, nameof(vm.CallButtonEnabled))
   .Bind(Button.TextProperty, nameof(vm.CallButtonText)); 


//  the following code is also ok
//btn.Bind(Button.IsEnabledProperty, nameof(vm.CallButtonEnabled))
//   .Bind(Button.TextProperty, nameof(vm.CallButtonText))
//   .BindCommand(nameof(vm.CallCommand));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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