【问题标题】:UWP VisualStates in UserControlUserControl 中的 UWP VisualStates
【发布时间】:2016-07-04 12:38:54
【问题描述】:

我很难理解如何正确使用 UWP VisualStateManager。我已经定义了一个包含一些简单视觉状态的 UserControl,这里是 XAML:

<UserControl
    x:Class="BingSearch.Views.UserControls.VerticalsTabHeader"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Margin="8 0 8 0"
    mc:Ignorable="d" IsTabStop="False">
    <UserControl.Resources>
        <VisualStateGroup x:Name="CommonStates">
            <VisualStateGroup.Transitions>
                <VisualTransition From="Normal" To="Minimal" 
                          GeneratedDuration="0:0:0.2">
                </VisualTransition>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="Normal">
            </VisualState>
            <VisualState x:Name="Minimal">
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="BubbleGrid" 
                    Storyboard.TargetProperty="Opacity" From="1" To="0" />
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </UserControl.Resources>
    <StackPanel>
        <Grid x:Name="BubbleGrid">
            <Ellipse Width="60" Height="60" Fill="Black" Stroke="White" StrokeThickness="1" />
            <FontIcon
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Glyph="{Binding Glyph}"
                Foreground="White"
                FontSize="26" />
        </Grid>
    </StackPanel>
</UserControl>

在我后面的代码中,当我调用VisualStateManager.GoToState(this, "Minimal", true);或VisualStateManager.GoToState(this, "Normal", true);时,什么也没有发生。调用的返回值始终为 false,CommonStates.CurrentState 始终为 null。似乎我的视觉状态从未“连接”到用户控件。这不是为 UserControls 定义视觉状态的正确方法吗?

【问题讨论】:

    标签: xaml windows-runtime uwp


    【解决方案1】:

    要想在UserControl中正确使用VisualStateManager,需要做以下两件事:

    首先你需要将你的 VisualStateGroup 包装在

    <VisualStateManager.VisualStateGroups>
    ...
    </VisualStateManager.VisualStateGroups>
    

    之后,您需要将 VisualStateManager 设置为 UserControl 中 Root 控件的直接子项,如下所示:

    <StackPanel>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualStateGroup.Transitions>
                    <VisualTransition From="Normal" To="Minimal" 
                          GeneratedDuration="0:0:0.2">
                    </VisualTransition>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="Normal">
                </VisualState>
                <VisualState x:Name="Minimal">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="BubbleGrid" 
                    Storyboard.TargetProperty="Opacity" From="1" To="0" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid x:Name="BubbleGrid">
            <Ellipse Width="60" Height="60" Fill="Black" Stroke="White" StrokeThickness="1" />
            <FontIcon
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Glyph="{Binding Glyph}"
                Foreground="White"
                FontSize="26" />
        </Grid>
    </StackPanel>
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2016-06-29
      • 2016-04-18
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多