【发布时间】:2022-06-15 06:26:50
【问题描述】:
我的 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();
}
}
MainPage.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
我仍然得到不受欢迎的行为。
【问题讨论】:
-
AFAIK,顺序不重要。 BindCommand 不应更改 Button IsEnabled。对我来说看起来像毛伊岛虫。
-
@ToolmakerSteve 好的,谢谢。如果明天之前没有人提供理由,我会将其作为错误提交。