第二十三章:触发器和行为(六)

MultiTrigger中的组合条件
Trigger和DataTrigger都有效地监视属性以确定它是否等于特定值。 这称为触发器的条件,如果条件为真,则调用Setter对象的集合。
作为程序员,您可能会开始怀疑是否可以在触发器中具有多个条件。 但是一旦你开始谈论多个条件,你需要确定是否要将条件与逻辑OR运算或AND运算结合起来 - 如果任何条件为真,是否调用触发器,或者是否需要所有条件是真的。
如果您希望在多个条件都为真时调用触发器 - 逻辑AND情况 - 这是从TriggerBase派生的四个类中的最后一个。 MultiTrigger定义了两个集合属性:

  • IList类型的Conditions
  • IList类型的setter

Condition是一个抽象类,有两个实现类:

  • PropertyCondition,具有属性和值属性,如Trigger
  • BindingCondition,它具有像DataTrigger这样的Binding和Value属性

您可以在MultiTrigger的相同Conditions集合中混合多个PropertyCondition和BindingCondition对象。 当所有条件都为真时,将应用Setters集合中的所有Setter对象。
让我们看一个简单的例子:在AndConditions程序中,四个Switch元素与蓝色BoxView共享页面。 当所有Switch元素都打开时,BoxView变为红色:
2019_04_04_151719
XAML文件显示了如何完成此操作。 BoxView的Triggers集合包含MultiTrigger。 TargetType属性是必需的。 Conditions集合包含四个BindingCondition对象,每个对象引用四个Switch元素之一的IsToggled属性并检查True值。 如果所有条件都为真,则MultiTrigger将BoxView的Color属性设置为Red:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AndConditions.AndConditionsPage">
    <StackLayout>
        <Grid VerticalOptions="CenterAndExpand">
            <Switch x:Name="switch1" Grid.Column="0"
                    HorizontalOptions="Center" />
 
            <Switch x:Name="switch2" Grid.Column="1"
                    HorizontalOptions="Center" />
 
            <Switch x:Name="switch3" Grid.Column="2"
                    HorizontalOptions="Center" />
 
            <Switch x:Name="switch4" Grid.Column="3"
                    HorizontalOptions="Center" />
        </Grid>
        <BoxView WidthRequest="100"
                 HeightRequest="100"
                 VerticalOptions="CenterAndExpand"
                 HorizontalOptions="Center"
                 Color="Blue">
            <BoxView.Triggers>
                <MultiTrigger TargetType="BoxView">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference switch1},
                                                            Path=IsToggled}"
                                          Value="True" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch2},
                                                            Path=IsToggled}"
                                          Value="True" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch3},
                                                            Path=IsToggled}"
                                          Value="True" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch4},
                                                            Path=IsToggled}"
                                          Value="True" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Color" Value="Red" />
                </MultiTrigger>
            </BoxView.Triggers>
        </BoxView>
    </StackLayout>
</ContentPage>

那就是AND组合。 OR组合怎么样?
因为Triggers集合可以容纳多个DataTrigger对象,所以您可能认为这可以工作:

<BoxView WidthRequest="100"
         HeightRequest="100"
         VerticalOptions="CenterAndExpand"
         HorizontalOptions="Center"
         Color="Blue">
    <BoxView.Triggers>
        <DataTrigger TargetType="BoxView"
                     Binding="{Binding Source={x:Reference switch1}, Path=IsToggled}"
                     Value="True">
            <Setter Property="Color" Value="Red" />
        </DataTrigger>
        <DataTrigger TargetType="BoxView"
                     Binding="{Binding Source={x:Reference switch2}, Path=IsToggled}"
                     Value="True">
            <Setter Property="Color" Value="Red" />
        </DataTrigger>
        <DataTrigger TargetType="BoxView"
                     Binding="{Binding Source={x:Reference switch3}, Path=IsToggled}"
                     Value="True">
            <Setter Property="Color" Value="Red" />
        </DataTrigger>
 
        <DataTrigger TargetType="BoxView"
                     Binding="{Binding Source={x:Reference switch4}, Path=IsToggled}"
                     Value="True">
            <Setter Property="Color" Value="Red" />
        </DataTrigger>
    </BoxView.Triggers>
</BoxView>

如果你尝试它,你可能会发现它似乎确实起作用。 但是当你进一步尝试打开和关闭各种开关元件时,你会发现它确实不起作用。
它是否应该起作用或不起作用是值得商榷的。 四个DataTrigger对象都以相同的Color属性为目标,如果每个DataTrigger独立工作以确定是否应该应用该Setter,那么这实际上不应该作为逻辑OR。
但是,请记住维多利亚时代的数学家奥古斯都德摩根的逻辑定律,其中说明了(使用C#语法进行AND,OR,逻辑否定和等价):

? | ? == ! (! ? & !?)
? & ? == ! (!? | !?)

这意味着您可以使用MultiTrigger执行逻辑OR,如OrConditions程序演示:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OrConditions.OrConditionsPage">
    <StackLayout>
        <Grid VerticalOptions="CenterAndExpand">
            <Switch x:Name="switch1" Grid.Column="0"
                    HorizontalOptions="Center" />
            <Switch x:Name="switch2" Grid.Column="1"
                    HorizontalOptions="Center" />
            <Switch x:Name="switch3" Grid.Column="2"
                    HorizontalOptions="Center" />
            <Switch x:Name="switch4" Grid.Column="3"
                    HorizontalOptions="Center" />
        </Grid>
        <BoxView WidthRequest="100"
                 HeightRequest="100"
                 VerticalOptions="CenterAndExpand"
                 HorizontalOptions="Center"
                 Color="Red">
            <BoxView.Triggers>
                <MultiTrigger TargetType="BoxView">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference switch1},
                                                            Path=IsToggled}"
                                          Value="False" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch2},
                                                            Path=IsToggled}"
                                          Value="False" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch3},
                                                            Path=IsToggled}"
                                          Value="False" />
 
                        <BindingCondition Binding="{Binding Source={x:Reference switch4},
                                                            Path=IsToggled}"
                                          Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Color" Value="Blue" />
                </MultiTrigger>
            </BoxView.Triggers>
        </BoxView>
    </StackLayout>
</ContentPage>

除了所有逻辑都被翻转之外,它与AndConditions相同。 所有BindingCondition对象都会检查IsToggled属性的False值,如果满足所有条件,则通常为红色的BoxView将显示为蓝色:
2019_04_04_152944
这是你可以想到这两个程序的另一种方式:在AndConditions中,除非切换所有Switch元素,否则BoxView始终是蓝色的。 在OrConditions中,除非所有Switch元素都关闭,否则BoxView始终为红色。
假设您有一个涉及两个Entry字段和一个Button的场景。 如果“输入”字段包含某些文本,则需要启用“按钮”。
翻转逻辑:如果两个Entry字段都不包含文本,您确实要禁用Button。 这很容易:

<StackLayout>
    <Entry x:Name="entry1"
           Text="" />
    <Entry x:Name="entry2"
           Text="" />
    <Button Text="Send">
        <Button.Triggers>
            <MultiTrigger TargetType="Button">
                <MultiTrigger.Conditions>
                    <BindingCondition Binding="{Binding Source={x:Reference entry1},
                                                        Path=Text.Length}"
                                     Value="0" />
                    <BindingCondition Binding="{Binding Source={x:Reference entry2},
                                                        Path=Text.Length}"
                                     Value="0" />
                </MultiTrigger.Conditions>
                <Setter Property="IsEnabled" Value="False" />
            </MultiTrigger>
        </Button.Triggers>
    </Button>
</StackLayout>

请注意,两个Entry字段将Text属性初始化为空字符串,以便该属性不等于null。 如果两个Text属性的长度都为零,则满足两个BindingConditions,并将Button的IsEnabled属性设置为False。
但是,只有当两个Entry视图都有一些文本时,才能调整它以启用Button。 如果您尝试翻转逻辑,则必须更改BindingCondition对象,以便它们检查长度不等于零的Text属性,并且这不是一个选项。
为了帮助实现逻辑,可以使用一些中间不可见的Switch元素:

<StackLayout>
    <Entry x:Name="entry1"
           Text="" />
    <Switch x:Name="switch1"
            IsVisible="False">
        <Switch.Triggers>
            <DataTrigger TargetType="Switch"
                         Binding="{Binding Source={x:Reference entry1},
                                           Path=Text.Length}"
                         Value="0">
                <Setter Property="IsToggled" Value="True" />
            </DataTrigger>
        </Switch.Triggers>
    </Switch>
 
    <Entry x:Name="entry2"
           Text="" />
 
    <Switch x:Name="switch2"
            IsVisible="False">
        <Switch.Triggers>
            <DataTrigger TargetType="Switch"
                         Binding="{Binding Source={x:Reference entry2},
                                          Path=Text.Length}"
                         Value="0">
                <Setter Property="IsToggled" Value="True" />
            </DataTrigger>
        </Switch.Triggers>
    </Switch>
    <Button Text="Send"
            IsEnabled="False">
        <Button.Triggers>
            <MultiTrigger TargetType="Button">
                <MultiTrigger.Conditions>
                    <BindingCondition Binding="{Binding Source={x:Reference switch1},
                                                        Path=IsToggled}"
                                      Value="False" />
                    <BindingCondition Binding="{Binding Source={x:Reference switch2},
                                                        Path=IsToggled}"
                                      Value="False" />
                </MultiTrigger.Conditions>
 
                <Setter Property="IsEnabled" Value="True" />
            </MultiTrigger>
        </Button.Triggers>
    </Button>
</StackLayout>

每个Entry现在都有一个伴随Switch,如果Entry的Text属性的长度为零,则使用DataTrigger将其IsToggled属性设置为True。 然后可以在MultiTrigger中使用两个Switch元素。 如果两个Switch元素的IsToggled属性都设置为True,则两个Entry字段都包含一些文本,Button的IsEnabled属性可以设置为True。
如果您想要实际组合AND和OR操作,您需要参与更深层次的逻辑。
例如,假设您有一个具有两个Entry视图和一个Button的场景,并且仅当两个Entry视图中的任何一个包含一些文本时才应启用Button,但如果两个Entry视图都包含一些文本则不应该启用Button:
2019_04_04_154648
也许(如截图所示)其中一个Entry视图用于文件名,另一个用于URL,程序只需要这两个文本字符串中的一个。
你需要的是异或(XOR)操作,它是AND,OR和否定运算符的组合:

? ^ ? == (? | ?) & ! (? & ?)

这可以通过三个MultiTrigger对象完成,其中两个在中间不可见的Switch元素上,最后一个在Button本身上。 这是XorConditions XAML文件,其中包含描述逻辑各个部分的注释:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XorConditions.XorConditionsPage"
             Padding="50, 20">
    <StackLayout>
        <Label Text="Enter:" />
 
        <Entry x:Name="entry1"
               Text=""
               Placeholder="filename" />
        <!-- IsToggled is true if entry1 has no text -->
        <Switch x:Name="switch1"
                IsVisible="False">
            <Switch.Triggers>
                <DataTrigger TargetType="Switch"
                             Binding="{Binding Source={x:Reference entry1},
                                               Path=Text.Length}"
                             Value="0">
                    <Setter Property="IsToggled" Value="True" />
                </DataTrigger>
            </Switch.Triggers>
        </Switch>
        <Label Text="Or:" />
 
        <Entry x:Name="entry2"
               Text=""
               Placeholder="url" />
        <!-- IsToggled is true if entry2 has no text -->
        <Switch x:Name="switch2"
                IsVisible="False">
            <Switch.Triggers>
                <DataTrigger TargetType="Switch"
                             Binding="{Binding Source={x:Reference entry2},
                                               Path=Text.Length}"
                             Value="0">
                    <Setter Property="IsToggled" Value="True" />
                </DataTrigger>
            </Switch.Triggers>
        </Switch>
        <!-- IsToggled is true if either Entry has some text (OR operation) -->

        <Switch x:Name="switch3"
                IsToggled="True"
                IsVisible="False">
            <Switch.Triggers>
                <MultiTrigger TargetType="Switch">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference switch1},
                                                            Path=IsToggled}"
                                          Value="True" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch2},
                                                            Path=IsToggled}"
                                          Value="True" />
                    </MultiTrigger.Conditions>
 
                    <Setter Property="IsToggled" Value="False" />
                </MultiTrigger>
            </Switch.Triggers>
        </Switch>
 
        <!-- IsToggled is true if both Entry's have some text (AND operation) -->
        <Switch x:Name="switch4"
                IsVisible="False">
            <Switch.Triggers>
                <MultiTrigger TargetType="Switch">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference switch1},
                                                            Path=IsToggled}"
                                          Value="False" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch2},
                                                            Path=IsToggled}"
                                          Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="IsToggled" Value="True" />
                </MultiTrigger>
            </Switch.Triggers>
        </Switch>
        <!-- Button is enabled if either Entry has some text but not both (XOR operation) -->
        <Button Text="Load"
                IsEnabled="False"
                FontSize="Large">
            <Button.Triggers>
                <MultiTrigger TargetType="Button">
                    <MultiTrigger.Conditions>
                        <BindingCondition Binding="{Binding Source={x:Reference switch3},
                                                            Path=IsToggled}"
                                          Value="True" />
                        <BindingCondition Binding="{Binding Source={x:Reference switch4},
                                                            Path=IsToggled}"
                                          Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True" />

                </MultiTrigger> 
            </Button.Triggers>
        </Button>
    </StackLayout>
</ContentPage>

当然,一旦XAML获得这种奢侈,如果您只是决定在代码中启用或禁用Button,则没有人会对您造成错误!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值