Windows Store WebView rendered html size

谁说我不能喝 提交于 2019-12-19 11:58:22

问题


I have a strong need to calculate/get the rendered html size in WebView.

Is there a way to achieve this?

In Objective-C i could pass a HTML string to the WebView and available size, and it returned the desired Size.

Is there something similar to this in Windows Store WebView?

For example:

WebView webView = new WebView();
webView.NavigateToString(html);
webView.ContentLoaded+= webView_ContentLoaded(,);

webView_ContentLoaded(,)
{
    //Height=99999999 - does the trick, like available height is infinite
    //Width=200 - means that width is limited to 200px
    Size desiredSize = webView.Content.Measure(new Size{Height=99999999, Width=200}());
}

And based on desiredSize value i could resize my WebView to prevent scrolling inside the WebView, and fit all the content on XAML page.


回答1:


Ok, i have came up with a solution:

I injected this string into my html :

string injectionStyleAndScript = @"html,body {margin: 0;padding: 0;height: 100%;}function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify(''+height);}";

That means insert it before

htmlFragment – this is HTMl that i had to place on my WebView.
htmlFragment = htmlFragment.Insert(htmlFragment.IndexOf("</HEAD>"),injectionStyleAndScript); // Inser the Injection

Next thing, to calculate the body insides, i wrap it

Find the indexes from and to:

int from = htmlFragment.IndexOf("<BODY>")+6;
int to = htmlFragment.IndexOf("</BODY>");

Next everything inside the wrap into .

string bodyWrapper = string.Format("<div id='wrapper'>{0}</div>", htmlFragment.Substring(from, (htmlFragment.Length - 1 - from)-(htmlFragment.Length - 1 - to)));

Next, replace the old content with a new one ( that we created inside the wrapper ):

//Cut out the old one
htmlFragment = htmlFragment.Remove(from, (htmlFragment.Length - 1 - from) - (htmlFragment.Length - 1 - to));
//Insert our wrapper
htmlFragment = htmlFragment.Insert(from, bodyWrapper);
//Navigate to the HTML
MyWebView.NavigateToString(htmlFragment);

Next we need to attach 2 events to the WebView - DOMContentLoaded и ScriptNotify:

MyWebView.ScriptNotify += MyWebView;
MyWebView.DOMContentLoaded += MyWebView;
MyWebView.NavigateToString(htmlFragment);

And here are the two event handlers:

void MyWebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
    MyWebView.InvokeScriptAsync("getHeight", null);
}

void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
    string result = e.Value.ToString();
    MyWebView.Height = double.Parse(result);
}



回答2:


I have had to change the injection script to this:

string injectionStyleAndScript = @"<style> html,body {margin: 0;padding: 0;height: 100%;} </style><script type='text/javascript'>function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify(''+height);}</script>";


来源:https://stackoverflow.com/questions/29127890/windows-store-webview-rendered-html-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!