C# WebView2 完整指南
一、CBA赛程
在开发过程中,获取相关信息是必须的。对于赛事信息,我们可以通过浏览器打开相关网页,或者通过 WebView2
控件进行开发。下面的代码段演示了如何在 WebView2
中加载中国篮球联赛的赛程网页。
private void LoadCbaSchedule()
{
webview2.Source = new Uri("https://cba.sports.sina.com.cn/schedule");
}
二、Clash
现在,游戏类 App 越来越流行,而有些游戏需要使用代理才能访问。这时,我们可以使用 Clash 代理进行开发。下面的代码展示了如何在 WebView2
中使用 Clash 代理。
private async void LoadPageWithClashProxy()
{
var wk = WebView2Environment.GetAvailableBrowserVersionString();
var proxy = new ProxyRules
{
Rules = @"PROXY 127.0.0.1:7890", // 这里修改成自己的 Clash 代理端口
Bypass = "",
};
var options = new BrowserViewOptions
{
Proxy = proxy,
};
using (var browserView = new BrowserView(options))
{
browserView.Bounds = webView2.Bounds;
webView2.Parent.Controls.Add(browserView);
await browserView.EnsureBrowserWindowReadyAsync();
browserView.Navigate("https://www.google.com.hk/");
}
}
三、CBA 总决赛
有时我们需要在 WebView2
页面中嵌入一些特定的内容,比如视频。下面的代码展示了如何在 WebView2
控件中播放优酷上的 CBA 总决赛视频。
private void LoadCbaFinalsVideo()
{
var html = @"<!DOCTYPE html>
<html>
<body>
<iframe height='100%' width='100%' src='https://player.youku.com/embed/XNDI5MTcxNTEyMA==' frameborder='0' allowfullscreen></iframe>
</body>
</html>";
webview2.NavigateToString(html);
}
四、C 开头的火车是什么车
对于一些难以确定的问题,我们可以直接使用 WebView2
进行搜索。下面的代码展示了如何使用 Bing 搜索引擎搜索“C 开头的火车是什么车”这个问题。
private void LoadTrainQuestion()
{
webview2.NavigateToString("<!DOCTYPE html><html><head></head><body><form id='form1' action='https://www.bing.com/search' method='get'><div><input id='sb_form_q' name='q' type='text' value='c开头的火车是什么车'><input type='submit' value='Search'></div></form></body></html>");
}
五、CCTV5 在线直播
有时我们需要在 WebView2
页面中直接展示视频源,比如 CCTV5 直播。下面的代码展示了如何在 WebView2
页面中嵌入 CCTV5 直播地址。
private void LoadCctv5Live()
{
var html = @"<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
</head>
<body>
<iframe src='http://tv.cctv.com/live/cctv5/' height='100%' width='100%'></iframe>
</body>
</html>";
webview2.NavigateToString(html);
}
六、CCTV5 体育节目表
有时候我们需要在 WebView2
中显示一些数据,比如 CCTV5 的体育节目表。下面的代码展示了如何在 WebView2
中嵌入 CCTV5 的体育节目表页面。
private void LoadCctv5ProgramSchedule()
{
webview2.Source = new Uri("https://tv.cctv.com/program/sports/05/");
}
七、C1 驾照能开什么车
我们也可以在 WebView2
中嵌入问答页面,展示用户所需要的信息。下面的代码展示了如何在 WebView2
页面中搜索“C1 驾照能开什么车”这个问题,并将搜索结果展示出来。
private void LoadC1DriverLicenseInfo()
{
webview2.NavigateToString("<!DOCTYPE html><html><head></head><body><form id='form1' action='https://www.baidu.com/s' method='get'><div><input name='wd' type='text' value='c1驾照能开什么车'><input type='submit' value='Search'></div></form></body></html>");
}
八、C 反应蛋白高说明什么
我们也可以在 WebView2
页面中嵌入医学页面,获取医学知识。下面的代码展示了如何在 WebView2
页面中搜索“C 反应蛋白高说明什么”这个问题,并将搜索结果展示出来。
private void LoadCReactiveProteinInfo()
{
webview2.NavigateToString("<!DOCTYPE html><html><head></head><body><form id='form1' action='https://www.baidu.com/s' method='get'><div><input name='wd' type='text' value='c反应蛋白高说明什么'><input type='submit' value='Search'></div></form></body></html>");
}
以上就是 C# WebView2 的完整指南。通过以上代码示例,我们学习了如何在 WebView2
中加载不同类型的内容,并演示了一些 WebView2
常用的操作。希望本文对大家的学习和开发工作有所帮助。