WPF WebBrowser滚动重新加载

时间:2016-02-27 19:34:16

标签: c# wpf webbrowser-control

我有一个System.Windows.Controls.WebBrowser。它有一些html来自用户正在编辑的另一个文档。当html发生变化时,我想要做的就是更新WebBrowser的html,然后将WebBrowser滚动回原处。我成功缓存了滚动偏移(请参阅How to retrieve the scrollbar position of the webbrowser control in .NET)。但是当负载完成时我无法收到回调。这是我尝试过的:

// constructor
public HTMLReferenceEditor()
{
      InitializeComponent();
      WebBrowser browser = this.EditorBrowser;
      browser.LoadCompleted += Browser_LoadCompleted;
      //browser.Loaded += Browser_Loaded; // commented out as it doesn't fire when the html changes . . .
}

private void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
     CommonDebug.LogLine("LoadCompleted");
     this.ScrollWebBrowser();
}

private void ScrollWebBrowser()
{
      WebBrowser browser = this.EditorBrowser;
      ReferenceHierarchicalViewModel rhvm = this.GetReferenceHierarchichalViewModel();
      int? y = rhvm.LastKnownScrollTop; // this is the cached offset. 
      browser?.ScrollToY(y);
}   

LoadCompleted回调正在解雇。但滚动没有发生。我怀疑回调太快了。但是我的滚动方法也可能是错误的:

public static void ScrollToY(this WebBrowser browser, int? yQ)
{
    if (yQ.HasValue)
    {
        object doc = browser?.Document;
        HTMLDocument castDoc = doc as HTMLDocument;
        IHTMLWindow2 window = castDoc?.parentWindow;
        int y = yQ.Value;
        window?.scrollTo(0, y);
        CommonDebug.LogLine("scrolling", window, y);
        // above is custom log method; prints out something like "scrolling HTMLWindow2Class3 54", which
        // at least proves that nothing is null.
    }
}

如何让浏览器滚动?顺便说一句,我没有看到其他人提到的一些回调方法,例如:这里提到的DocumentCompleted对我来说不存在。 Detect WebBrowser complete page loading。换句话说,由于某些原因我不明白,我的WebBrowser与他们的不同。对我来说,这些方法并不存在。

0 个答案:

没有答案
相关问题