Tuesday, September 4, 2012

Orientation Changed Behavior

Orientation Changed Behavior

I've touched with such situation. I have a behavior:

public sealed class OrientationChangedBehavior : Behavior<PhoneApplicationPage>     {         public string LandscapeStateName { get; set; }             public string PortraitStateName { get; set; }                     protected override void OnAttached()         {             base.OnAttached();                 AssociatedObject.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(AssociatedObject_OrientationChanged);         }                     protected override void OnDetaching()         {             base.OnDetaching();                             AssociatedObject.OrientationChanged -= new EventHandler<OrientationChangedEventArgs>(AssociatedObject_OrientationChanged);                     }             private void AssociatedObject_OrientationChanged(object sender, OrientationChangedEventArgs e)         {             string sState = null;             if (e.Orientation == PageOrientation.Landscape || e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)             {                 sState = LandscapeStateName;             }             else             {                 sState = PortraitStateName;             }             VisualStateManager.GoToState(AssociatedObject, sState, true);         }     } 

I attached it to the page:

<behavior:OrientationChangedBehavior LandscapeStateName="LandscapeState"                                       PortraitStateName="PortraitState" /> 

And added some states:

<VisualStateManager.VisualStateGroups>     <VisualStateGroup x:Name="OrientationStates">         <VisualState x:Name="LandscapeState">         <Storyboard>             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scrollViewer" Storyboard.TargetProperty="VerticalScrollBarVisibility" >                 <DiscreteObjectKeyFrame KeyTime="0" Value="Hidden" />             </ObjectAnimationUsingKeyFrames>     </VisualState>     <VisualState x:Name="PortraitState">         <Storyboard>             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="VerticalScrollBarVisibility" Storyboard.TargetName="scrollViewer">                  <DiscreteObjectKeyFrame KeyTime="0" Value="Disabled" />              </ObjectAnimationUsingKeyFrames>         </Storyboard>     </VisualState>     </VisualStateGroup>     </VisualStateManager.VisualStateGroups> 

LayoutRoot has ScrollViewer:

<ScrollViewer x:Name="scrollViewer" Grid.Row="1" > 

But the change of states do not proceed though the method VisualStateManager.GoToState(AssociatedObject, sState, true); has run. Why?

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog