WPF/C#:一个 WebView/浏览器组件,用于显示本地页面并在内部处理传出请求
WPF/C#: A WebView / Browser component to show local pages and handle outgoing requests internally
我在找什么:
我正在寻找一种方法来在 WPF 应用程序中显示包含 CSS 和 JS 的本地网页。但棘手的部分是,还应该可以 CATCH AND PROCESS 传出请求。
所以我需要捕获 GetRequest,自己处理它们,然后 return 自己回答。
为什么:
对于基于服务器的应用程序,我有相当广泛的 UI。 (HTML/CSS/JS) 所以我可以选择是在 WPF 中完全重制整个 UI 还是我需要找到一种方法在 WPF 应用程序中使用这个基于 HTML 的 UI。虽然显示 UI 适用于我目前发现的大多数组件,但另一方面,捕获请求然后提供我自己的答案 JSON 似乎是一件棘手的事情。
我目前试过的两个:
- 用于 Windows 表单和 WPF 的 WebView 控件 WebView Windows Community Toolkit
但据我所知,这不支持捕获传出请求。
- CefSharp (Chromium): 是否支持捕获和重定向,但我似乎无法回答我自己的请求? CefSharp
你们中有人知道可以做到这一点的 component/framework 吗?
编辑
CefSharp 项目:https://1drv.ms/u/s!ArLDiUd-U5dtg06kzBSMOqDZLWxJ?e=Mdkchd
正如我最后得到的解决方案:
javascript调用了一个不存在的网页。请求和资源处理程序捕获此调用并 return 他们自己的 anwser。当您实现自己的 Request/Resource 处理程序时,即使您不需要该功能,在某些情况下也必须替换 NotImplementedException。在这种情况下,只需访问默认行为的官方文档:http://cefsharp.github.io/api/75.1.x/html/T_CefSharp_IRequestHandler.htm
XAML:
<Window x:Class="Chromium.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<cefSharp:ChromiumWebBrowser
x:Name="ChromiumWebBrowser"
/>
</Grid>
</Window>
MainWindow.Xaml.cs
public MainWindow()
{
InitializeComponent();
ChromiumWebBrowser.BrowserSettings.WebSecurity = CefState.Disabled;
ChromiumWebBrowser.RequestHandler = new RequestHandler();
ChromiumWebBrowser.Address = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestHtml", "index.html");
}
index.html
<!doctype html>
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/script.js" type="text/javascript"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>Title</h1>
<p id="result"></p>
<button onclick="call()">Call</button>
</body>
JS 脚本
function call() {
axios.get('http://doesnt_exist')
.then((response) => {
console.log(response);
document.getElementById("result").innerHTML = JSON.stringify(response.data);
});
}
请求处理程序
public class RequestHandler : IRequestHandler
{
public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
Console.WriteLine("GetResourceRequestHandler " + request.Url);
return new ResourceRequestHandler();
}
...
}
ResourceRequestHandler
class ResourceRequestHandler : IResourceRequestHandler
{
public IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
Console.WriteLine("GetResourceHandler " + request.Url);
if (request.Url.Contains("doesnt_exist"))
return ResourceHandler.FromString("Hello");
return null;
}
...
}
我在找什么:
我正在寻找一种方法来在 WPF 应用程序中显示包含 CSS 和 JS 的本地网页。但棘手的部分是,还应该可以 CATCH AND PROCESS 传出请求。
所以我需要捕获 GetRequest,自己处理它们,然后 return 自己回答。
为什么:
对于基于服务器的应用程序,我有相当广泛的 UI。 (HTML/CSS/JS) 所以我可以选择是在 WPF 中完全重制整个 UI 还是我需要找到一种方法在 WPF 应用程序中使用这个基于 HTML 的 UI。虽然显示 UI 适用于我目前发现的大多数组件,但另一方面,捕获请求然后提供我自己的答案 JSON 似乎是一件棘手的事情。
我目前试过的两个:
- 用于 Windows 表单和 WPF 的 WebView 控件 WebView Windows Community Toolkit
但据我所知,这不支持捕获传出请求。 - CefSharp (Chromium): 是否支持捕获和重定向,但我似乎无法回答我自己的请求? CefSharp
你们中有人知道可以做到这一点的 component/framework 吗?
编辑
CefSharp 项目:https://1drv.ms/u/s!ArLDiUd-U5dtg06kzBSMOqDZLWxJ?e=Mdkchd
正如我最后得到的解决方案:
javascript调用了一个不存在的网页。请求和资源处理程序捕获此调用并 return 他们自己的 anwser。当您实现自己的 Request/Resource 处理程序时,即使您不需要该功能,在某些情况下也必须替换 NotImplementedException。在这种情况下,只需访问默认行为的官方文档:http://cefsharp.github.io/api/75.1.x/html/T_CefSharp_IRequestHandler.htm
XAML:
<Window x:Class="Chromium.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<cefSharp:ChromiumWebBrowser
x:Name="ChromiumWebBrowser"
/>
</Grid>
</Window>
MainWindow.Xaml.cs
public MainWindow()
{
InitializeComponent();
ChromiumWebBrowser.BrowserSettings.WebSecurity = CefState.Disabled;
ChromiumWebBrowser.RequestHandler = new RequestHandler();
ChromiumWebBrowser.Address = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestHtml", "index.html");
}
index.html
<!doctype html>
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/script.js" type="text/javascript"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>Title</h1>
<p id="result"></p>
<button onclick="call()">Call</button>
</body>
JS 脚本
function call() {
axios.get('http://doesnt_exist')
.then((response) => {
console.log(response);
document.getElementById("result").innerHTML = JSON.stringify(response.data);
});
}
请求处理程序
public class RequestHandler : IRequestHandler
{
public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
Console.WriteLine("GetResourceRequestHandler " + request.Url);
return new ResourceRequestHandler();
}
...
}
ResourceRequestHandler
class ResourceRequestHandler : IResourceRequestHandler
{
public IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
Console.WriteLine("GetResourceHandler " + request.Url);
if (request.Url.Contains("doesnt_exist"))
return ResourceHandler.FromString("Hello");
return null;
}
...
}