Developing for Modern Windows

Tips, tricks, and guides for developing on modern Windows platforms

Tip: Place Code before InitializeComponent

Did you know you can place code before the InitializeComponent line in a Windows app?

Usually it’s taken for granted that an app page constructor does this:

public MainPage()

{

this.InitializeComponent();

// your code goes here

}

this.InitializeComponent(); is what sets up your XAML page, and it makes sense that it is called first.

But…you don’t have to wait for the XAML to load before doing some work, especially if that work affects the XAML.
Consider this:

public MainPage()

{

    this.InitializeComponent();

    // modify XAML properties for UI colours here

}

UI that is already drawn by InitializeComponent won’t have its colours automatically changed in Windows Store apps. Instead you could do this:

public MainPage()

{
// modify XAML properties for UI colours here

    this.InitializeComponent();

}

Now your UI will draw with the modified colours. This is great for theming and branding.

I’m sure you can find other great uses for this little trick.

 

Leave a Reply

Your email address will not be published.