简体   繁体   English

我有一个 Xamarin 形式的 MultiTrigger,对于一个按钮,它不起作用

[英]I have a MultiTrigger in Xamarin form, for a button, it's not working

I have a create-account page in my PCL, with a button that depends on several entry fields.我的 PCL 中有一个创建帐户页面,其中有一个取决于多个输入字段的按钮。

I want to enable the Create button only when the fields have the proper input.我只想在字段具有正确输入时启用“创建”按钮。 Here's my markup:这是我的标记:

<Button Text="Create Account" Clicked="CreateAccountButton_Clicked" IsEnabled="False">
     <Button.Triggers>
          <MultiTrigger TargetType="Button">
               <MultiTrigger.Conditions>
                    <BindingCondition Binding="{Binding Source={x:Reference EmailValidator}, Path=IsValid}" Value="True" />
                    <BindingCondition Binding="{Binding Source={x:Reference PasswordValidator}, Path=IsValid}" Value="True" />
               </MultiTrigger.Conditions>
               <Setter Property="IsEnabled" Value="True" />
          </MultiTrigger>
     </Button.Triggers>
</Button>

I have verified that the validators are setting their IsValid properties to True, yet the button is NOT being enabled.我已验证验证器将其 IsValid 属性设置为 True,但未启用该按钮。 I am using an Android emulator and Visual Studio 2019.我正在使用 Android 模拟器和 Visual Studio 2019。

The code above is identical to several examples I found online.上面的代码与我在网上找到的几个示例相同。

Am I missing something?我错过了什么吗?

In your Xaml :在您的 Xaml 中:

<ResourceDictionary>
   <local:MultiTriggerConverter x:Key="DataHasBeenEnteredTrigger" />
</ResourceDictionary>

My Example :我的例子:

<Button IsEnabled="False" Text="{localization:Translate login}" Margin="25,0" TextColor="{StaticResource color-fourth}" BackgroundColor="Yellow" Opacity="0.5" FontAttributes="Bold" Command="{Binding GoConnexionCommand}" >
                <Button.Triggers>
                    <MultiTrigger TargetType="Button">
                        <MultiTrigger.Conditions>
                            <BindingCondition Binding="{Binding Source={x:Reference user},
                                      Path=Text.Length,
                                      Converter={StaticResource DataHasBeenEnteredTrigger}}"
                                      Value="true" />
                            <BindingCondition Binding="{Binding Source={x:Reference password},
                                      Path=Text.Length, 
                                      Converter={StaticResource DataHasBeenEnteredTrigger}}"
                                      Value="true" />
                </MultiTrigger.Conditions>
            <Setter Property="IsEnabled" Value="True" />
        <Setter Property="Opacity" Value="1" />
    </MultiTrigger>
</Button.Triggers>

Convert Class:转换类:

public class MultiTriggerConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return false;

        if ((int)value > 0) return true;            
         else return false;   // Input is empty
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Test this and tell me what it looks like!测试一下并告诉我它的样子!

Best regrads最佳成绩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM