Skip to content
分类目录:

WebView2

Post date:
Author:

https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/concepts/developer-guide

https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/concepts/process-related-events?tabs=dotnetcsharp

https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/concepts/distribution?tabs=dotnetcsharp#details-about-the-fixed-version-runtime-distribution-mode

https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp

https://developer.microsoft.com/zh-cn/microsoft-edge/webview2?form=MA13LH#download

Microsoft Edge WebView2 Runtime 主页:

https://developer.microsoft.com/zh-cn/microsoft-edge/webview2

Microsoft Edge WebView2 Runtime 和Edge浏览器是两个独立的程序,并没有直接的相关性。

Microsoft Edge WebView2 Runtime分为x86,x64,arm64安装程序,

x86安装后的目录路径为:version为当前安装的版本号

C:\Program Files (x86)\Microsoft\EdgeWebView\Application\<version>

如:C:\Program Files (x86)\Microsoft\EdgeWebView\Application\136.0.3240.64

Microsoft.Web.WebView2:

C#的Nuget package,该package的版本依赖Runtime的版本,两者间不能随意搭配。

在WPF中的使用:

获取当前安装的Microsoft Edge WebView2 Runtime 的版本号:

var version = CoreWebView2Environment.GetAvailableBrowserVersionString();

在View中添加WebView2:

<UserControl x:Class="Module.Views.WebPdfView"   
             xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
             >
    <Grid Margin="0,0,80,20" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="Print" Click="Button_Click"/>
        <wv2:WebView2 Grid.Row="1" Name="webView" Source="{Binding DisplayReportFileUri, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</UserControl>

等待初始化完成:

webView.EnsureCoreWebView2Async();

显示PDF文件:

public class WebPdfViewModel
{
    public string DisplayReportFileUri {get;set;}= @"C:\Test.pdf"

}

调用打印功能,通过传递不同的CoreWebView2PrintDialogKind,可以选择打开浏览器中的打印对话框或者系统对话框。

webView.CoreWebView2.ShowPrintUI(Microsoft.Web.WebView2.Core.CoreWebView2PrintDialogKind.System);

执行脚本:

webView.ExecuteScriptAsync("window.print();")

释放:

webView?.Dispose();
豫ICP备2021008859号-1