SPQR3
SPQR3

Reputation: 636

VisualStateManager GoToState does not work

When the screen's orientation changes I call this:

        string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();

        // Trigger the Visual State Manager
        bool success = VisualStateManager.GoToState(this, CurrentViewState, true);

In my XAML there is a simple GRID (deep in the page's hierarchy)

            <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Grid.Row="2">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Rectangle x:Name="rect_1" Grid.Column="0" Fill="Blue"></Rectangle>
                <Rectangle x:Name="rect_2" Grid.Column="1" Fill="Red"></Rectangle>
                <Rectangle x:Name="rect_3" Grid.Column="2" Fill="Green"></Rectangle>

                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState x:Name="Landscape">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames
      Storyboard.TargetName="rect_1"
      Storyboard.TargetProperty="Fill">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Brown"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>

                        </VisualState>

                        <VisualState x:Name="Portrait">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames
      Storyboard.TargetName="rect_1"
      Storyboard.TargetProperty="Fill">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>

            </Grid>

So it is 3 simple rectangles, but their colour never changes. All this is taken straight from the tutorial here:http://msdn.microsoft.com/en-us/library/windows/apps/dn495655.aspx The only difference is that my Grid is not the top element of the Page. The problem might be that the first parameter of

VisualStateManager.GoToState(this, CurrentViewState, true);

is "this". But I have no clue what it should be, setting it to the grid or one of the rectangles is not allowed.

        IList<VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(this);
        int count= visualStateGroups.Count;      

count is 0.

Upvotes: 3

Views: 3086

Answers (1)

SPQR3
SPQR3

Reputation: 636

I found out: VisualStateManager has to be the direct child of the direct child of Page.

<Page>
    <Grid>
        <VisualStateManager...>
        </VisualStateManager...>

        <!-- deep in the hierarchy somewhere -->
                     <Grid>
                          <Rectangle/>
                          <Rectangle/>
                          <Rectangle/>
                     </Grid>
    </Grid>
</Page>

I tried adding it as the direct child of Page, but didn't work. I don't get at all what the logic is behind this.

Upvotes: 12

Related Questions