cef调用本地html,wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu...

第一步:

f4fc8c07d6932e4404fcf5dc0648c54b.png

第二步:

072650a73b8a715b99ffd4d8756dd257.png

true

第三步:

c072968af2578d2e47ba88a9a259aa83.png

第四步:

79fddf7ccf92e392e1ebbb1b1a9e1937.png

App.xaml.cs对应的代码:

using CefSharp;

using CefSharp.Wpf;

using System;

using System.IO;

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Windows;

namespace CefSharpe

{

/// /// App.xaml 的交互逻辑

///

public partial class App : Application

{

public App()

{

//Add Custom assembly resolver

AppDomain.CurrentDomain.AssemblyResolve += Resolver;

//Any CefSharp references have to be in another method with NonInlining

// attribute so the assembly rolver has time to do it's thing.

InitializeCefSharp();

}

[MethodImpl(MethodImplOptions.NoInlining)]

private static void InitializeCefSharp()

{

var settings = new CefSettings();

// Set BrowserSubProcessPath based on app bitness at runtime

settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,

Environment.Is64BitProcess ? "x64" : "x86",

"CefSharp.BrowserSubprocess.exe");

// Make sure you set performDependencyCheck false

Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);

}

// Will attempt to load missing assembly from either x86 or x64 subdir

// Required by CefSharp to load the unmanaged dependencies when running using AnyCPU

private static Assembly Resolver(object sender, ResolveEventArgs args)

{

if (args.Name.StartsWith("CefSharp"))

{

string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";

string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,

Environment.Is64BitProcess ? "x64" : "x86",

assemblyName);

return File.Exists(archSpecificPath)

? Assembly.LoadFile(archSpecificPath)

: null;

}

return null;

}

}

}

以上实现的是cefsharp支持any cpu

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,针对您的问题,CefSharp.Wpf 是一个基于 Chromium Embedded Framework(CEF)的 .NET 库,用于在 WPF 应用程序嵌入浏览器。要实现多个浏览器隔离 Cookie,您可以在代码创建多个 CefSharp.BrowserSubprocess 对象,并为每个对象设置独立的 cookie 存储路径。 具体实现步骤如下: 1. 在代码创建多个 CefSharp.BrowserSubprocess 对象,例如: ``` var browser1 = new CefSharp.Wpf.ChromiumWebBrowser(); var browser2 = new CefSharp.Wpf.ChromiumWebBrowser(); ``` 2. 设置每个浏览器的 cookie 存储路径,例如: ``` browser1.BrowserSettings = new CefSharp.BrowserSettings { ApplicationCache = CefState.Disabled, WebSecurity = CefState.Enabled, FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, DefaultEncoding = "UTF-8", JavaScript = CefState.Enabled, JavaScriptAccessClipboard = CefState.Disabled, JavaScriptCloseWindows = CefState.Disabled, JavaScriptDomPaste = CefState.Disabled, LocalStorage = CefState.Enabled, Plugins = CefState.Enabled, RemoteFonts = CefState.Enabled, WebGl = CefState.Enabled, WebAudio = CefState.Enabled, FileAccessFromUrls = CefState.Enabled, ImageLoading = CefState.Enabled, TextAreaResize = CefState.Enabled, WebSecurityEnabled = true, CefCommandLineArgs = new Dictionary<string, string> { { "disable-gpu", "1" }, { "disable-gpu-compositing", "1" }, { "disable-software-rasterizer", "1" } }, PersistSessionCookies = true, FilePath = @"C:\Users\user1\AppData\Local\Temp\cefsharp_cookies1" }; browser2.BrowserSettings = new CefSharp.BrowserSettings { ApplicationCache = CefState.Disabled, WebSecurity = CefState.Enabled, FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, DefaultEncoding = "UTF-8", JavaScript = CefState.Enabled, JavaScriptAccessClipboard = CefState.Disabled, JavaScriptCloseWindows = CefState.Disabled, JavaScriptDomPaste = CefState.Disabled, LocalStorage = CefState.Enabled, Plugins = CefState.Enabled, RemoteFonts = CefState.Enabled, WebGl = CefState.Enabled, WebAudio = CefState.Enabled, FileAccessFromUrls = CefState.Enabled, ImageLoading = CefState.Enabled, TextAreaResize = CefState.Enabled, WebSecurityEnabled = true, CefCommandLineArgs = new Dictionary<string, string> { { "disable-gpu", "1" }, { "disable-gpu-compositing", "1" }, { "disable-software-rasterizer", "1" } }, PersistSessionCookies = true, FilePath = @"C:\Users\user1\AppData\Local\Temp\cefsharp_cookies2" }; ``` 3. 将浏览器对象添加到 WPF 窗口,例如: ``` grid.Children.Add(browser1); grid.Children.Add(browser2); ``` 通过以上步骤,您就可以实现多个浏览器隔离 Cookie 的功能了。需要注意的是,每个浏览器的 cookie 存储路径应该是独立的,以避免 cookie 冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值