简体   繁体   English

.Net Maui Button.IsEnabled 在启动时未禁用

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

I have this button in my MainPage.cs and am using CommunityToolkit.Maui.Markup;我的 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 in my viewmodel's constructor and is toggled by another buttons command. CallButtonEnabled = false在我的视图模型的构造函数中,并由另一个按钮命令切换。

When I use the extension methods in this order, the button is enabled at program start.当我按此顺序使用扩展方法时,该按钮在程序启动时启用。 I think this is because the command's button enabling mechanics are overriding the value I manually set.我认为这是因为命令的按钮启用机制覆盖了我手动设置的值。

If I change the extention method's order like so如果我像这样更改扩展方法的顺序

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

with the BindCommand coming first, the button is now disabled at program start;随着BindCommand首先出现,该按钮现在在程序启动时被禁用; confirming my suspicion.证实了我的怀疑。

My question being;我的问题是; is this just something that I have to be aware of, making sure that the .BindCommand is called first, or is there another way to get the results I want?这只是我必须注意的事情,确保首先调用.BindCommand ,还是有其他方法可以获得我想要的结果?

EDIT 6-14-22 Here is my minimal reproducible example as requested.编辑 6-14-22这是我要求的最小可重现示例。

MauiProgram.cs 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();
   }
}

MainPage.cs主页.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 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");
   }
}

These three files are the only changes I made to the default .NET Maui template, except for: deleting MainPage.xaml , renaming MainPage.xaml.cs -> MainPage.cs , and installing the NuGet packages used in the above code.这三个文件是我对默认 .NET Maui 模板所做的唯一更改,除了:删除MainPage.xaml 、重命名MainPage.xaml.cs -> MainPage.cs以及安装上述代码中使用的 NuGet 包。

I tested this example on我测试了这个例子

Microsoft Visual Studio Community 2022 (64-bit) - Preview; Microsoft Visual Studio Community 2022(64 位)- 预览版; Version 17.3.0 Preview 2.0版本 17.3.0 预览版 2.0

and am still getting the undesired behavior.我仍然得到不受欢迎的行为。

According to my test , we just need to set BindingContext before creating the binding relationship , in this case no matter we put BindCommand before or after , it will work as expected .根据我的测试,我们只需要在创建binding关系之前设置BindingContext ,这样不管我们把BindCommand放在前面还是后面,都可以正常工作。

Sample code示例代码

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-2024 STACKOOM.COM