How to get notified in silverlight application when size of browser changes

by Viper 26. December 2008 09:13

A lot of time we develop content for silverlight applications that depend on size of browser or host application. Silverlight framework exposes some of host application's settings through System.Windows.Interop.SilverlightHost object which are accessible through Host property of Application class of your silverlight application. One of the properties of System.Windows.Interop.SilverlightHost object is System.Windows.Interop.Content. You can subscribe to Resized event of this object to be notified whenever there is a change in browser window.

You can find the value of browser window size from ActualWidth and ActualHeight property of Content object. When host application resizes, silverlight application gets notified through Resized event. You can subscribe to this event when application startup completes. Always access these size values in Resized event because this is time in rendering workflow when these values are actually known. These values are not known before this event. Silverlight framework uses two step process to resize UI elements. In first pass it calculates the sizes and positions. And in second step it actually positions the elements based on calculated sizes. And then final calculations have completed, then actual sizes are known.


private void Application_Startup(object sender, StartupEventArgs e)
{
startPage = new HostInfomation();
this.RootVisual = startPage;
this.Host.Content.Resized += new EventHandler(Content_Resized);
}

private void Content_Resized(Object sender, EventArgs e)
{
 HostInfomation hostInfoPage = this.startPage as HostInfomation;
 hostInfoPage.LayoutRoot.Children.Clear();
 StackPanel stackPanel = new StackPanel();
 stackPanel.Orientation = Orientation.Vertical;
 TextBlock heightBlock = new TextBlock();
 heightBlock.Text = string.Format("Height = {0}", this.Host.Content.ActualHeight);
 TextBlock widthBlock = new TextBlock();
 widthBlock.Text = string.Format("Width = {0}", this.Host.Content.ActualWidth);
 stackPanel.Children.Add(heightBlock);
 stackPanel.Children.Add(widthBlock);
 hostInfoPage.LayoutRoot.Children.Add(stackPanel);
}

Views: 8314

Tags:

ASP.Net | Silverlight

Comments

12/12/2008 11:22:47 AM #

Busby SEO Test Pinay

searching for the best app

Busby SEO Test Pinay United States

1/26/2009 3:23:35 AM #

Replica Ordnance

yet another blog about silverlight. Microsoft must have done an awesome job with this product. I'm considering delving into it after reading so many bloggers blogging about it

Replica Ordnance Australia

3/3/2009 5:04:42 PM #

Doctor is Out

yes i must be change my  silverlight app

Doctor is Out United States

6/29/2009 7:43:02 PM #

dsi r4

I was looking for a better app

dsi r4 United States

8/5/2009 4:53:51 AM #

online earning

Great information.....nice to meet U.

online earning United States

8/13/2009 7:09:32 PM #

Zenith Mens Defy Classic

Hey I love your style I will subscribe for your feed please keep posting!

Zenith Mens Defy Classic United States

8/18/2009 3:20:55 AM #

Web design


The content on this site is unique. A good work done related to tutorials and must be carried on.

Web design United Kingdom

8/18/2009 10:49:24 PM #

seo

Hi,
    What are the system requirements and code for silver light application in asp.net.??????

seo United States

8/20/2009 6:49:16 AM #

Search engine marketing

Hey,
waoo that's really interesting info. I would really love to read more about it...

Search engine marketing United States

8/27/2009 9:35:41 AM #

Baume Mercier Classima

Hmm strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

Baume Mercier Classima United States

9/6/2009 11:51:13 PM #

san diego realestate

Hey,
Great post i found it very interesting and at the same time very informative i will definitely bookmark this site for future reference...

san diego realestate United States

9/24/2009 8:12:02 AM #

Web design

As an Internet marketing strategy, SEO considers how search engines work and what people search for. Optimizing a website primarily involves editing its content and HTML and associated coding to both increase its relevance to specific keywords and to remove barriers to the indexing activities of search engines.
www.webmarketingexperts.com.au/web-design.php

Web design India

10/7/2009 7:34:57 AM #

buy aion online accounts

I digged this for more news from you.



Regards and respect
Mian

buy aion online accounts United States

10/7/2009 7:35:01 AM #

buy cheap aion item

When is the next post comming on this topic.


Regards

Willam

buy cheap aion item United States

5/10/2010 5:12:48 PM #

pingback

Pingback from condosencinitas.com

san diego car insurance

condosencinitas.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli