• CefGlue中js与C#交互(实现mp3播放)


    一、C#调用js

    1.通过CefFrame.ExecuteJavaScript(string code, string url, int line)方法执行JS

    private void browser_LoadEnd(object sender, Xilium.CefGlue.WPF.LoadEndEventArgs e)
            {
                browser.ExecuteJavaScript("window.iscef = true;", browser.GetCefBrowser().GetMainFrame().Url, 0);
            }

    2.通过CefV8Value.ExecuteFunction或CefV8Value.ExecuteFunctionWithContext执行

    二、js调用C#

    1.定义DemoRenderProcessHandler,继承自CefRenderProcessHandler,重写OnWebKitInitialized事件,根据实际情况,人工手写JS脚本代码,通过官方提供的RegisterExtension(string extensionName, string javascriptCode, CefV8Handler handler)方法进行注册:

     public class DemoRenderProcessHandler : CefRenderProcessHandler
        {
            private TGCefV8Handler tgCefV8Handler;
            protected override void OnWebKitInitialized()
            {
                tgCefV8Handler = new TGCefV8Handler();
    
                const string exampleAJavascriptCode = @"function tgCefV8Handler() {}
        if (!tgCefV8Handler) tgCefV8Handler = {};
        (function() {
            tgCefV8Handler.playMp3 = function(arg0) {
                native function PlayMp3(arg0);
                return PlayMp3(arg0);
            };
        })();";
    
                CefRuntime.RegisterExtension("exampleAExtensionName", exampleAJavascriptCode, tgCefV8Handler);
    
                base.OnWebKitInitialized();
            }
        }

    2.定义一个CefV8Handler,继承自CefV8Handler,在ExampleAv8Handler里的Execute事件中,写你要执行的操作代码,下面代码中的JSFunc.PlayMp3表示一个播放mp3的函数

    public class TGCefV8Handler : CefV8Handler
        {
            /// <summary>
            /// 默认构造函数
            /// </summary>
            public TGCefV8Handler()
            {
            }
    
    
            protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
            {
                string result = string.Empty;
    
                switch (name)
                {
                    case "PlayMp3":
                        result = JSFunc.PlayMp3(arguments[0].GetStringValue());
                        break;
                    default:
                        break;
                }
    
                returnValue = CefV8Value.CreateString(result);
                exception = null;
                return true;
            }
        }

    3.在CefApp中重写GetRenderProcessHandler方法:

    internal sealed class SampleCefApp : CefApp
        {
            public SampleCefApp()
            {
            }
    
            private CefRenderProcessHandler _renderProcessHandler = new DemoRenderProcessHandler();
    
            protected override void OnBeforeCommandLineProcessing(string processType, CefCommandLine commandLine)
            {
                if (string.IsNullOrEmpty(processType))
                {
                    commandLine.AppendSwitch("disable-gpu");
                    commandLine.AppendSwitch("disable-gpu-compositing");
                    commandLine.AppendSwitch("enable-begin-frame-scheduling");
                    commandLine.AppendSwitch("disable-smooth-scrolling");
                }
            }
    
            protected override CefRenderProcessHandler GetRenderProcessHandler()
            {
                return _renderProcessHandler;
            }
        }

    4.最后在js中调用方式为:

    var msg = window.tgCefV8Handler.playMp3(url);
  • 相关阅读:
    [csp-s模拟测试72] 简单的期望
    [csp-s模拟测试72] 简单的序列
    csp-s模拟测试70
    经验积累
    [csp-s模拟测试69] 回滚莫队
    [模板]主席树查前趋后继
    复习及学习计划
    错排公式
    csp-s模拟测试 56~65
    LIS LCS LCIS
  • 原文地址:https://www.cnblogs.com/xienb/p/9361612.html
Copyright © 2020-2023  润新知