本文整理汇总了C#中Windows.UI.Xaml.Controls.WebView.InvokeScript方法的典型用法代码示例。如果您正苦于以下问题:C# WebView.InvokeScript方法的具体用法?C# WebView.InvokeScript怎么用?C# WebView.InvokeScript使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.UI.Xaml.Controls.WebView
的用法示例。
在下文中一共展示了WebView.InvokeScript方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetWebPages
IEnumerable<FrameworkElement> GetWebPages(WebView webView, Windows.Foundation.Size page)
{
// ask the content its width
var widthString = webView.InvokeScript("eval",
new[] { "document.body.scrollWidth.toString()" });
int contentWidth;
if (!int.TryParse(widthString, out contentWidth))
throw new Exception(string.Format("failure/width:{0}", widthString));
webView.Width = contentWidth;
// ask the content its height
var heightString = webView.InvokeScript("eval",
new[] { "document.body.scrollHeight.toString()" });
int contentHeight;
if (!int.TryParse(heightString, out contentHeight))
throw new Exception(string.Format("failure/height:{0}", heightString));
webView.Height = contentHeight;
// how many pages will there be?
var scale = page.Width / contentWidth;
var scaledHeight = (contentHeight * scale);
var pageCount = (double)scaledHeight / page.Height;
pageCount = pageCount + ((pageCount > (int)pageCount) ? 1 : 0);
// create the pages
var pages = new List<Windows.UI.Xaml.Shapes.Rectangle>();
for (int i = 0; i < (int)pageCount; i++)
{
var translateY = -page.Height * i;
var _page = new Windows.UI.Xaml.Shapes.Rectangle
{
Height = page.Height,
Width = page.Width,
Margin = new Thickness(5),
Tag = new TranslateTransform { Y = translateY },
};
_page.Loaded += (s, e) =>
{
var rectangle = s as Windows.UI.Xaml.Shapes.Rectangle;
var brush = GetWebViewBrush(webView);
brush.Stretch = Stretch.UniformToFill;
brush.AlignmentY = AlignmentY.Top;
brush.Transform = rectangle.Tag as TranslateTransform;
rectangle.Fill = brush;
};
pages.Add(_page);
}
return pages;
}
示例2: GetWebViewBrush
WebViewBrush GetWebViewBrush(WebView webView)
{
// resize width to content
var originalWidth = webView.Width;
var widthString = webView.InvokeScript("eval",
new[] { "document.body.scrollWidth.toString()" });
int contentWidth;
if (!int.TryParse(widthString, out contentWidth))
throw new Exception(string.Format("failure/width:{0}", widthString));
webView.Width = contentWidth;
// resize height to content
var originalHeight = webView.Height;
var heightString = webView.InvokeScript("eval",
new[] { "document.body.scrollHeight.toString()" });
int contentHeight;
if (!int.TryParse(heightString, out contentHeight))
throw new Exception(string.Format("failure/height:{0}", heightString));
webView.Height = contentHeight;
// create brush
var originalVisibilty = webView.Visibility;
webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
var brush = new WebViewBrush
{
SourceName = webView.Name,
Stretch = Stretch.Uniform
};
brush.Redraw();
// reset, return
webView.Width = originalWidth;
webView.Height = originalHeight;
webView.Visibility = originalVisibilty;
return brush;
}
示例3: GetUserAgent
/// <summary>
/// Uses WebView control to get the user agent string
/// </summary>
/// <returns></returns>
private static Task<string> GetUserAgent()
{
var tcs = new TaskCompletionSource<string>();
WebView webView = new WebView();
string htmlFragment =
@"<html>
<head>
<script type='text/javascript'>
function GetUserAgent()
{
return navigator.userAgent;
}
</script>
</head>
</html>";
webView.LoadCompleted += (sender, e) =>
{
try
{
//Invoke the javascript when the html load is complete
string result = webView.InvokeScript("GetUserAgent", null);
//Set the task result
tcs.TrySetResult(result);
}
catch(Exception ex)
{
tcs.TrySetException(ex);
}
};
//Load Html
webView.NavigateToString(htmlFragment);
return tcs.Task;
}