Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

initialization - Initialize cefsharp browser instance via code in C # WPF project

I created a small application in WPF C # using CefSharp as a browser and thanks to "Amaitland" it is working fine. But now I need to make the two other cefbrowser controls initialize via code without requiring the user to click on each tabitem. When I tried to initialize browser2 while loading the main form, a System.Exception was released saying that the browser was not initialized. Overview: The application contains a TabControl with 3 tabitens. In turn, each tabitem has a browser control defined via XAML (eg browser1 for tabitem 1, browser2 for tabitem 2, ...). When the application starts the first tabitem receives the focus and browser1 is initialized but the other two are not. So I need that, for example, browser2 is also initialize when the main form (windows_loaded event) is loaded in order to load a website (browser2.load(link)) at that moment. I saw that cefwebrowser2 only is initialized when tabitem2 is clicked with the mouse. I found many examples of cefbrowser initialization when loading the form for windows form but not found for WPF C # and I was unable to use the windows forms examples for WPF. Can someone give me a tip on how to initialize cefbrowser2 via code in my case? Thank you very much in advance.

Here is part of the code I used to load browser2 but without success.

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        
        //Dispatcher.BeginInvoke((Action)(() => tbrWeb.SelectedIndex = 2));
        string vLink = @"www.google.com";

        Browser2.BeginInit();
        Browser2.Focus();
        Browser2.Address = vLink;
        Browser2.Load(vLink);
    }

public MainWindow()
    {

        InitBrowser();

        InitializeComponent();

        //Delegates

        EventDownloadInfo += new DelInformDownload(mInfoDownload);
        DelPassaTamanhoArquivo = EventDownloadInfo;


    }

public void InitBrowser()
    {
        if (!Cef.IsInitialized)
        {
            
            CefSettings setting = new CefSettings();
            setting.CachePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\Cache");
            setting.CefCommandLineArgs.Add("enable-media-stream", "1");
            setting.CefCommandLineArgs.Add("allow-running-insecure-content", "1");
            setting.CefCommandLineArgs.Add("use-fake-ui-for-media-stream", "1");
            setting.CefCommandLineArgs.Add("enable-speech-input", "1");
            setting.CefCommandLineArgs.Add("enable-usermedia-screen-capture", "1");

            Cef.Initialize(setting);
            
            //BrowserExigencia.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;

        }

    }
question from:https://stackoverflow.com/questions/66067562/initialize-cefsharp-browser-instance-via-code-in-c-wpf-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...