4

WPF使用WebView2的空域问题的解决方案

 9 months ago
source link: https://www.cnblogs.com/TianFang/p/17707013.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

我在之前文章中介绍过WPF使用WebView2的空域问题(Airspace issuse),距离那篇文章大半年后,那个issue下有一个第好用的第三方解决方案了,我这里介绍一下。

引入Microsoft.Web.WebView2组件,同时引入CrissCross.WPF.WebView2组件

<ItemGroup> <PackageReference Include="CrissCross.WPF.WebView2" Version="1.0.3" /> <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2065-prerelease" /></ItemGroup>

主窗口示例如下:

<Window x:Class="WpfApp2.MainWindow" 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:rxnav="https://github.com/ChrisPulman/CrissCross" xmlns:local="clr-namespace:WpfApp2" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <rxnav:WebView2Wpf x:Name="WebView2Wpf" Source="https://www.cnblogs.com"> <Grid> <Button Opacity="0.8" Content="I am on top of WebView2 control with Criscross" Height="50" Width="575" FontSize="25" HorizontalAlignment="Center" Foreground="LimeGreen" Click="Button_Click" /> </Grid> </rxnav:WebView2Wpf></Window>

这里是在CrissCrossWebView2Wpf.WebView2Wpf中封装了webview2控件,但同时可以在此控件中放置子控件, 子控件可以是任意wpf组件,子控件可以覆盖在webview之上,并且支持透明等各种属性。

我试了一下这个示例,运行良好,虽然和普通wpf组件相比还是有一些限制,不过已经满足了大部分的需求(大部分的时候无非就是想在浏览器上叠一个控件而已)。即使没有官方解决方案,也可以用这个组件顶上了。两年多的一个bug终于有了一个比较令人满意的解决方案。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK