Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using pre-created environment with WinForms WebView2 control #177

Closed
ebriangast opened this issue May 15, 2020 · 6 comments
Closed
Labels
bug Something isn't working

Comments

@ebriangast
Copy link

ebriangast commented May 15, 2020

I've followed the Getting Started with WebView2 in Windows Forms documentation and am now trying to extend the application to pre-create the CoreWebView2Environment with a specific user data folder.

It seems the setting of WebView2.Source in the designer generated code triggers the automatic initialization of the environment before I have a chance to call EnsureCoreWebView2Async to provide my own. I see this happen even with the WebView2 control Source property default value of "about:blank", and if I try to remove the value for the Source property in designer I get an error of "Property value is not valid."

Specifically, I added code to Main to create the environment and pass it to the Form constructor:

           String userDataFolder = Path.GetTempPath() + @"NETWebView2UserDataTest";

           CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();
           CoreWebView2Environment env = CoreWebView2Environment.CreateAsync("", userDataFolder, options).GetAwaiter().GetResult();

           Application.Run(new Form1(env));

Then immediately called EnsureCoreWebView2Async after calling InitializeComponent:

        public Form1(CoreWebView2Environment webView2Env = null)
        {
            InitializeComponent();
            webView.EnsureCoreWebView2Async(webView2Env);
            webView.CoreWebView2Ready += WebView_CoreWebView2Ready;

            this.Resize += new System.EventHandler(this.Form_Resize);
        }

        private void WebView_CoreWebView2Ready(object sender, EventArgs e)
        {
            webView.CoreWebView2.Navigate(addressBar.Text);
        }

And also set an appropriate default of addressBar.Text in the designer.

If I manually comment out the setting of webView.Source in the Designer.cs file this works, and the environment I constructed is used. But making additional changes in the designer reverts this change to the generated code.

AB#26434822

@david-risney david-risney added the bug Something isn't working label May 15, 2020
@david-risney
Copy link
Contributor

Thanks for reporting this @ebriangast! Great issue. We'll look into how we might address this.

@igotbass
Copy link

@ebriangast thank you as I spent the last 2 hours wondering why this is not working.

Same issue, my use case is a application that is being delivered via RemoteApp where each user has own directory, if source is set it will not be able to set the user directory. Comment out the source solves the issue.

Thanks

@champnic
Copy link
Member

This should be fixed now - setting Source to about:blank should avoid the implicit initialization and this error. If you still see this please reopen. Thanks!

@champnic champnic reopened this Aug 15, 2020
@champnic
Copy link
Member

Apologies, apparently we have a fix but it hasn't been committed yet. This should be fixed soon!

@champnic
Copy link
Member

champnic commented Oct 7, 2020

We're making a change to the default behavior here so that the Source property doesn't get automatically set for Winforms. Instead the default is null, and the dev can choose to set Source if they want implicit initialization, or can manually create the environment and initialize explicitly. Additionally, we are adding a CoreWebView2CreationProperties like the existing WPF one, to allow easily setting the environment while still using the Source property. These changes will be coming in about 2 weeks. Thanks!

@champnic
Copy link
Member

This should be fixed in WebView2 SDK 1.0.674-prerelease. If this is still an issue please let us know. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants