如何在UWP App中使用此AdaptiveTrigger

时间:2016-08-03 13:10:43

标签: c# xaml windows-phone-8.1 uwp windows-10-universal

我正在撰写 UWP应用

我将 SplitView (用于汉堡包)添加到页面。

SplitView窗格中,我有两个项目:

  • SellButtonGrid
  • SellButtonGridAlternate

以上两项的可见性取决于两个条件

  • 最低520 Windows高度
  • 和SplitViewPane已打开或关闭。

这意味着4个案例:

  
      
  1. Windows高度低于520且splitviewpane已关闭
  2.   
  3. Windows高度小于520,splitviewpane打开

  4.   
  5. Windows高度大于520且splitviewpane已关闭

  6.   
  7. Windows高度大于520且splitviewpane打开
  8.   

对于splitview打开或关闭,我使用:

<VisualState x:Name="SplitViewOpen">
  <VisualState.StateTriggers>
  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen}" />
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Visible" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Collapsed" />
  </VisualState.Setters>
</VisualState>
<VisualState x:Name="SplitViewClosed">
  <VisualState.StateTriggers>
  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen, Converter={StaticResource BooleanNegationConverter}}" />
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
  </VisualState.Setters>
</VisualState>

为了检测低于或高于520的高度,我使用:

<VisualState x:Name="SmallHeight">
  <VisualState.StateTriggers>
  <AdaptiveTrigger MinWindowHeight="0" />
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
  </VisualState.Setters>
</VisualState>
<VisualState x:Name="LargeHeight">
  <VisualState.StateTriggers>
  <AdaptiveTrigger MinWindowHeight="520" />
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Visible" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Collapsed" />
  </VisualState.Setters>
</VisualState>

但是,结果不是我的预期。 如何合并这些视觉状态??

我尝试使用: http://dotnetbyexample.blogspot.in/2016/02/an-adaptivetrigger-that-works-with.html

我使用上面的解决方案,所以: 我的返工代码:

xmlns:Utils="using:DellApp.Utils"
xmlns:converters="using:DellApp.Converters"
xmlns:windowsStateTriggers="using:WindowsStateTriggers"

新创建的州:

  <VisualState.StateTriggers>
  <windowsStateTriggers:CompositeStateTrigger>
  <Utils:AdaptiveTrigger MinWindowHeight="520" MaxWindowHeight="18000" />

  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen,
  Converter={StaticResource BooleanNegationConverter}}" />
  </windowsStateTriggers:CompositeStateTrigger>
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
  </VisualState.Setters>
</VisualState>

<VisualState x:Name="SplitViewCollapsed_SmallHeight">
  <VisualState.StateTriggers>
  <windowsStateTriggers:CompositeStateTrigger>
  <Utils:AdaptiveTrigger MaxWindowHeight="519"
  MinWindowHeight="0" />
  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen,
  Converter={StaticResource BooleanNegationConverter}}" />
  </windowsStateTriggers:CompositeStateTrigger>
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
  </VisualState.Setters>
</VisualState>

<VisualState x:Name="SplitViewOpen_SmallHeight">
  <VisualState.StateTriggers>
  <windowsStateTriggers:CompositeStateTrigger>
  <Utils:AdaptiveTrigger MaxWindowHeight="519"
  MinWindowHeight="0" />
  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen}" />
  </windowsStateTriggers:CompositeStateTrigger>
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
  </VisualState.Setters>
</VisualState>

<VisualState x:Name="SplitViewOpen_LargeHeight">
  <VisualState.StateTriggers>
  <windowsStateTriggers:CompositeStateTrigger>
  <Utils:AdaptiveTrigger MinWindowHeight="520" MaxWindowHeight="18000"/>
  <StateTrigger IsActive="{Binding ElementName=LayoutSplitView,
  Path=IsPaneOpen}" />
  </windowsStateTriggers:CompositeStateTrigger>
  </VisualState.StateTriggers>
  <VisualState.Setters>
  <Setter Target="SellButtonGrid.Visibility" Value="Visible" />
  <Setter Target="SellButtonGridAlternate.Visibility" Value="Collapsed" />
  </VisualState.Setters>
</VisualState>

所以,我做的是,我添加了CompositeStateTrigger from WindowsStateTriggers,但我无法触发它。

1 个答案:

答案 0 :(得分:1)

我根据你的代码制作了一个演示并重现了这个问题。经过研究,我发现了以下问题:

  1. Binding不适用于StateTrigger。但x:Bind效果很好,因此您可以使用x:Bind,而不要忘记为Mode=OneWay指定x:Bind
  2. Operator="And"需要在windowsStateTriggers:CompositeStateTrigger上指定。
  3. 所以你的XAML应该修改如下:

    <Page.Resources>
        <converter:BooleanNegationConverter x:Name="BooleanNegationConverter" x:Key="BooleanNegationConverter"/>
    </Page.Resources>
    
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState>
                    <VisualState.StateTriggers>
                    <windowsStateTriggers:CompositeStateTrigger Operator="And">//use Operator='And' here
                            <Utils:AdaptiveTrigger MinWindowHeight="520" MaxWindowHeight="18000" />
                            <StateTrigger IsActive="{x:Bind LayoutSplitView.IsPaneOpen,Mode=OneWay,Converter={StaticResource BooleanNegationConverter}}" />
                    </windowsStateTriggers:CompositeStateTrigger>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
                    <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
                </VisualState.Setters>
                </VisualState>
    
                <VisualState x:Name="SplitViewCollapsed_SmallHeight">
                    <VisualState.StateTriggers>
                        <windowsStateTriggers:CompositeStateTrigger Operator="And">
                            <Utils:AdaptiveTrigger MaxWindowHeight="519" MinWindowHeight="0" />
                            <StateTrigger IsActive="{x:Bind LayoutSplitView.IsPaneOpen,Mode=OneWay,Converter={StaticResource BooleanNegationConverter}}" />
                        </windowsStateTriggers:CompositeStateTrigger>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
                        <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
                    </VisualState.Setters>
                </VisualState>
    
                <VisualState x:Name="SplitViewOpen_SmallHeight">
                    <VisualState.StateTriggers>
                        <windowsStateTriggers:CompositeStateTrigger Operator="And">
                            <Utils:AdaptiveTrigger MaxWindowHeight="519" MinWindowHeight="0" />
                            <StateTrigger IsActive="{x:Bind LayoutSplitView.IsPaneOpen}" />
                        </windowsStateTriggers:CompositeStateTrigger>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="SellButtonGrid.Visibility" Value="Collapsed" />
                        <Setter Target="SellButtonGridAlternate.Visibility" Value="Visible" />
                    </VisualState.Setters>
                </VisualState>
    
                <VisualState x:Name="SplitViewOpen_LargeHeight">
                    <VisualState.StateTriggers>
                        <windowsStateTriggers:CompositeStateTrigger Operator="And">
                            <Utils:AdaptiveTrigger MinWindowHeight="520" MaxWindowHeight="18000"/>
                            <StateTrigger IsActive="{x:Bind LayoutSplitView.IsPaneOpen}" />
                        </windowsStateTriggers:CompositeStateTrigger>
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="SellButtonGrid.Visibility" Value="Visible" />
                        <Setter Target="SellButtonGridAlternate.Visibility" Value="Collapsed" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    

    以下是我制作的示例:AdaptiveTriggerSample