gpt4 book ai didi

android - 无法更改按钮的 IsEnabled 状态

转载 作者:行者123 更新时间:2023-11-28 23:26:02 25 4
gpt4 key购买 nike

我已经尝试了无数种方法到星期天让它工作,但我显然错过了一些东西,因为我的“Register”按钮是无论我做什么,都不会启用。

在我的 XAML 中:

<Button Text="Register" 
Style="{StaticResource RegularButtonStyle}"
WidthRequest="280"
x:Name="RegisterButton">
<!-- I HAD the following, but it wasn't working so I finally decided to try DataTriggers -->
<!-- bindings:Bi.nd="Clicked RegisterButtonClickedCommand;IsEnabled IsRegisterButtonEnabled"> -->
<Button.Triggers>
<DataTrigger TargetType="Button"
Binding="{Binding IsRegisterButtonEnabled}"
Value="False">
<Setter Property="IsEnabled" Value="False"></Setter>
</DataTrigger>
<DataTrigger TargetType="Button"
Binding="{Binding IsRegisterButtonEnabled}"
Value="True">
<Setter Property="IsEnabled" Value="True"></Setter>
</DataTrigger>
</Button.Triggers>
</Button>

在 View 模型中:

public class RegisterViewModel : MvxViewModel
{
...

private bool _isRegisterButtonEnabled;
public bool IsRegisterButtonEnabled
{
get => ShouldEnableRegisterButton();

set
{
_isRegisterButtonEnabled = value;
SetProperty(ref _isRegisterButtonEnabled, value);
}
}

...
public IMvxCommand RegisterButtonClickedCommand { get; private set; }
...

private void InitializeCommands()
{
...
RegisterButtonClickedCommand = new MvxCommand(RegisterUser);
}

...

private bool ShouldEnableRegisterButton()
{
var isValidUser = _userName.Validate();
var isValidPass = _password.Validate();
var isValidConfirmedPass = _confirmedPassword.Validate();

var shouldEnable = isValidUser && isValidPass && isValidConfirmedPass;

_mvxLogger.Log(MvxLogLevel.Trace, () => $"RegisterViewModel : ShouldEnableRegisterButton() called. Returning: {shouldEnable}");

return shouldEnable;
}

...
}

我已经阅读了 MvvmCross“文档”,但它全都是 session 式的,我找不到任何绑定(bind)到按钮的 IsEnabled 属性的具体示例,并且具有足够的特异性让我到达那里。

当然会感谢一些帮助。 :)

客房服务信息:我正在使用以下 NuGet 包 (/libraries)
  • MvvmCross & MvvmCross.Forms v6.3.1
  • Xamarin.Forms v4.0.0.497661
  • Xamarin.Android.* 库均为 v28.0.0.1
然后想到我应该更新,因为有更新可用,所以我更新到以下;
  • MvvmCross & MvvmCross.Forms v6.4.1
  • Xamarin.Forms v4.2.0.848062
  • Xamarin.Android.* 库现在都是 v28.0.0.3

还使用 .Net Standard v2.0.3 来共享内容。

最佳答案

你可以试试这个:

public class RegisterViewModel : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;
private bool _isRegisterButtonEnabled;

public bool IsRegisterButtonEnabled
{
set { _isRegisterButtonEnabled = value; PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(nameof(IsRegisterButtonEnabled))); }
get { return _isRegisterButtonEnabled; }
}
}

然后在Xaml中

<Button Text="Register" 
Style="{StaticResource RegularButtonStyle}"
IsEnabled = "{Binding IsRegisterButtonEnabled}"
WidthRequest="280"
x:Name="RegisterButton">
</Button>

关于android - 无法更改按钮的 IsEnabled 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com