Partilhar via


What does the LayoutAwarePage Class provide?

In this post I’m going to be discussing what’s available in the LayoutAwarePage Class. So First what is that class and how can we get it?

The LayoutAwarePage is a class that Microsoft has developed for us in order to help us in handling the applications Navigation, Visual State Switching and Process Lifetime management. You can get this class if you started developing a windows 8 application and choose either the Grid App template or the Split App template. On the other hand if you started a Blank template you can add that class later on by selecting the add a basic page option instead of the blank page, this option will prompt you that It will need additional classes and ask you if visual studio should add them automatically, if you press yes they will be added automatically for you.

So if I want a button that just goes back I will write the following code in the layout aware page:

 <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>

So the GoBack functionality will be handled automatically for you and the button will be disabled if you can’t go back from this page. You can also use other available Navigation functions like GoHome and GoForward.

Visual State Support

If the user control needs to be able to deal with the state or visual state update you can tie it to these events StartLayoutUpdates and StopLayoutUpdates you can do that as follows:

 <UserControl Loaded=" StartLayoutUpdates" Unloaded=" StopLayoutUpdates" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>

 

State Management (LoadState & SaveState: SuspensionManager)

When your application gets suspended you want to save you application state but you don’t have a lot of time to call a webservice or save to a database, this can be solved by using the Load state and save state methods all what you have to do is override this two methods to tell your application what to do in each of this situations. You can use them as follows:

 protected override void SaveState(System.Collections.Generic.Dictionary<string, object> pageState)
 {
 var virtualizingStackPanel = 
 VisualTreeUtilities.GetVisualChild<VirtualizingStackPanel>(itemGridView);
 
 if (virtualizingStackPanel != null && pageState != null)
 {
 pageState["virtualizingStackPanelHorizontalOffset"] = virtualizingStackPanel.HorizontalOffset;
 }
 }
 
 
 protected override void LoadState(object navigationParameter, 
 System.Collections.Generic.Dictionary<string, object> pageState)
 {
 if (pageState != null && pageState.ContainsKey("virtualizingStackPanelHorizontalOffset"))
 {
 double.TryParse(pageState["virtualizingStackPanelHorizontalOffset"].ToString(),
 out virtualizingStackPanelHorizontalOffset);
 }
 }

In the above example I am saving the scrollbar location so that when the application gets resumed the user will find it in the same location he left it.

To sum up

These are the available events:

               protected virtual void GoHome(object sender, RoutedEventArgs e)

               protected virtual void GoBack(object sender, RoutedEventArgs e)

               protected virtual void GoForward(object sender, RoutedEventArgs e)

Visual state switching

               public void StartLayoutUpdates(object sender, RoutedEventArgs e)

               public void StopLayoutUpdates(object sender, RoutedEventArgs e)

Process lifetime management

               protected virtual void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)

               protected virtual void SaveState(Dictionary<String, Object> pageState)