【问题标题】:UWP XAML VisualState does not have an accessible setterUWP XAML VisualState 没有可访问的设置器
【发布时间】:2016-03-12 17:46:11
【问题描述】:

我的MainPage 中有一些Button,如下所示:

<Grid Grid.Row="1">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10"  >
            <Image Stretch="None" Source="Images/GameMainMenuIcon.png" Margin="0 0 0 50" />
            <Button Style="{StaticResource ButtonGameLarge}">Button1</Button>
            <Button Style="{StaticResource ButtonGameLarge}">Button2</Button>
            <Button Style="{StaticResource ButtonGameLarge}">Button3</Button>
        </StackPanel>
</Grid>

在我的Style.xaml 文件中,我为Buttons 编写了这些样式:

<Style TargetType="Button" x:Key="ButtonGameLarge">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <Setter.Value>
            <VisualStateGroup>

                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="720" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Property="Height" Value="80" />
                        <Setter Property="Width" Value="400" />
                        <Setter Property="Margin" Value="0 0 0 20" />
                    </VisualState.Setters>
                </VisualState>

                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="400" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Property="Height" Value="40" />
                        <Setter Property="Width" Value="200" />
                        <Setter Property="Margin" Value="0 0 0 10" />
                    </VisualState.Setters>
                </VisualState>

            </VisualStateGroup>
        </Setter.Value>
    </Setter>

</Style>

但它给了我这些错误:

  • 属性“VisualStateGroups”没有可访问的设置器。
  • 无法将“VisualStateGroup”分配给属性“VisualStateGroups”,类型必须可分配给“IList”
  • “VisualStateGroups”属性不是 DependencyProperty。要在标记中使用,非附加属性必须通过可访问的实例属性“VisualStateGroups”在目标类型上公开。对于附加属性,声明类型必须提供静态“GetVisualStateGroups”和“SetVisualStateGroups”方法。

如何为一组元素设置多个VisualState

【问题讨论】:

    标签: xaml win-universal-app visualstatemanager visualstategroup


    【解决方案1】:

    附加属性VisualStateManager.VisualStateGroups 只能应用于派生自FrameworkElement 的类型。

    对于你的问题,你可以这样修改样式:

    <Style x:Key="ButtonGameLarge" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup>
                                <VisualState>
                                    <VisualState.StateTriggers>
                                        <AdaptiveTrigger MinWindowWidth="720" />
                                    </VisualState.StateTriggers>
                                    <VisualState.Setters>
                                        <Setter Target="RootGrid.Height" Value="80" />
                                        <Setter Target="RootGrid.Width" Value="400" />
                                        <Setter Target="RootGrid.Margin" Value="0,0,0,20" />
                                    </VisualState.Setters>
                                </VisualState>
    
                                <VisualState>
                                    <VisualState.StateTriggers>
                                        <AdaptiveTrigger MinWindowWidth="400" />
                                    </VisualState.StateTriggers>
                                    <VisualState.Setters>
                                        <Setter Target="RootGrid.Height" Value="40" />
                                        <Setter Target="RootGrid.Width" Value="200" />
                                        <Setter Target="RootGrid.Margin" Value="0,0,0,10" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    在我的代码中,我们可以将您的VisualStateManager.VisualStateGroups 附加到Button 内部的RootGrid,该FrameworkElement 派生自FrameworkElement

    【讨论】:

      【解决方案2】:

      您应该在控件模板中设置视觉状态,而不是多次重复使用该控件。在这里你可以找到examples

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        • 2018-06-02
        相关资源
        最近更新 更多