簡體   English   中英

UWP XAML VisualState沒有可訪問的設置器

[英]UWP XAML VisualState does not have an accessible setter

我的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

您應該在控件模板中設置視覺狀態,然后多次重用控件。 在這里您可以找到示例

附加屬性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附加到RootGrid內的RootGrid上,該Button是從FrameworkElement派生的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM