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

Hiding the parent form does not hide webview2 control #1079

Closed
RalphLAtGitHub opened this issue Mar 16, 2021 · 9 comments
Closed

Hiding the parent form does not hide webview2 control #1079

RalphLAtGitHub opened this issue Mar 16, 2021 · 9 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@RalphLAtGitHub
Copy link

RalphLAtGitHub commented Mar 16, 2021

Description
Hiding the parent form does not hide WebView2 control but shows it at the origin of the screen.

Repro Steps
Create a MDI form and child with WebView2 inside. Hide MDI child programmatically.

Screenshots
MDI_1
MDI_2

Einvironment
Windows 10 Pro 20H2 Build 19042.844
Visual Studio 16.9.1
.NET 5.0.4 WindowsDesktop
Edge 89.0.774.50
WebView2 1.0.774.44

AB#32237299

@RalphLAtGitHub RalphLAtGitHub added the bug Something isn't working label Mar 16, 2021
@champnic
Copy link
Member

Hey @RalphLAtGitHub - I'm not familiar with MDI forms specifically. The visibility of the web content is tied to the WebView2 control's Visibility. When I try to repro with a regular form and call hide on the form, the form calls Hide() on the children (or sets .Visible = false), which our control then forwards to the web content to tell it to hide. It's possible the MDI form doesn't automatically tell the children to hide? When your MDI form is hiding, can you try calling WebView2.Visible = false; or WebView2.Hide() and see if that fixes the issue?

@champnic champnic self-assigned this Mar 22, 2021
@RalphLAtGitHub
Copy link
Author

Hi @champnic: Of course, .Hide() solves the problem, but only for child windows on the first level below the MDI form and unfortunately not for child windows on lower levels. Therefore this workaround is not suitable for me.

The only "solution" that I have found so far is to close the form containing the WebView2 control via .Close(). This only works without errors if the form was previously displayed with .Show() or .Visible = true. If not, closing throws an exception: "Object reference not set to an instance of an object."
Stack trace is:
at Microsoft.Web.WebView2.WinForms.WebView2.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

You can find a demo here: https://github.com/RalphLAtGitHub/WebView2-MDI

Unfortunately, I have to create the form containing the WebView2 control (asynchronously) in advance, because I have not yet succeeded in creating it synchronously in WinForms (see #1065).

Thanks in advance for your support!

@champnic
Copy link
Member

Thanks for the repro app @RalphLAtGitHub, I took a quick look at it. It doesn't look like the WebViewForm is being added as a child of the Form1 unless I'm missing something? Typically the expectation is that the parent cleans up it's children, or in this case, calls Hide on all children when closing. If the WebView2 is a child of the Form1 then this should happen automatically.

@RalphLAtGitHub
Copy link
Author

@champnic: The WebViewForm is (according to the documentation) added as a MDI child in the ctor of Form1:
m_WebViewForm = New WebViewForm With {.MdiParent = Me, .Name = "DEMO"}
Debug.WriteLine("First MDI-child " + Me.MdiChildren.First.Name + " is of type " + Me.MdiChildren.First.GetType.ToString) gives the expected result: First MDI-child DEMO is of type WebView2.WebViewForm
Maybe the WebView2 control does not process the window messages correctly in this special case?

@champnic
Copy link
Member

Thanks for confirming - the integration with MDI is new to me and not a scenario we've specifically tested, so it's definitely possible we aren't handling a window message we should be. Our control overrides OnVisibleChanged (as a result of Show()/Hide()/Visible = x or other code paths) to tell the CoreWebView2 to show or hide. I can open a bug on our backlog to debug more in depth, but we probably won't be able to look at this for a while. So I would suggest looking into any workaround that would allow propagating the .Visible/Hide()/Show() to the WebView2 control.

@champnic champnic added the tracked We are tracking this work internally. label Mar 24, 2021
@RalphLAtGitHub
Copy link
Author

Hi @champnic, I am happy to be able to make a small contribution. I fully understand the prioritization - there are enough more important things to do. I think everyone is eagerly awaiting a version of the WebView2 control that we can deploy to our users. I wish you and your team much success!

@champnic
Copy link
Member

Thanks @RalphLAtGitHub - we appreciate the kind words :)

@billybooth
Copy link

Ours is also an MDI application (granted, WinForms) in .NET, but I haven't extended the WebView2 to any of our envisioned uses that land it in MDI child windows. I'll see if I can carve out a bit of time to do the preliminary implementation and let you know whether we can reproduce the issue.

@champnic
Copy link
Member

champnic commented Jun 5, 2021

This should be fixed in 1.0.902-prerelease SDK. Thanks!

@champnic champnic closed this as completed Jun 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

3 participants