如何在 UWP 应用程序中使用此 AdaptiveTrigger

Posted

技术标签:

【中文标题】如何在 UWP 应用程序中使用此 AdaptiveTrigger【英文标题】:How to use this AdaptiveTrigger in UWP App 【发布时间】:2016-12-09 05:42:33 【问题描述】:

我正在编写一个 UWP 应用程序

我在页面中添加了 SplitView(用于汉堡包)。

SplitView Pane中,我有两个项目:

SellButtonGrid SellButtonGridAlternate

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

至少 520 个窗口高度 SplitViewPane 打开或关闭。

这意味着 4 个案例:

    窗口高度小于 520 且拆分视图已关闭

    窗口高度小于 520 且 splitviewpane 已打开

    窗口高度大于 520 且 splitviewpane 关闭

    窗口高度大于 520 且 splitviewpane 已打开

对于 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】:

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

    Binding 不适用于 StateTrigger。但是x:Bind 工作得很好,所以你可以改用x:Bind,不要忘记为x:Bind 指定Mode=OneWayOperator="And" 需要在windowsStateTriggers:CompositeStateTrigger 上指定。

所以你的 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

【讨论】:

是的,成功了!。非常感谢

以上是关于如何在 UWP 应用程序中使用此 AdaptiveTrigger的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中更改 UWP 应用程序中按钮的背景颜色?

如何在 UWP 的应用中打开另一个应用

如何在 UWP 中声明 Haptic 类(VibrationDevice)的权限?

如何在UWP [XAML]中隐藏列格式DataGridTextColumn?

在 UWP 应用中使用 AudioGraph 将音频输出发送到两台设备

如何使用当前 Windows 登录用户验证 UWP 应用程序