Mike Snows Silverlight Blog

Game Programming with Silverlight 2.0

Tip of the Day #9 – Browser Resize – How to capture the size of your browser when it is resized.

Now that Beta 2 is almost out the door I will start delivering more tutorials related to game development. I apologize for the delay but I have been swamped!

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 your align your elements to the browsers borders, center your character on the screen, etc.

In Beta1 there was a BrowserHost object that could be used but is no longer available in Beta2. For Beta 2, 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:

   1: void Content_Resized(object sender, EventArgs e)
   2: {
   3:     double height = App.Current.Host.Content.ActualHeight;
   4:     double width = App.Current.Host.Content.ActualHeight;
   5: }

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Now that Beta 2 is almost out the door I will start delivering more tutorials related to game development

# June 6, 2008 12:25 PM

yassir.2 said:

when should we expect the beta 2 ??

# June 6, 2008 1:06 PM

mike.snow said:

Very soon, no later than the end of this week.

# June 6, 2008 1:13 PM

BenHayat said:

>>Very soon, no later than the end of this week.<<

Is "end" of the week considered to be "Friday" or "Saturday" or "Sunday". Some places, Saturday is the beginning of a new week and some places Sunday is the first day of the week. :-)

# June 6, 2008 1:56 PM

yelong said:

we DO have this resized event in beta1, don't we?

# June 6, 2008 1:58 PM

yelong said:

Confused...

We DO have this resized event in Beta1, don't we?

# June 6, 2008 2:00 PM

mike.snow said:

Yelong- Yes, I am publishing this technique for those who used the BrowserHost object which is now removed.

As for the release date eow I am referring to USA PST.

# June 6, 2008 2:45 PM

CraigN said:

USA PST is a timezone, not a day of the week. Is it Friday or Sunday?

# June 6, 2008 3:40 PM

Community Blogs said:

Some reading while waiting for B2--- John Stockton on Intranet Installations, Jesse Liberty on the Popup

# June 6, 2008 6:18 PM

trickyt said:

"I am referring to USA PST"

So it should be released in the next 45 minutes? :)

# June 6, 2008 7:14 PM

mike.snow said:

It's released!

# June 7, 2008 12:12 PM

Dew Drop – June 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &ndash; June 7, 2008 | Alvin Ashcraft's Morning Dew

# June 7, 2008 5:02 PM

jasonbsteele said:

Does the resize event fire when the page is first loaded? If not, then how can we tell how to initially size our content?

Thanks, Jason

# June 9, 2008 3:27 AM

mike.snow said:

Jason- Yes, it fires on load.

# June 26, 2008 4:35 PM

14 Silverlight Tips | DavideZordan.net said:

Pingback from  14 Silverlight Tips | DavideZordan.net

# July 2, 2008 4:31 AM