3

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.

SPQR3
  • 647
  • 7
  • 20
  • Move your VSM above the elements you're trying to manipulate it with, aka... Move it above your rectangles and may as well even above your columndefinitions, like it shows you in the tut. Oh and change "CurrentViewState" to the visualstate name of what you're switching too... – Chris W. Dec 04 '14 at 20:54
  • Didn't work unfortunately. – SPQR3 Dec 04 '14 at 20:58
  • You changed currentviewstate to like Portrait and it didn't? Guess I'll have to actually read your whole thing in a min. – Chris W. Dec 04 '14 at 20:59
  • Yes, CurrentViewState is either "Landscape" or "Portrait". – SPQR3 Dec 04 '14 at 21:00
  • The tutorial doesn't work either if Grid is not the direct child of Page. – SPQR3 Dec 04 '14 at 21:04
  • 1
    Yupp, would have helped if I had taken the time to actually read it and see "my Grid is not the top element" lol, glad you got your remedy. – Chris W. Dec 04 '14 at 21:22

1 Answers1

12

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.

SPQR3
  • 647
  • 7
  • 20
  • 1
    Thanks so much, I could have wasted hours on this! – James Wright Apr 10 '16 at 16:50
  • Now I just need to figure out if this also applies if `VisualStateManager` is part of a UserControl... Does the UserControl need to be a direct child? Does the VSM need to be direct child within the UserControl? ...gonna update when I find something – MOnsDaR Jun 26 '18 at 15:09