wpf vue cef 交互

<UserControl x:Class="LabInOnePC.View.UcSampleBankView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cef="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
             mc:Ignorable="d" Height="900" Width="1100">
    <Grid Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="48"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Cursor="Hand" Width="42" Height="42" BorderThickness="0" VerticalAlignment="Top" 
                    HorizontalAlignment="Right" x:Name="btnClose">
            <Button.Background>
                <ImageBrush ImageSource="/Images/PC/Close1.png"/>
            </Button.Background>
        </Button>
        <cef:ChromiumWebBrowser x:Name="webBrowser" Grid.Row="1"/>
        <Grid VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="gdLoad" Grid.Row="1">
            <StackPanel>
                <Image Source="/Images/PC/LoadingData.png" Width="{StaticResource LoadingImageWidth}" Height="{StaticResource LoadingImageHeight}"></Image>
                <TextBlock Margin="0,14,0,0" Text="加载中…" Foreground="#666666" FontSize="{StaticResource FontSize12}" HorizontalAlignment="Center"></TextBlock>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl> 

public partial class UcSampleBankView : UserControl
    {
        public UcSampleBankView()
        {
            InitializeComponent();

            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            CefSharpSettings.ConcurrentTaskExecution = true;

            string address =ConfigurationManager.AppSettings["SampleBank"] + "?token=" + ViewModel.Global.CurrentUser.token;   //"http://localhost:8080/" + "?token=" + ViewModel.Global.CurrentUser.token;
            webBrowser.Address = address;

            webBrowser.LoadError += _webView_LoadError;

            webBrowser.FrameLoadEnd += (sender, e) =>
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    gdLoad.Visibility = System.Windows.Visibility.Collapsed;
                });
                this.Dispatcher.Invoke(new Action(delegate
                {
                    webBrowser.WebBrowser.ExecuteScriptAsyncWhenPageLoaded(@"(async function() {await CefSharp.BindObjectAsync('callbackObj', 'bound');})();");
                }));
            };
            webBrowser.MenuHandler = new MenuHandler();

            注册js对象据说,79版本以上的注册方法是下面这句
            webBrowser.JavascriptObjectRepository.Register("bound", new FormMainProcess(this), false);

            btnClose.Click += (sender, e) =>
            {
                this.ResultData?.Invoke(false);
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    xxx.Close();
                });
                OnClose();
            };
        }

        public void OnClose()
        {
            try
            {
                webBrowser.CloseDevTools();
                webBrowser.GetBrowser().CloseBrowser(true);
            }
            catch { }

            try
            {
                if (webBrowser != null)
                {
                    webBrowser.Dispose();
                }
            }
            catch { }
        }

        public void _webView_LoadError(object sender, CefSharp.LoadErrorEventArgs e)
        {
            MessageBox.Show("加载页面失败,请退出客户端进行重试");
            webBrowser.Dispose();
        }
    }


    [ComVisible(true)]
    public class FormMainProcess : IResultObject
    {
        UserControl control;
        public Action<object> ResultData { get; set; }
        public FormMainProcess(UserControl user)
        {
            control = user;
        }


        public void speakMsg(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                var result = false;
                ViewModel vm = new ViewModel();
                if (proId != null)
                {
                    var resultList = JsonConvert.DeserializeObject<List<VoModel>>(message);
                    vm.List = resultList;

                    result = vm.xxx();
                }
   

                if (result)
                {
                    var view = control as UcSampleBankView;
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        view.OnClose();
                        view.ResultData?.Invoke(result);
                        ControlManager.Close();
                    });
                }
            }
        }
    }
    /// <summary>
    /// cef菜单事件
    /// </summary>
    public class MenuHandler : CefSharp.IContextMenuHandler
    {

        void CefSharp.IContextMenuHandler.OnBeforeContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model)
        {
            model.Clear();
        }

        bool CefSharp.IContextMenuHandler.OnContextMenuCommand(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.CefMenuCommand commandId, CefSharp.CefEventFlags eventFlags)
        {
            //throw new NotImplementedException();
            return false;
        }

        void CefSharp.IContextMenuHandler.OnContextMenuDismissed(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame)
        {
            //throw new NotImplementedException();
        }

        bool CefSharp.IContextMenuHandler.RunContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model, CefSharp.IRunContextMenuCallback callback)
        {
            return false;
        }
    }
    public class SimpleModel
    {
        public long? pageNum { get; set; }
        public long? id { get; set; }
        public long? categoryId { get; set; }
        public string name { get; set; }
        public string amount { get; set; }
        public string no { get; set; }
    }

 

vue

window.bound.speakMsg(JSON.stringify(this.selectMap));
console.log(JSON.stringify(this.selectMap));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值