本文介绍了CefSharp定制SchemeHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用CefSharp的SchemeHandler为了抢夺从我的C#项目资源,如的.css ,的.js 或荫使用自定义的网址,例如巴纽文件自定义://cefsharp/assets/css/style.css Iam using CefSharp's SchemeHandler in order to grab resources from my C# project like .css, .js or .png files using a custom url for example custom://cefsharp/assets/css/style.css我有2自定义类,以便存档此。I've 2 custom classes in order to archive this. MyCustomSchemeHandlerFactory / code>将处理该定制方案之一,它看起来像这样,在自定义,将自定义方案:First class, MyCustomSchemeHandlerFactory will be the one that handles the custom Scheme and it looks like this, where "custom" will be the custom scheme:internal class MyCustomSchemeHandlerFactory : ISchemeHandlerFactory{ public const string SchemeName = "custom"; public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) { return new MyCustomSchemeHandler(); }} 我实现下一个类 MyCustomSchemeHandler 将接收呼叫,输出的响应,它看起来是这样的:The next class I've implemented is MyCustomSchemeHandler which will receive the call and output a response and it looks like this:internal class MyCustomSchemeHandler : IResourceHandler{ private static readonly IDictionary<string, string> ResourceDictionary; private string mimeType; private MemoryStream stream; static MyCustomSchemeHandler() { ResourceDictionary = new Dictionary<string, string> { { "/home.html", Properties.Resources.index}, { "/assets/css/style.css", Properties.Resources.style} }; } public Stream Stream { get; set; } public int StatusCode { get; set; } public string StatusText { get; set; } public string MimeType { get; set; } public NameValueCollection Headers { get; private set; } public Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl) { redirectUrl = null; responseLength = -1; response.MimeType = MimeType; response.StatusCode = StatusCode; response.StatusText = StatusText; response.ResponseHeaders = Headers; var memoryStream = Stream as MemoryStream; if (memoryStream != null) { responseLength = memoryStream.Length; } return Stream; } public bool ProcessRequestAsync(IRequest request, ICallback callback) { // The 'host' portion is entirely ignored by this scheme handler. var uri = new Uri(request.Url); var fileName = uri.AbsolutePath; string resource; if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource)) { var resourceHandler = ResourceHandler.FromString(resource); stream = (MemoryStream)resourceHandler.Stream; var fileExtension = Path.GetExtension(fileName); mimeType = ResourceHandler.GetMimeType(fileExtension); callback.Continue(); return true; } else { callback.Dispose(); } return false; } void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl) { responseLength = stream == null ? 0 : stream.Length; redirectUrl = null; response.StatusCode = (int)HttpStatusCode.OK; response.StatusText = "OK"; response.MimeType = mimeType; } bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback) { //Dispose the callback as it's an unmanaged resource, we don't need it in this case callback.Dispose(); if (stream == null) { bytesRead = 0; return false; } //Data out represents an underlying buffer (typically 32kb in size). var buffer = new byte[dataOut.Length]; bytesRead = stream.Read(buffer, 0, buffer.Length); dataOut.Write(buffer, 0, buffer.Length); return bytesRead > 0; } bool CanGetCookie(Cookie cookie) { return true; } bool CanSetCookie(Cookie cookie) { return true; } void Cancel() { }}这里面类我定义这将决定将使用来自哪些资源文件的自定义资源字典,所以我在第一个例子说明,自定义://cefsharp/assets/css/style.css 应该加载资源 Properties.Resources.style ,问题是,没有被加载,一旦我进入到具体的网址,我试图输出mime类型和它的工作原理,但不知何故该文件本身将无法正确输出。 ?是不是有什么毛病我实施Inside this class I've defined a custom resource dictionary which will dictate what file from the resources will be used, so as I stated in the first example, custom://cefsharp/assets/css/style.css should load the resource Properties.Resources.style, the problem is that nothing gets loaded once I enter to the specific url, I've tried to output the mimeType and It works but somehow the file itself won't output correctly. Is there something wrong with my implementation? Additionaly我试图输出形式的原始文件:Additionaly I've tried to output the raw file in the form of:if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource)){ MessageBox.Show(resource);}和它输出没有任何问题了正确的文件。And it outputs the correct file without any problems.要加载我初始化CefSharp之前使用下面的代码自定义方案:To load the custom Scheme I use the following code before initializing CefSharp:var settings = new CefSettings();settings.RegisterScheme(new CefCustomScheme{ SchemeName = MyCustomSchemeHandlerFactory.SchemeName, SchemeHandlerFactory = new MyCustomSchemeHandlerFactory()}); 以上类是基于以下链接: MyCustomSchemeHandlerFactory:的 FlashResourceHandlerFactory.cs MyCustomSchemeHandler:的 CefSharpSchemeHandler.cs 和的 ResourceHandler.cs 推荐答案如果你只需要返回一个字符串,那么你可以使用 ResourceHandler.FromString(HTML,mime类型)。为此,您只需要执行 ISchemeHandlerFactory 。If you simply need to return a string, then you can use ResourceHandler.FromString(html, mimeType). For this you just need to implement the ISchemeHandlerFactory.的 https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp/ResourceHandler.cs#L98 例从文件中读取的 https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp.Example/CefSharpSchemeHandlerFactory.cs#L17 它可以转化为读从字符串很简单。Example reading from a file https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp.Example/CefSharpSchemeHandlerFactory.cs#L17 which can be translated to reading from a string quite simply. 这篇关于CefSharp定制SchemeHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 01:53