C# WPF WebBrowser (3.5 SP1) Always on top - 在 WPF 中显示 HTML 的其他建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/980334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:45:59  来源:igfitidea点击:

WPF WebBrowser (3.5 SP1) Always on top - other suggestion to display HTML in WPF

c#htmlwpf.net-3.5

提问by RoelF

I've been desperately looking for an easy way to display HTML in a WPF-application. There are some options:
1) use the WPF WebBrowser Control
2) use the Frame Control
3) use a third-party control

我一直在拼命寻找一种在 WPF 应用程序中显示 HTML 的简单方法。有一些选项:
1) 使用 WPF WebBrowser Control
2) 使用 Frame Control
3) 使用第三方控件

but, i've ran into the following problems: 1) the WPF WebBrowser Control is not real WPF (it is a Winforms control wrapped in WPF). I have found a way to create a wrapper for this and use DependencyProperties to navigate to HTML text with bindings and propertychanged. The problem with this is, if you put a Winforms control in WPF scrollviewer, it doesnt respect z-index, meaning the winform is always on top of other WPF control. This is very annoying, and I have tried to work around it by creating a WindowsFormsHost that hosts an ElemenHost etc.. but this completely breaks my binding obviously.

但是,我遇到了以下问题:1) WPF WebBrowser Control 不是真正的 WPF(它是 WPF 中包装的 Winforms 控件)。我找到了一种为此创建包装器的方法,并使用 DependencyProperties 导航到具有绑定和属性更改的 HTML 文本。这样做的问题是,如果您在 WPF 滚动查看器中放置一个 Winforms 控件,它不尊重 z-index,这意味着 Winform 始终位于其他 WPF 控件之上。这很烦人,我试图通过创建一个承载 ElemenHost 等的 WindowsFormsHost 来解决它,但这显然完全打破了我的绑定。

2) Frame Control has the same problems with displaying if it shows HTML content. Not an option.

2)Frame Control如果显示HTML内容,也有同样的问题。不是一个选项。

3) I haven't found a native HTML-display for WPF. All options are winforms, and with the above mentioned problems.

3) 我还没有找到 WPF 的原生 HTML 显示。所有选项都是winforms,并且存在上述问题。

the only way out I have at the moment is using Microsoft's buggy HtmlToXamlConverter, which crashes hard sometimes. (MSDN)

我目前唯一的出路是使用微软有问题的 HtmlToXamlConverter,它有时会严重崩溃。(微软)

Does anybody have any other suggestions on how to display HTLM in WPF, without these issues?

在没有这些问题的情况下,是否有人对如何在 WPF 中显示 HTLM 有任何其他建议?

sorry for the long question, hope anyone knows what i'm talking about...

抱歉问了这么长的问题,希望有人知道我在说什么......

采纳答案by Ana Betts

If you can't use WebBrowser, your best bet is to probably rewrite your HTML content into a FlowDocument (if you're using static HTML content).

如果您不能使用 WebBrowser,最好的办法是将您的 HTML 内容重写为 FlowDocument(如果您使用的是静态 HTML 内容)。

Otherwise, as you mention, you kind of have to special-case WebBrowser, you're right that it doesn't act like a "real" WPF control. You should probably create a ViewModel object that you can bind to that represents the WebBrowser control where you can hide all of the ugly non-binding code in one place, then never open it again :)

否则,正如你所提到的,你必须对 WebBrowser 进行特殊处理,你是对的,它不像“真正的”WPF 控件。您可能应该创建一个可以绑定到代表 WebBrowser 控件的 ViewModel 对象,您可以在其中将所有丑陋的非绑定代码隐藏在一个地方,然后再也不打开它:)

回答by David Waters

Another approach to working round the z-index limitation is to use a popup to overlay your WPF components over the HTML See http://karlshifflett.wordpress.com/2009/06/13/wpf-float-buttons-over-web-browser-control/Note code below is taken straight from the link

解决 z-index 限制的另一种方法是使用弹出窗口将 WPF 组件覆盖在 HTML 上参见http://karlshifflett.wordpress.com/2009/06/13/wpf-float-buttons-over-web-下面的浏览器控制/注释代码直接取自链接

<Grid>
  <WebBrowser x:Name="wbBrowser" />

  <Popup x:Name="puOverlay" AllowsTransparency="True" Placement="Bottom"
         PlacementTarget="{Binding ElementName=wbBrowser}">
    <Border x:Name="bdrOverLay" CornerRadius="30" BorderBrush="Blue"
            Background="#1F000000" Padding="7" BorderThickness="2">

      <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
          <Style TargetType="{x:Type Button}">
            <Setter Property="Width" Value="75" />
            <Setter Property="Margin" Value="3.5" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
          </Style>
        </StackPanel.Resources>
        <Button Command="NavigationCommands.BrowseBack" Content="Back" />
        <Button Command="NavigationCommands.BrowseForward" Content="Forward" />
        <Button Command="NavigationCommands.BrowseHome" Content="Home" />
        <Button Command="ApplicationCommands.Close" Content="Exit" />
      </StackPanel>

    </Border>
  </Popup>
</Grid>

Alternativly there is a 3rd party control that takes Win32 controls and renders them (as bit maps) into WPF http://www.codeplex.com/WPFWin32Renderer

或者有一个 3rd 方控件,它采用 Win32 控件并将它们(作为位图)呈现到 WPF http://www.codeplex.com/WPFWin32Renderer

回答by jing