首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【愚公系列】2023年01月 .NET CORE工具案例-WebWindow的使用

【愚公系列】2023年01月 .NET CORE工具案例-WebWindow的使用

作者头像
愚公搬代码
发布2023-03-16 16:06:21
发布2023-03-16 16:06:21
7090
举报
文章被收录于专栏:历史专栏历史专栏

文章目录


前言

WebWindow顾名思义就是Windows中的web。

WebWindow是跨平台的库,要运行WebWindow必须有以下条件:

  • Windows – 需要基于Chromium的Edge
  • Linux – 使用WebKit
  • Mac – 需要Safari

必须是预览版Edge下载地址:https://www.microsoft.com/en-us/edge/business/download

WebWindow官网:https://github.com/SteveSandersonMS/WebWindow

还有另一个库,这边不做详细介绍,WebWindowNetCore官网:https://github.com/uriegel/WebWindowNetCore

一、WebWindowNetCore的使用

1.安装包

代码语言:javascript
复制
NuGet\Install-Package WebWindow -Version 0.1.0-20200807.1

2.基于html的运行

代码语言:javascript
复制
using WebWindows;

var window = new WebWindow("My super app");
window.NavigateToString("<h1>Hello, world!</h1> This window is from a .NET Core app.");
window.WaitForExit();

3.基于vue的运行

代码语言:javascript
复制
class Program
    {
        static void Main(string[] args)
        {
            var window = new WebWindow(".NET Core + Vue.js file explorer");
            window.OnWebMessageReceived += HandleWebMessageReceived;
            window.NavigateToLocalFile("wwwroot/index.html");
            window.WaitForExit();
        }

        static void HandleWebMessageReceived(object sender, string message)
        {
            var window = (WebWindow)sender;
            var parsedMessage = JsonDocument.Parse(message).RootElement;
            switch (parsedMessage.GetProperty("command").GetString())
            {
                case "ready":
                    ShowDirectoryInfo(window, Directory.GetCurrentDirectory());
                    break;
                case "navigateTo":
                    var basePath = parsedMessage.GetProperty("basePath").GetString();
                    var relativePath = parsedMessage.GetProperty("relativePath").GetString();
                    var destinationPath = Path.GetFullPath(Path.Combine(basePath, relativePath)).TrimEnd(Path.DirectorySeparatorChar);
                    ShowDirectoryInfo(window, destinationPath);
                    break;
                case "showFile":
                    var fullName = parsedMessage.GetProperty("fullName").GetString();
                    ShowFileContents(window, fullName);
                    break;
            }
        }

        static void ShowDirectoryInfo(WebWindow window, string path)
        {
            window.Title = Path.GetFileName(path);

            var directoryInfo = new DirectoryInfo(path);
            SendCommand(window, "showDirectory", new
            {
                name = path,
                isRoot = Path.GetDirectoryName(path) == null,
                directories = directoryInfo.GetDirectories().Select(directoryInfo => new
                {
                    name = directoryInfo.Name + Path.DirectorySeparatorChar,
                }),
                files = directoryInfo.GetFiles().Select(fileInfo => new
                {
                    name = fileInfo.Name,
                    size = fileInfo.Length,
                    fullName = fileInfo.FullName,
                }),
            });
        }

        private static void ShowFileContents(WebWindow window, string fullName)
        {
            var fileInfo = new FileInfo(fullName);
            SendCommand(window, "showFile", null); // Clear the old display first
            SendCommand(window, "showFile", new
            {
                name = fileInfo.Name,
                size = fileInfo.Length,
                fullName = fileInfo.FullName,
                text = ReadTextFile(fullName, maxChars: 100000),
            });
        }

        private static string ReadTextFile(string fullName, int maxChars)
        {
            var stringBuilder = new StringBuilder();
            var buffer = new char[4096];
            using (var file = File.OpenText(fullName))
            {
                int charsRead = int.MaxValue;
                while (maxChars > 0 && charsRead > 0)
                {
                    charsRead = file.ReadBlock(buffer, 0, Math.Min(maxChars, buffer.Length));
                    stringBuilder.Append(buffer, 0, charsRead);
                    maxChars -= charsRead;
                }

                return stringBuilder.ToString();
            }
        }

        static void SendCommand(WebWindow window, string commandName, object arg)
        {
            window.SendMessage(JsonSerializer.Serialize(new { command = commandName, arg = arg }));
        }
    }

4.基于Blazor的运行

Blazor 是可以和 JS 互操作的,不需要底层通信API

代码语言:javascript
复制
static void Main(string[] args)
{
    ComponentsDesktop.Run<Startup>("My Blazor App", "wwwroot/index.html");
}

5.相关介绍

相关API介绍:

  • NavigateToString(html) 从硬编码的 .NET 字符串渲染 HTML
  • NavigateToUrl(url) 来显示来自 HTTP 服务器的内容(本地或远程)
  • NavigateToLocalFile(path) 来显示来自本地磁盘的 HTML 文件,其中 path是绝对路径或相对于当前工作目录的路径。

运行中的web程序通信方式如下:

  • JS中和.NET通信:window.external.sendMessage/receiveMessage
  • .NET中和JS通信:webWindowInstance.SendMessage/webWindowInstance.OnWebMessageReceived
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、WebWindowNetCore的使用
    • 1.安装包
    • 2.基于html的运行
    • 3.基于vue的运行
    • 4.基于Blazor的运行
    • 5.相关介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档