2015-10-04 37 views

回答

0

的WebView的WebView控件的等效SaveToString()允许我们调用脚本,我们可以转储document.documentElement.outerHTML做同样的作为SaveToString()。

下面显示了一个非常简单的测试示例。请注意,我没有尝试制作一个很酷的用户界面设计,并将所有东西都格式化,但只是试图向您展示这个想法。

在XAML

<StackPanel> 
     <WebView x:Name="MyWebView" Source="http://www.bing.com" Height="200" 
       DOMContentLoaded="MyWebView_DOMContentLoaded"/> 
     <Button x:Name="ViewSourceBtn" Content="View Source" Click="ViewSourceBtn_Click"/> 
     <RichTextBlock x:Name="SourceBlock" Height="300" > 
     </RichTextBlock> 
    </StackPanel> 

后面的代码

bool contentloaded = false; 
    private async void ViewSourceBtn_Click(object sender, RoutedEventArgs e) 
    { 
     if (contentloaded) 
     { 
      string html = await MyWebView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" }); 
      var paragraph = new Paragraph(); 
      paragraph.Inlines.Add(new Run { Text = html }); 
      SourceBlock.Blocks.Clear(); 
      SourceBlock.Blocks.Add(paragraph); 
     } 
     else 
     { 
      string err = "Waiting for HTML content to load!"; 
      var paragraph = new Paragraph(); 
      paragraph.Inlines.Add(new Run { Text = err }); 
      SourceBlock.Blocks.Clear(); 
      SourceBlock.Blocks.Add(paragraph); 
     } 
    } 

    private void MyWebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args) 
    { 
     contentloaded = true; 
    } 

下面是结果通过单击视图源按钮: enter image description here

+0

感谢您的帮助! – Razor