Exemplo n.º 1
0
        /// <summary>
        /// Sets the content of the size to.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetSizeToContent(DependencyObject element, bool value)
        {
            bool oldValue = WebViewService.GetSizeToContent(element);

            element.ThrowIfNull(nameof(element)).SetValue(WebViewService.SizeToContentProperty, value);

            if (!oldValue && value)
            {
                WebView view = (WebView)element;
                view.NavigationCompleted += async(s, e) =>
                {
                    if (e.IsSuccess)
                    {
                        string heightString = await view.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });

                        int height;
                        if (int.TryParse(heightString, out height))
                        {
                            view.Height = height;
                        }

                        string widthString = await view.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });

                        int width;
                        if (int.TryParse(widthString, out width))
                        {
                            view.Width = width;
                        }
                    }
                };
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the HTML.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="html">The HTML.</param>
        public static void SetHtml(DependencyObject element, string html)
        {
            string oldHtml = WebViewService.GetHtml(element);

            element.SetValue(WebViewService.HtmlProperty, html);
            if (string.IsNullOrEmpty(oldHtml) && !string.IsNullOrEmpty(html))
            {
                WebView view = (WebView)element.ThrowIfNull(nameof(element));
                view.Loaded += (s, e) => view.NavigateToString(html);
            }
        }