我有一些内容要阅读,加载到webbrowser控件中。我希望在阅读时看到进展。是否有可能以某种方式在webbrowser控件中显示垂直滚动条?尝试ScrollViewer.VerticalScrollVisibility
并将整个webbrowser放入ScrollViewer,没有任何作用!
答案 0 :(得分: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
}
答案 1 :(得分:0)
..没有人回答..
但我已经找到了解决方案。不是很理想,有点脏,但它有效,我现在可以显示阅读进度。 http://www.pitorque.de/MisterGoodcat/post/Somethings-Missing-from-the-WebBrowser-Control.aspx主要思想是在页面中注入一些javascript并通知浏览器滚动事件;之后更改位于浏览器附近的自定义滚动条控件的位置。