Friday, December 19, 2008

Silverlight 2.0 Resize using Resized Event

I remember doing this in Silverlight 1.0, when Silverlight 1.0 released, I thought of giving it a try, I wanted to make sure that Silverlight application that I create should resize it self based on browser size. I am very much particular about width and height for the web sites and web applications, as well as for any other applications. I do not like pixel values much, I prefer working in terms of %, as it makes my application layouts dynamic and adapt to the preferred size of the user. I remember doing this kind of thing in Silverlight 1.0 using Javascript, discovering how to do it was a bit difficult but it was easy to do in Silverlight 1.0

Recently, I started writing an article for my web site article on Menubar in Silverlight, and I wanted to do layout in %s format or dynamic format. This time I was using Silverlight 2.0, which means using languages like C# or VB.NET, this is where I really thought it will be really easy to do that, and yes it is.. following code will do that.

in C#

public Page()
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);

        void Content_Resized(object sender, EventArgs e)
            this.Width = Application.Current.Host.Content.ActualWidth;
            this.Height = Application.Current.Host.Content.ActualHeight;


Public Sub New()
       AddHandler App.Current.Host.Content.Resized, AddressOf Resized
   End Sub

   Private Sub Resized(ByVal sender As Object, ByVal e As EventArgs)
       Me.Width = Application.Current.Host.Content.ActualWidth
       Me.Height = Application.Current.Host.Content.ActualHeight
   End Sub

Without resize you will see your Silverlight Application as follows:


Silverlight application with resizing


No comments: