Product Spotlight
(0 votes)

Tip: Resizing the Silverlight object from the Silverlight application

6 comments   /   posted by Martin Mihaylov on Aug 19, 2008

Update: The demo, the source code and the code in the article are compatible with Silverlight 2 RTW. The only changes I made were to the object element in the host page.

This tip is about adjusting the size of the Silverlight object in the Html to the size of the content in our Silverlight application. The example I will use will manipulate only the height, because the width can be manipulated in analogical way.

Let's start with explanation of how we're going to access the Silverlight object in the HTML. We use the HtmlPage object in the codebehind. With its help we invoke a javascript function, which will deal with Silverlight object in the mark up:

private void ResizeSilverlightOnject( double height )
{
    HtmlPage.Window.Invoke( "ResizeObject", new object[] { height } );
}

The function is placed in the markup and is called ResizeObject. It takes as parameter the desired height. Note that to declare the Silverlight plug-in I use a div and an object element. Here it is:

function ResizeObject( height )
{     
    var host = document.getElementById( "silverlightControlHost" );
 
    host.style.height = height + "px";
}

We simply get the div that hosts the Silverlight object and set the height in its style property to the passed height. If you don't want the div to be smaller than the content area of the browser the function should look like that:

function ResizeObject( height )
{     
    var host = document.getElementById( "silverlightControlHost" );
    if( document.documentElement.clientHeight < height )
    {
        host.style.height = height + "px";
    }
    else
    {
        host.style.height = document.documentElement.clientHeight.toString() + "px";
    }
}

Here we use the clientHeight property of the documentElement. It returns the size of the height of the content area of the browser. We compare it to the passed height and in dependance of the result we set the div's height either to the height sent as function's argument or to the clientHeight.

That's it, not hard at all. Hope it solves some problems. Here is also a very simple demo and its source code if you want to see this tip in action. Notice that when the content of the grid grows bigger than the content area of the browser the browser's scrollbar appears. Of course it can be used in other cases. For example a silverlight control placed in the mark up can also be resized depending on the content in it like Silverlightshow's Highlights control.



Comments

Comments RSS RSS
  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by David Roh on Aug 20, 2008 07:28
    Hi Martin,
     
    Thanks for sharing this important tip.
     
    HtmlPage.Window.Invoke("ResizeObject", new object[] { height });
     
    throws   "Failed to Invoke: ResizeObject." exception
     
    on my system - Windows Server 2008 with all of the latest updates for Silverlight and Visual Studio 2008
     
    David Roh
  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by Enrai on Aug 20, 2008 08:33

    Hm...I hadn't updated my studio fully yet, maybe that's the reason. Try adding these attributes to the class and the memebers that are used to access the HTML:

     

    [ScriptableType]
    public partial class Page : UserControl
    {
        public Page(){...}
        
        //the methods that are not involved in accessing the HTML don't need such attributes.
        
        [ScriptableMember]
        private void ResizeSilverlightOnject( double height )
        {
            HtmlPage.Window.Invoke( "ResizeObject", new object[] { height } );
        }
    }

     

    and tell me if it worked. :)

  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by samvayn on Aug 20, 2008 12:59

    The problem actually is in ..\ResizeSilverlightObject\LayoutExperiments\Bin\Debug\TestPage.html - it is missing the function. If running from VisualStudio - then make sure that your Web project is set as a StartUp project - page in it is correct.

     

  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by Enrai on Aug 20, 2008 13:27

    Yes, I used the TestPage in the Web Site porject while writing the example. In the automatically generated HTML page in the Silverlight project the javascript function is missing.

  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by samvayn on Aug 20, 2008 13:31

    The problem actually is in ..\ResizeSilverlightObject\LayoutExperiments\Bin\Debug\TestPage.html - it is missing the function. If running from VisualStudio - then make sure that your Web project is set as a StartUp project - page in it is correct.

     

  • RE: Tip: Resizing the Silverlight object from the Silverlight application  

    posted by Anna on Aug 21, 2008 05:29

    Thanks a lot! My boss wanted me to do something similar and I just did it with your tutorial.

     

    math-geek-rock-chick.blogspot.com

Add Comment