Professional, Software

Using Browser Host to Resize the Page Size

For one of the application I am building, I needed to resize my root page canvas size according to the size of the Silverlight control.

There is a class called System.Windows.Interop.BrowserHost that exposes some of the OM on the Silverlight control. As part of those APIs, you get ActualHeight/ActualWidth as well as resize events.

My first take at this code was just to set the rootCanvas.Height and rootCanvas.Width to ActualHeight and Actual Width. This actually did not work because when Loaded event is fired for the Root Canavas, control has not yet received its finalHeight and final Width. So what i get in the loaded event is 0/0.

So to do this you need to hook into resize event that Browser host exposes and this event will actually fire twice and second time around the height/width seems to be the final one. This also means, my canvas gets resized for all subsequent browser resize (that is because i set control dimensions as 100% instead of fixed size).

public void Page_Loaded(object o, EventArgs e)
{
    // Required to initialize variables
    InitializeComponent();
    System.Windows.Interop.BrowserHost.Resize += new EventHandler(BrowserHost_Resize);
}

void BrowserHost_Resize(object sender, EventArgs e)
{
    Height = System.Windows.Interop.BrowserHost.ActualHeight;
    Width = System.Windows.Interop.BrowserHost.ActualWidth;
    UpdateLayout();   
}

 

 

Advertisements
Standard

3 thoughts on “Using Browser Host to Resize the Page Size

  1. Pingback: Recursive Reflection : Using Browserhost to Resize the page size

  2. I’m looking for resize at control level (drag with the mouse).

    I read a drag and drop sample long time ago, but dono if I should try to apply that resize encapsulated inside a control (e.g. rubberBand) or just manage it at parent canvas level.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s