• wpf & javascript & web


    最近有一个需求是,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
    具体代码

  • 相关阅读:
    c语言 414 根据输入的整数,循环显示1234567890
    c语言 47 编写一段程序,显示小于输入的整数的所有2的乘方。
    c语言49 交替输出正负号,当输入0以下时什么也不显示
    c语言48 改写48的程序,当输入的值小于1时不输出换行符
    c语言 411 逆向输出输入的整数值(同时输出原始数据)
    c语言47 显示出小于输入的整数的所有2的乘方
    c语言412 输入一个整数值显示其位数
    c语言415 输出标准身高体重对照表
    c语言413 求1到n的和
    c语言 410 输出连续* \n
  • 原文地址:https://www.cnblogs.com/androllen/p/14761492.html
Copyright © 2020-2023  润新知