Wednesday, February 11, 2009

Silverlight Tip of the Day # 7 – How to Capture the Size of your Browser when it is Resized.

For this tutorial, I will be showing you how to capture the size of your browser when it is resized. This is essential if you want to align your elements to the browsers borders, center your character on the screen, etc.

To accomplish this we will simply attach a resized event handler for the App.Current.Host.Content object.

Page.xaml.cs:

public Page()
{
    InitializeComponent();
 
    App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
}

From here, we can capture the size of your browser when it changes:

void Content_Resized(object sender, EventArgs e)
{
     double height = App.Current.Host.Content.ActualHeight;
     double width = App.Current.Host.Content.ActualWidth;
}

Thank you, 

0 comments:

Post a Comment