通常我们都是把Silverlight插件嵌入到Html代码中的:
我们的Silverlight代码如何取得整个浏览器的大小呢?你可不要告诉我用Application.Host:
double height=Application.Current.Host.Content.ActualHeight; double width=Application.Current.Host.Content.ActualWidth;
这样取得的只是SL插件的大小,也就是id=“slplugin”的大小。正确的方法是调用浏览器JavaScript代码获取浏览器的大小,使用Eval:
double width = (double)HtmlPage.Window.Eval("document.documentElement.clientWidth"); double height = (double)HtmlPage.Window.Eval("document.documentElement.clientHeight");
这是最简单的方法,当然使用Invoke调用JS函数也行,稍显麻烦了,还需要事先写个Js函数到网页里。
JS:
function GetBrowserWidth() { return document.documentElement.clientWidth; } function GetBrowserHeight() { return document.documentElement.clientHeight; }
C#:
double width = (double)HtmlPage.Window.Invoke("GetBrowserWidth"); double height = (double)HtmlPage.Window.Invoke("GetBrowserHeight");