Custom Page Template for Windows apps

In almost all Windows 8 or phone project involved in we use a create custom page class which derives from the Page class. Doing this allows you centralize functionality such as:

  • Providing a global appbar across your entire app
  • Centralized isloading indicator management
  • Setting different visual states depending on screen resolution

It's very simple to do. Almost too simple. You create a class in your solution inheriting the Page class:

namespace MyApp.Base
{
public abstract class PageBase : Page
{
 protected PageBase()
 {
 // Create global appbar 
 // Hook into this.SizeChanged to set visual states depending on screen width // Create centralized loading mechanism
 }
}
}

To use the new class called PageBase you need to inherit and use that on your xaml.cs and xaml pages. Replace your code with this;

<base:PageBase
xmlns:base="using:MyApp.Base"
....

and in you code behind you should have this instead;

public sealed partial class MainPage : PageBase

That's all it takes guys! - Using his simple maneuver you can now extend the functionality as much as you like and save your self a ton of duplicated code across pages inside your app and it only takes 5 minutes to set up. 

Pro tip: You can do the exact same thing for UserControl's!

Feel free to let me know if you have any issues with the implementation.