2013-01-05 66 views
0

我有一些內容需要閱讀,加載到瀏覽器控件中。即時閱讀時,我想看看進展情況。以某種方式在Web瀏覽器控件中顯示垂直滾動條?試過ScrollViewer.VerticalScrollVisibility並把整個網頁瀏覽器放入ScrollViewer,沒有任何工作!在windows phone顯示滾動條瀏覽器控件

+1

不是任何人對Windows Phone的開發? :( – Luke

+0

@Coulton,aha..strange!這個平臺真的很友善,不那麼難:)很快我的小程序就會出現在市場上! ^^ – 0x49D1

+0

您是否正在爲Windows 8開發? – Luke

回答

0

..Nobody接聽..
但香港專業教育學院已經找到了解決辦法。不太理想,有點骯髒,但它有效,我現在可以顯示閱讀進度。 http://www.pitorque.de/MisterGoodcat/post/Somethings-Missing-from-the-WebBrowser-Control.aspx的主要思想是在頁面中注入一些javascript並通知瀏覽器關於滾動事件;之後,改變自定義滾動條控件放置在瀏覽器附近的位置。

+0

請問您可以打電話給我,因爲您提供的鏈接目前沒有打開。 – 2013-09-21 09:59:16

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 
} 
相關問題