炫意html5
最早CSS3和HTML5移动技术网站之一

C# 如何使用CefSharp向JS中的数组赋值

C#自带的WebBrowser控件,传递数组只要webBrowser.InvokeScript(“test”, arguments.ToArray());就能实现;CefSharp中该怎么做,构造一个js方法慢慢push进去数据量大了就很狼狈。

回答

    public class Primitive图表Echart : Primitive浏览器本地
    {
        [SortIndex(1000)]
        [DataType(DataType.Url)]
        [System.ComponentModel.Category("绑定相关|基本")]
        [System.ComponentModel.DisplayName("示例")]
        [System.ComponentModel.Description("示例")]
        public virtual Uri DemoUri { get; set; } = new Uri("https://echarts.apache.org/examples/zh/index.html#chart-type-line");

        [Browsable(false)]
        [Filed]
        [SortIndex(1000)]
        [System.ComponentModel.Category("基本属性|场景")]
        [System.ComponentModel.DisplayName("网页文件")]
        [System.ComponentModel.Description("网页文件")]
        public override string Uri
        {
            get => base.Uri;
            set => base.Uri = value;
        }

        [Filed()]
        [DoNotCheckEquality]
        //[AutoUpdateText]
        [SortIndex(6000)]
        [System.ComponentModel.Category("绑定相关|基本")]
        [System.ComponentModel.DisplayName("绑定JS")]
        [System.ComponentModel.Description("绑定JS.")]
        [HeaderPlacement(HeaderPlacement.Above)]
        [Height(500)]
        public string JsValue
        {
            get => _jsValue;
            set
            {
                _jsValue = value;
                Browser?.Reload();
            }
        }
        private string _jsValue = 
    @"option = {
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: [150, 230, 224, 218, 135, 147, 260],
                type: 'line'
            }]
        };";

        private const string FileName = "Echarts.html";
        public Primitive图表Echart()
        {
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            Browser?.Reload();
        }

        void EvalChartJs()
        {
            if (!string.IsNullOrEmpty(_jsValue)) 
                _frame?.ExecuteJavaScriptAsync($"{_jsValue}acceptOption();",_frame.Url);
        }
        protected override void OnLoaded(object sender, RoutedEventArgs e)
        {
            Uri = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName);
            base.OnLoaded(sender, e);
            Browser.FrameLoadEnd += Browser_FrameLoadEnd;
        }

        protected override void SetAddress(string uri)
        {
            if (string.IsNullOrEmpty(uri)) return;
            var absoluteFilePath = uri;
            Browser.Address = $"file:///{uri}";
        }

        protected override void OnUnloaded(object sender, RoutedEventArgs e)
        {
            base.OnUnloaded(sender, e);
            _frame = null;
        }

        private IFrame _frame;
        private void Browser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
        {
            if (e.Frame.IsMain && e.Url.EndsWith(FileName))
            {
                _frame = e.Frame;
                EvalChartJs();
            }
        }
    }

炫意HTML5 » C# 如何使用CefSharp向JS中的数组赋值

Java基础教程Android基础教程