最近有一个需求是,WPF里面要嵌入一个Vue前端框架,也就是把网页嵌入进WPF里面,找了好久发现用CefSharp还是比较不错的,但是有一点打包占空间太大
这是第一种解法:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); CefSharpSettings.LegacyJavascriptBindingEnabled = true; //this.cefsharp.Address = AppDomain.CurrentDomain.BaseDirectory + @"index.html"; this.cefsharp.RegisterJsObject("jsobjp", new CallbackObjectForJs(), new CefSharp.BindingOptions { CamelCaseJavascriptNames = false }); //阻止默认行为 cefsharp.MenuHandler = new MenuHandler(); this.cefsharp.LifeSpanHandler = new OpenPageSelf(); } public class CallbackObjectForJs { public string name = ""; public void send(string msg) { MessageBox.Show(msg); MessageBox.Show(name); } } private void Button_Click(object sender, RoutedEventArgs e) { cefsharp.ExecuteScriptAsync("ydb1('asdadaasdasdas我是杨道波')"); } public class MenuHandler : IContextMenuHandler { public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) { model.Clear(); } public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) { return false; } public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame) { } public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) { return false; } } /// <summary> /// 在自己窗口打开链接 /// </summary> internal class OpenPageSelf : ILifeSpanHandler { public bool DoClose(IWebBrowser browserControl, IBrowser browser) { return false; } public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser) { } public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser) { } public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { newBrowser = null; var chromiumWebBrowser = (ChromiumWebBrowser)browserControl; chromiumWebBrowser.Load(targetUrl); return true; //Return true to cancel the popup creation copyright by codebye.com. } } }
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" <Grid> <cefSharp:ChromiumWebBrowser x:Name="cefsharp" Address="http://127.0.0.1:8081/" /> <Button Height="30" VerticalAlignment="Bottom" Click="Button_Click"> 调用js的方法 </Button> </Grid>
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; } }
第二种解法:
https://docs.microsoft.com/zh-cn/microsoft-edge/webview2/get-started/wpf
请详细看官方文档,里面也讲述了如何嵌入网页,并从网页返回消息给 WPF
具体代码