Archive for May 16th, 2007
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(); }