Click here to Skip to main content
11,629,481 members (66,966 online)
Click here to Skip to main content

Dynamically resize WebBrowser-control in Silverlight4 for Windows Phone

, 27 Jan 2011 CPOL 13.3K 4
Rate this:
Please Sign up or sign in to vote.
Dynamic WebBrowser size
Add this JavaScript to the end of your HTML which should placed in the WebBrowser-Control:
<script>
function ResizeWebBrowser() {
    var _renderedHeight=document.body.firstChild.offsetHeight * 0.5; // because -ms-text-size-adjust: 150%;
    var _json = '{""rendered_height"" : '+_renderedHeight+'}';
    window.external.notify(_json);
}
ResizeWebBrowser();
</script>

On the Control, activate JavaScript and register a action for ScriptNotify:
<phone:WebBrowser Name="Browser" Height="100" IsScriptEnabled="True" ScriptNotify="WebBrowser_ScriptNotify" IsHitTestVisible="False" />

Now we can resize our WebBrowser-Control:
private void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    Dictionary<String, String> _json = (Dictionary<String, String>)JsonConvert.DeserializeObject(e.Value, typeof(Dictionary<String, String>));
    if (_json.ContainsKey("rendered_height"))
    {
        this.Browser.Height = Int32.Parse(_json["rendered_height"]);
    }
}

That's all and it works perfectly for me. With the Option IsHitTestVisible="False" the WebBrowser-Control can't zoom and scroll and it feels like a Formatted TextBlock.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arogarth
Software Developer Shopgate GmbH
Germany Germany
Interface Developer for eCommerce to mCommerce

Knwoledge:
C/C++, C#, ASP.NET, JAVA, Prolog, PHP, SQL, (N)Hibernate, HTML, CSS, SAP ABAP, Linux, Windows (nearly all Versions)

You may also be interested in...

Comments and Discussions

 
QuestionSir, Need help from you..!!! Pin
Vijaydhas26-Jan-15 22:13
memberVijaydhas26-Jan-15 22:13 
QuestionVery nice Pin
Farhad Jabiyev12-Aug-13 7:20
memberFarhad Jabiyev12-Aug-13 7:20 
GeneralReason for my vote of 5 It seems it's one of the best approa... Pin
Siavash Mortazavi28-Jan-11 3:42
memberSiavash Mortazavi28-Jan-11 3:42 
QuestionI'lost which SL4 in WP7, Did I miss something? Pin
Juan Pablo G.C.28-Jan-11 4:04
memberJuan Pablo G.C.28-Jan-11 4:04 
GeneralWP7 Silverlight Pin
mvermef28-Jan-11 3:20
membermvermef28-Jan-11 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 27 Jan 2011
Article Copyright 2011 by Arogarth
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid