Desperately Seeking Love of Sophie

Using Browser Host to Resize the Page Size

with 3 comments

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();
}

 

 

Written by Vivek

May 16, 2007 at 8:23 pm

Posted in Professional, Software

Tagged with

3 Responses

Subscribe to comments with RSS.

  1. System.Windows.Interop.BrowserHost.ActualWidth from Silverigth 1.1 alpha doesn’t work well in Firefox ..

    Michael Sync

    February 15, 2008 at 2:27 am

  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.

    Braulio

    March 16, 2008 at 5:09 pm


Leave a Reply