我在之前文章中介紹過(guò)WPF使用WebView2的空域問(wèn)題(Airspace issuse),距離那篇文章大半年后,那個(gè)issue下有一個(gè)第好用的第三方解決方案了,我這里介紹一下。
引入Microsoft.Web.WebView2組件,同時(shí)引入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="http://www.rzrgm.cn">
<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控件,但同時(shí)可以在此控件中放置子控件, 子控件可以是任意wpf組件,子控件可以覆蓋在webview之上,并且支持透明等各種屬性。
我試了一下這個(gè)示例,運(yùn)行良好,雖然和普通wpf組件相比還是有一些限制,不過(guò)已經(jīng)滿足了大部分的需求(大部分的時(shí)候無(wú)非就是想在瀏覽器上疊一個(gè)控件而已)。即使沒(méi)有官方解決方案,也可以用這個(gè)組件頂上了。兩年多的一個(gè)bug終于有了一個(gè)比較令人滿意的解決方案。
浙公網(wǎng)安備 33010602011771號(hào)