InvokeScript() public method

public InvokeScript ( [ scriptName, [ arguments ) : string
scriptName [
arguments [
return string
        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;
        }
        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;
        }
Exemplo n.º 3
0
        /// <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;
        }