Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #9 – 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,
--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

Silverlight Tips of the Day - Blog by Mike Snow said:

Let’s say you have a Silverlight application that you want to be scaled to the same width and height

# August 27, 2008 1:23 AM

ueqt said:

SilverlightTipoftheDay#9

# December 10, 2008 2:21 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

??????????????? ??? ?????? « jin_u as blog said:

Pingback from  ??????????????? ??? ?????? &laquo; jin_u as blog

# March 24, 2009 10:02 PM