简体   繁体   中英

How do I use VisualStateManager correctly?

my Code for changing properties does not work and I have absolutely no idea whats wrong, but maybe you do.

Here is an simplified example of my Code, which reproduces the error. This is my Xaml-Code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="MyButton"
            Height="100" 
            Width="300" 
            Content="Click" 
            FontSize="40" 
            FontWeight="Bold" 
            VerticalAlignment="Center" 
            HorizontalAlignment="Center" 
            Background="Red" Click="MyButton_Click"/>
</Grid>
<VisualStateManager.VisualStateGroups>
    <VisualStateGroup>
        <VisualState x:Name="Blue">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyButton"
                                               Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Aqua"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

This is the Code-Behind:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void MyButton_Click(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Blue", true);
    }
}

In theory, this should change the Button-Color to "Aqua" when clicking it, but nothing happens.

Place the content inside ContentControl and apply VisualState on the control instead on Page.

Also instead of ObjectAnimation , use ColorAnimation to animate the color of Button.

<ContentControl x:Name="contentControl">
    <ContentControl.Template>
        <ControlTemplate>
            <Grid
              Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                <Button x:Name="MyButton"
                        Height="100" 
                        Width="300" 
                        Content="Click" 
                        FontSize="40" 
                        FontWeight="Bold" 
                        VerticalAlignment="Center" 
                        HorizontalAlignment="Center" 
                        Background="Red" Click="Button_Click"/>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CustomGroups">
                        <VisualState x:Name="Blue">
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="MyButton" 
                                    Storyboard.TargetProperty="Background.Color"
                                    To="Aqua"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

and in code behind, pass content control instead of Page:

VisualStateManager.GoToState(contentControl, "Blue", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM