在C#插件中使用ChromiumFX / ChromiumWebBrowser

时间:2019-10-31 10:58:37

标签: c# chromium-embedded

我正在尝试在C#/ WPF插件中添加ChromiumWebBrowser组件。 (在该插件使用CefSharp之前,但新版本的宿主应用程序在自定义AppDomain中运行插件,因此CefSharp不再起作用。)

问题是,当我调用ChromiumWebBrowser.Initialize()时,将启动主机应用程序的新实例(启动时带有一些命令行参数,这些参数会被应用程序删除)。

我真的不了解此命令行中的内容。

您能向我解释我在哪里做错了吗? ChromiumWebBrowser不能在插件内部使用?我想念什么吗?

这是我的代码的C#示例

public partial class myWindow : Window
{
        public myWindow()
        {

            CfxRuntime.LibCefDirPath = @"E:\sources\app\Output\Debug_msvc2017_x64\Addins\cef\Release64";

            ChromiumWebBrowser.OnBeforeCfxInitialize += ChromiumWebBrowser_OnBeforeCfxInitialize;
            ChromiumWebBrowser.OnBeforeCommandLineProcessing += ChromiumWebBrowser_OnBeforeCommandLineProcessing;
            Chromium.WebBrowser.ChromiumWebBrowser.Initialize();

            InitializeComponent();

            // browser is a ChromiumWebBrowser instance
            browser.MouseClick += Wb_MouseClick;
            browser.Show();
        }


        private void Wb_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            (sender as ChromiumWebBrowser).LoadUrl("https://www.google.com"); // properly trigger but nothings will be displayed
        }

        static void ChromiumWebBrowser_OnBeforeCommandLineProcessing(CfxOnBeforeCommandLineProcessingEventArgs e)
        {
            Console.WriteLine(e.CommandLine.CommandLineString); // here the command line create a new instance of the application where the plugin is loaded
        }

        static void ChromiumWebBrowser_OnBeforeCfxInitialize(OnBeforeCfxInitializeEventArgs e)
        {
            e.Settings.LocalesDirPath = @"E:\sources\app\Output\Debug_msvc2017_x64\Addins\cef\locales";
            e.Settings.ResourcesDirPath = @"E:\sources\app\Output\Debug_msvc2017_x64\Addins\cef\Resources";
        }    
}

‌ 还有xaml部分 ‌

<Window x:Class="App.Views.myWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:App.Views"
        xmlns:webbrowser="clr-namespace:Chromium.WebBrowser;assembly=ChromiumWebBrowser"
        mc:Ignorable="d"
        Title="myWindow" Height="800" Width="1200">
    <Grid x:Name="grid1">
        <WindowsFormsHost x:Name="wpfContainer">
            <webbrowser:ChromiumWebBrowser x:Name="browser"></webbrowser:ChromiumWebBrowser>
        </WindowsFormsHost>
    </Grid>
</Window>

在此先感谢您的帮助;)

0 个答案:

没有答案