1、Nuget下载 Microsoft.Web.WebView2 (运行电脑中必须安装edage浏览器)
2、在xaml中引用
<Window x:Class="WPFAndHtml.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:local="clr-namespace:WPFAndHtml"
xmlns:webview2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid>
<webview2:WebView2 x:Name="web" Source="https://www.baidu.com" />
</Grid>
</Grid>
</Window>
this.web.Source = new Uri(@"D:\\Package\\Code\\C#\\1.c#\\Text\\WPFAndHtml\\WPFAndHtml\\html\\HTMLPage1.html");
效果如下:
注意:
如果在wpf中使用本地html文件必须设置所要显示的所有资源为始终复制。包括调用的js和css文件。
如果使用原生态的webbrowser 则需要添加如下到html中
<!-- saved from url=(0013)about:internet -->