Skip to content

Paging Overview

Rico Suter edited this page Jul 8, 2015 · 5 revisions
  • Package: MyToolkit.Extended
  • Platforms: WP7SL, WP8SL, WinRT, UWP

Improved paging classes for Universal Windows Apps and Windows Phone Silverlight apps.

WinRT and UWP

For Universal Windows Apps (UWP) and Windows Phone Apps

The Page, Frame, SuspensionManager and other classes have been reimplemented to avoid the annoying page caching problem (see this StackOverflow Question) and to add more features - for example asynchronous navigation methods.

The idea was to completely replace the original Frame and Page class usages through the new implementations. The replacement classes provide the exact same functionality plus more advanced features which help build more robust apps.

Check out the following classes for more information about the new features:

Usage

There are two ways to replace the original initialization of the frame object with the new logic for instantiating an MtFrame:

  • (Recommended) Replace the Application class with MtApplication: Completely replace the application class so that the MtApplication class can hook into the appropriate events and abstract away all logic to create the root frame.
  • Replace the Frame class with MtFrame: Only replace the instantiation of the frame object through the new code.

In both cases, all pages have to inherit from the MtPage class instead of the original page class and all usages of the NavigationHelper class must be replaced with the MtNavigationHelper class:

<paging:MtPage
    x:Class="MyPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Rezeptewiki.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:paging="using:MyToolkit.Paging"
    mc:Ignorable="d">
...
</paging:MtPage>

(Recommended) Replace the Application class with MtApplication

Open your App.xaml and replace the Application class with the MtApplication class:

<paging:MtApplication
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:paging="using:MyToolkit.Paging">
...
</paging:MtApplication>

In the application's code-behind, implement the abstract member StartPageType and override OnInitializedAsync if needed:

public sealed partial class App
{
    public App()
    {
        InitializeComponent();

#if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
            DebugSettings.EnableFrameRateCounter = true;
#endif
    }

    public override Type StartPageType
    {
        get { return typeof(MainPage); }
    }

    public override Task OnInitializedAsync(MtFrame frame, ApplicationExecutionState e)
    {
        // TODO: Called when the app is started (not resumed)
        return null;
    }
}

Full sample: App.xaml.cs

Replace the Frame class with MtFrame

To use the new paging classes without changing the application class, perform the following steps:

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
	var rootFrame = Window.Current.Content as MtFrame;
	if (rootFrame == null)
	{
		rootFrame = new MtFrame();
		MtSuspensionManager.RegisterFrame(rootFrame, "AppFrame");
		if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
		{
			try
			{
				await MtSuspensionManager.RestoreAsync();
			}
			catch { }
		}
		Window.Current.Content = rootFrame;
			
		// TODO initialize application (same as OnInitializedAsync above)
	}
	if (rootFrame.Content == null)
	{
		if (# rootFrame.Initialize(typeof(MainPage)))
			throw new Exception("Failed to create initial page");
	}
	Window.Current.Activate();
}

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    await MtSuspensionManager.SaveAsync();
    deferral.Complete();
}

Issues / known problems

Do not add NavigationThemeTransition to ContentTransitions of MtFrame as it is not supported. Use the PageAnimation property of MtFrame for page transition animations. Adding NavigationThemeTransition causes a "Invalid pointer" exception on application start.

Windows Phone Silverlight

Improved and added overridable life cycle methods and page animations.

Classes:

Clone this wiki locally