locked
ApplicationViewStates inside a UserControl RRS feed

  • Question

  • Are the ApplicationViewStates acessible inside a UserControl nested inside a LayoutAwarePage?

    I'm trying to use the same control inside diferent elements.

    thanks!

    Tuesday, August 7, 2012 4:26 PM

Answers

  • You can define the VisualStateGroup for the user control as well as the page root element. But then you have to register the user control as a layout aware control (if you are using layout aware page as a base, if not you have to call GoToState on that specific control). You can try it without the Loaded and Unloaded to compare the result. These methods are inherited from the LayoutAwarePage and are used to register and unregister controls in the list of layout aware controls.

    <controls:MyLayoutAwareControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates"></controls:MyLayoutAwareControl>

    And the user control would look something like this (for instance a control that displays a text in black in full and red in snapped view):

    <UserControl
        x:Class="MySolution.UserControls.MyLayoutAwareControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MySolution.UserControls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <TextBlock Name="MyTextControl" Width="400" FontSize="17" Foreground="Black" Text="Here is the Text Value"></TextBlock>
            <VisualStateManager.VisualStateGroups>
    
                <!-- Visual states reflect the application's view state -->
                <VisualStateGroup x:Name="ApplicationViewStates">
                    <VisualState x:Name="FullScreenLandscape"/>
                    <VisualState x:Name="Filled"/>
                    <VisualState x:Name="FullScreenPortrait" />
                    <VisualState x:Name="Snapped">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyTextControl" Storyboard.TargetProperty="Foreground">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="SharingMode"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Grid>
    </UserControl>

    Hope it helps.


    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Can Bilgin Sunday, August 12, 2012 5:56 AM
    • Marked as answer by Jesse Jiang Tuesday, August 14, 2012 8:53 AM
    Tuesday, August 7, 2012 7:04 PM
  • I bolded the code in the first part...  Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates" on your user control registers the user control for layout updates...

    Can Bilgin
    Blog CompuSight


    • Edited by Can Bilgin Wednesday, August 8, 2012 10:01 AM
    • Marked as answer by Jesse Jiang Tuesday, August 14, 2012 8:53 AM
    Wednesday, August 8, 2012 10:00 AM

All replies

  • You can define the VisualStateGroup for the user control as well as the page root element. But then you have to register the user control as a layout aware control (if you are using layout aware page as a base, if not you have to call GoToState on that specific control). You can try it without the Loaded and Unloaded to compare the result. These methods are inherited from the LayoutAwarePage and are used to register and unregister controls in the list of layout aware controls.

    <controls:MyLayoutAwareControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates"></controls:MyLayoutAwareControl>

    And the user control would look something like this (for instance a control that displays a text in black in full and red in snapped view):

    <UserControl
        x:Class="MySolution.UserControls.MyLayoutAwareControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MySolution.UserControls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <TextBlock Name="MyTextControl" Width="400" FontSize="17" Foreground="Black" Text="Here is the Text Value"></TextBlock>
            <VisualStateManager.VisualStateGroups>
    
                <!-- Visual states reflect the application's view state -->
                <VisualStateGroup x:Name="ApplicationViewStates">
                    <VisualState x:Name="FullScreenLandscape"/>
                    <VisualState x:Name="Filled"/>
                    <VisualState x:Name="FullScreenPortrait" />
                    <VisualState x:Name="Snapped">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyTextControl" Storyboard.TargetProperty="Foreground">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="SharingMode"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Grid>
    </UserControl>

    Hope it helps.


    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Can Bilgin Sunday, August 12, 2012 5:56 AM
    • Marked as answer by Jesse Jiang Tuesday, August 14, 2012 8:53 AM
    Tuesday, August 7, 2012 7:04 PM
  • Thanks Can,

    I'm using a LayoutAwarePage, but it doesn't send that VisualState change to the control. How do I register my control as a LayoutAwareControl on my LayoutAwarePage?

    My VisualStates are already implemented, but the one inside the control isn't working...

    Wednesday, August 8, 2012 9:45 AM
  • I bolded the code in the first part...  Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates" on your user control registers the user control for layout updates...

    Can Bilgin
    Blog CompuSight


    • Edited by Can Bilgin Wednesday, August 8, 2012 10:01 AM
    • Marked as answer by Jesse Jiang Tuesday, August 14, 2012 8:53 AM
    Wednesday, August 8, 2012 10:00 AM
  • If your question has been answered, please mark the appropriate post as answer so other community members with a similar question can find help faster.

    Can Bilgin
    Blog CompuSight

    Monday, August 13, 2012 8:57 PM