当前位置: 首页>>代码示例>>C#>>正文


C# WebView.InvokeScript方法代码示例

本文整理汇总了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;
        }
开发者ID:NikolayKostadinov,项目名称:TelerikAkademy,代码行数:49,代码来源:MainPage.xaml.cs

示例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;
        }
开发者ID:NikolayKostadinov,项目名称:TelerikAkademy,代码行数:36,代码来源:MainPage.xaml.cs

示例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;
        }
开发者ID:vigneshmurugesan,项目名称:Q42.WinRT,代码行数:43,代码来源:Util.cs


注:本文中的Windows.UI.Xaml.Controls.WebView.InvokeScript方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。