.Net6 WPF 使用 WebView2套壳,js调用api

1.初始化

using api;
using System;
using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            if (webView != null)
            {
                webView.Source = new Uri(Environment.CurrentDirectory + @"\web\mypage.html");
                InitializeAsync();
            }
        }

        public async void InitializeAsync()
        {
            //等待CoreWebView2对象就绪,否则为null
            await webView.EnsureCoreWebView2Async();
            webView.CoreWebView2.AddHostObjectToScript("webapi", new ScriptCallbackObject());
            //直接将api定义到页面中,调用直接使用webapi.xxxx()
            await webView.CoreWebView2.ExecuteScriptAsync("var webapi = window.chrome.webview.hostObjects.webapi;");
            //禁止鼠标缩放
            webView.CoreWebView2.Settings.IsZoomControlEnabled = false;
        }
    }
}

2.定义接口

using System.Runtime.InteropServices;

namespace api
{
    /// <summary>
    /// 提供给web页面的api
    /// </summary>
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class ScriptCallbackObject
    {
        public string GetData(string arg)
        {
            return "获取到了:" + arg;
        }
    }
}

3.页面调用

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h3>有点扯淡哦</h3>
    <input type="text" id="input" />
    <button onclick="myclick()">事件aaaa</button>
    <script type="text/javascript">
        async function myclick() {
            var data = document.getElementById('input').value;
            console.log(webapi);
            var result = await webapi.GetData(data);
            alert(result);
        }
    </script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `PdfiumViewer` 这个 NuGet 包将 PDF 文件转换为图片,它可以在 WPF 应用程序中使用。下面是一个简单的示例: 1. 首先,在 Visual Studio 中创建一个 WPF 应用程序。 2. 在项目中添加 `PdfiumViewer` NuGet 包。 3. 创建一个 `PdfToImageConverter` 类,该类包含一个 `ConvertPdfToImage` 方法,该方法将 PDF 文件转换为图片。代码如下: ```csharp using (var document = PdfiumViewer.PdfDocument.Load(pdfFilePath)) { var image = document.Render(0, 300, 300, true); image.Save(imageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); } ``` 在这个代码中,我们使用 `PdfiumViewer` 中的 `PdfDocument` 类加载 PDF 文件,然后将它渲染成一个 `System.Drawing.Image` 对象,最后保存为 JPEG 格式的图片文件。 4. 在 WPF 应用程序中,创建一个 `Image` 控件用于显示转换后的图片。 5. 在 WPF 应用程序中,使用 `OpenFileDialog` 对话框选择要转换的 PDF 文件,并将其路径传递给 `ConvertPdfToImage` 方法。 6. 将转换后的图片显示在 `Image` 控件中。 完整的代码示例如下: ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ConvertButton_Click(object sender, RoutedEventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf"; if (openFileDialog.ShowDialog() == true) { var pdfFilePath = openFileDialog.FileName; var imageFilePath = Path.Combine(Path.GetDirectoryName(pdfFilePath), Path.GetFileNameWithoutExtension(pdfFilePath) + ".jpg"); var converter = new PdfToImageConverter(); converter.ConvertPdfToImage(pdfFilePath, imageFilePath); var image = new BitmapImage(new Uri(imageFilePath)); ImageControl.Source = image; } } } public class PdfToImageConverter { public void ConvertPdfToImage(string pdfFilePath, string imageFilePath) { using (var document = PdfiumViewer.PdfDocument.Load(pdfFilePath)) { var image = document.Render(0, 300, 300, true); image.Save(imageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } ``` 这个示例将 PDF 文件转换为 JPEG 格式的图片,但你也可以将其保存为其他格式。同时,你还需要添加一些错误处理和异常处理的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值