C# WebView2 在你的应用中使用Chromium内核
什么是WebView2?
- Win10上对标Edge浏览器
- Chromium内核
- 简单的可视为WebBrowser组件的升级版
如何使用WebView2?
- 官网下载 WebView2 RunTime
- VS2019 NuGet搜索 WebView2,安装最新版即可
WebView2 具体使用技巧
- 引入
using Microsoft.Web.WebView2.WinForms;
- 声明 WebView2,如
var edge=new WebView2();
- 将窗体 Load 事件声明为
async
,如private async void ViewForm_Load(object sender, EventArgs e)
- 将
edge
添加到Controls
中,如Controls.Add(edge);edge.Dock = DockStyle.Fill;
- 在窗体 Load 事件中 增加
await edge.EnsureCoreWebView2Async();//这步是必须的
- 设置网址,
edge.Source = new Uri("https://www.cnblogs.com");
我想怎么玩?
- WebView2 提供 UI 👉 TidyView
- TidyScriptCore 提供 C# 脚本运行
TidyView
- 把 WebView2 封装成 TidyWebView
public class TidyWebView:WebView2
{
public TidyWebView();
public List<Typing.FuncSender> Main;
public Runner ViewRunner;
private void CoreWebView2_DOMContentLoaded(object sender, Microsoft.Web.WebView2.Core.CoreWebView2DOMContentLoadedEventArgs e);
private void TidyWebView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e);
public string Url{get;set;}
public void Navigate(string Url)
public delegate void CallNone();
public void EvalJS(string Script);
public void EvalTidy(string Script);
private EventCollection EventList;
public int AddFunc(Typing.FuncSender func);
public int AddFunc(string Name,Typing.FuncSender func);
public void CallIndex(int Index,params object[] args);
public void Call(string Name, params object[] args);
}
- 将 TidyWebView 放到窗体
using System;
using System.Windows.Forms;
using System.IO;
using TidyScriptCore;
namespace TidyView
{
public partial class ViewForm : Form
{
public ViewForm()
{
InitializeComponent();
edge.NavigationCompleted += Edge_NavigationCompleted;
Inner.console.host = edge;
}
private void Edge_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
Text = edge.CoreWebView2.DocumentTitle;
}
TidyWebView edge = new TidyWebView();
public string CodePath = string.Empty;
private async void ViewForm_Load(object sender, EventArgs e)
{
Tidy.Using(typeof(Inner));
p_web.Controls.Add(edge);
edge.Dock = DockStyle.Fill;
Icon = Properties.Resources.browser;
await edge.EnsureCoreWebView2Async();
edge.ViewRunner.Space.Create("MainForm",this);
if (File.Exists(CodePath))
{
Log.Path = TidyScriptCore.API.GetDir(CodePath) + "\\Logs.txt";
edge.ViewRunner.Environment["ScriptPath"] = CodePath;
edge.ViewRunner.Environment["ScriptDir"] = TidyScriptCore.API.GetDir(CodePath);
Tidy.EvalFile(CodePath,edge.ViewRunner);
}
}
}
}
- 写 TidyScript 脚本 个人门户(密码:Tidy),基本和C#代码风格保持一致
using "Baidu.csc";
using "News.csc";
using "Weather.csc";
edge.AddFunc("search",(kw,filter_str,limit_str,page_str)=>
{
var filter=json.obj(filter_str);
var limit=json.obj(limit_str);
var page=json.obj(page_str);
var filter_lamda;
var bd=new Baidu();
//...
int index=page.start_index;
bd.search(kw,(info)=>
{
//...
if(filter.on){/*...*/}
else
{
edge.EvalJS($"_add({index++},{info});");
}
return true;
},()=>{/*...*/},page.first,page.last);
edge.EvalJS("_show_sufooter()");
});
//...
//...
edge.Main.Add(()=>
{
if(file.exists(locate("filter_code.txt")))
{
var default_code=transfer(file.read(locate("filter_code.txt")));
edge.EvalJS($"document.getElementById('filter_code').value='{default_code}'");
}
});
edge.Url="file:///"+locate("page.html");
-
右键你写的脚本,用TidyView.exe打开,点击BiliBili
下一个玩具 FFMPEG Visual ,我打算这么做
- 将功能封装在FfmpegEx.dll中
- TidyView用来 UI
- 写C#脚本,来桥接 FfmpegEx.dll 和 JS
posted on 2021-04-15 10:00 TypeSharp 阅读(2092) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库