0

我有一些内容要阅读,加载到 webbrowser 控件中。我想在阅读时看到进度。是否可以以某种方式在 webbrowser 控件中显示垂直滚动条?尝试ScrollViewer.VerticalScrollVisibility将整个网络浏览器放入 ScrollViewer,没有任何效果!

4

2 回答 2

1

根据 MisterGoodcat 的想法查看我的解决方案。我创建了一个自定义 Web 浏览器控件,它在页面加载后注入 javascript。

XAML:

<UserControl x:Class="Wallet.Views.Controls.WebBrowserWithScrollbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">

<Grid>
    <phone:WebBrowser 
            Name="WebBrowserControl"
            IsScriptEnabled="True"
            Margin="0 0 5 0"
            ScriptNotify="WebBrowser_OnScriptNotify">

    </phone:WebBrowser>

    <ScrollBar x:Name="DisplayScrollBar"
                Orientation="Vertical"
                HorizontalAlignment="Right"
                VerticalAlignment="Stretch"
                Minimum="0"
                Maximum="100"
                Value="0" 
                Visibility="Collapsed"/>
</Grid>

后面的代码:

/// <summary>
/// Adds a vertical scrollbar to the normal WebBrowser control
/// </summary>
public partial class WebBrowserWithScrollbar : UserControl
{
    #region Dependency Properties
    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
        "Source", typeof(string), typeof(WebBrowserWithScrollbar), new PropertyMetadata(string.Empty, OnSourcePropertyChanged));

    #endregion

    #region Fields
    private int _visibleHeight;
    private int _scrollHeight;
    #endregion

    #region Constructors
    public WebBrowserWithScrollbar()
    {
        InitializeComponent();
        this.LoadCompleted += this.WebBrowserControlLoadCompleted;
    }
    #endregion

    #region Event Handlers

    public event LoadCompletedEventHandler LoadCompleted
    {
        add
        {
            WebBrowserControl.LoadCompleted += value;
        }

        remove
        {
            WebBrowserControl.LoadCompleted -= value;
        }
    }

    public event EventHandler<NotifyEventArgs> ScriptNotify
    {
        add
        {
            WebBrowserControl.ScriptNotify += value;
        }

        remove
        {
            WebBrowserControl.ScriptNotify -= value;
        }
    }

    public event EventHandler<NavigationEventArgs> Navigated
    {
        add
        {
            WebBrowserControl.Navigated += value;
        }

        remove
        {
            WebBrowserControl.Navigated -= value;
        }
    }

    public event EventHandler<NavigatingEventArgs> Navigating
    {
        add
        {
            WebBrowserControl.Navigating += value;
        }

        remove
        {
            WebBrowserControl.Navigating -= value;
        }
    }

    public event NavigationFailedEventHandler NavigationFailed
    {
        add
        {
            WebBrowserControl.NavigationFailed += value;
        }

        remove
        {
            WebBrowserControl.NavigationFailed -= value;
        }
    }

    #endregion

    #region Properties
    public string Source
    {
        get
        {
            return (string)this.GetValue(SourceProperty);
        }

        set
        {
            this.SetValue(SourceProperty, value);
        }
    }
    #endregion

    #region Static Methods

    private static void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var controlWrapper = d as WebBrowserWithScrollbar;
        if (controlWrapper != null)
        {
            var webBrowser = controlWrapper.WebBrowserControl;
            var newSource = e.NewValue as string;
            Uri uri;

            if (webBrowser != null &&
                newSource != null &&
                e.NewValue != e.OldValue &&
                !string.IsNullOrEmpty(newSource) &&
                Uri.TryCreate(newSource, UriKind.Absolute, out uri))
            {
                webBrowser.Source = uri;
            }
        }
    }

    #endregion

    #region Methods
    private void WebBrowserControlLoadCompleted(object sender, NavigationEventArgs e)
    {
        this.InitializeScrollBarScript();
    }

    private void WebBrowser_OnScriptNotify(object sender, NotifyEventArgs e)
    {
        var parts = e.Value.Split('=');
        if (parts.Length != 2)
        {
            return;
        }

        int number;
        if (!int.TryParse(parts[1], out number))
        {
            return;
        }

        if (parts[0] == "scrollHeight")
        {
            _scrollHeight = number;
            if (_visibleHeight > 0)
            {
                DisplayScrollBar.Maximum = _scrollHeight - _visibleHeight;
            }
        }
        else if (parts[0] == "clientHeight")
        {
            _visibleHeight = number;
            if (_scrollHeight > 0)
            {
                DisplayScrollBar.Maximum = _scrollHeight - _visibleHeight;
            }
        }
        else if (parts[0] == "scrollTop")
        {
            DisplayScrollBar.Value = number;
        }

        this.DisplayScrollBar.Visibility = this._visibleHeight >= this._scrollHeight ? Visibility.Collapsed : Visibility.Visible;
    }

    private void InitializeScrollBarScript()
    {
        try
        {
            WebBrowserControl.InvokeScript(
                "eval",
                new[]
                    {
                        "function onScroll() { " + "var scrollPosition = document.body.scrollTop;"
                        + "window.external.notify(\"scrollTop=\" + scrollPosition.toString());"
                        + "window.external.notify(\"scrollHeight=\" + document.body.scrollHeight.toString());"
                        + "window.external.notify(\"clientHeight=\" + document.body.clientHeight.toString()); } "

                        + "window.external.notify(\"scrollHeight=\" + document.body.scrollHeight.toString());"
                        + "window.external.notify(\"clientHeight=\" + document.body.clientHeight.toString()); "
                        + "window.onscroll = onScroll"
                    });
        }
        catch (Exception xcp)
        {
            Debug.WriteLine("Exception occured while executing invoke script:");
            Debug.WriteLine(xcp);
            DisplayScrollBar.Visibility = Visibility.Collapsed;
        }
    }
    #endregion
}
于 2014-09-25T19:30:04.857 回答
0

..没有人回答..
但我已经找到了解决方案。不是很理想,而且有点脏,但它可以工作,我现在可以显示阅读进度。http://www.pitorque.de/MisterGoodcat/post/Somethings-Missing-from-the-WebBrowser-Control.aspx主要思想是在页面中注入一些javascript并通知浏览器滚动事件;之后更改放置在浏览器附近某处的自定义滚动条控件的位置。

于 2013-01-06T09:30:39.283 回答