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

WPF WebView2 not working in WPF Library #970

Closed
triod315 opened this issue Feb 22, 2021 · 10 comments
Closed

WPF WebView2 not working in WPF Library #970

triod315 opened this issue Feb 22, 2021 · 10 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@triod315
Copy link

triod315 commented Feb 22, 2021

Description
I have .net core 3.1 console application that uses WPF library. This library has a window with WebView.

<Window x:Class="WpfLibrary.TestWindow"
        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:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"       
        xmlns:local="clr-namespace:WpfLibrary"
        mc:Ignorable="d"
        Title="TestWindow" Height="450" Width="800">
    <DockPanel>
        <wv2:WebView2 Name="webView" Source="https://www.microsoft.com"  />
    </DockPanel>
</Window>

I am trying to use this window in my console app:

static void Main(string[] args)
        {
            WpfLibrary.TestWindow testWindow = null;
            Thread thread = new Thread(()=> {
                testWindow = new WpfLibrary.TestWindow();
                testWindow.ShowDialog();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }

And after start WebView2 shows a blank page instead of "https://www.microsoft.com" which written in Source. Also, I tried to create a window using System.Windows.Application instance, but got the same problem.

static void Main(string[] args)
        {
            WpfLibrary.TestWindow testWindow = null;
            Thread thread = new Thread(()=> {
                   testWindow = new WpfLibrary.TestWindow();
                   System.Windows.Application application = new System.Windows.Application();
                   application.Run(testWindow);
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }

To resolve this problem I tried to use EnsureCoreWebView2Async() to initialize CoreWebView2 but got next Error

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{4D00C0D1-9434-4EB6-8078-8697A560334F}' failed due to the following error: No such interface supported (0x80004002 (E_NOINTERFACE)).

Version
Microsoft.Web.WebView2: 1.0.705.50
Runtime: Edge Canary 90.0.803.0
Framework: WPF .Net Core 3.1
OS: Windows 10 version 2004, os build 19041.804

Additional context
This problem only in WebView in WPF. With WinForms all works fine. Also, this problem appears only if a window created in a new STA thread (like in code examle).

Repository with example

AB#32059542

@triod315 triod315 added the bug Something isn't working label Feb 22, 2021
@champnic
Copy link
Member

champnic commented Mar 2, 2021

Hey @triod315 - Can you share the code you wrote using EnsureCoreWebView2Async()? Typically we see cast errors like that when there isn't a supported runtime, but in this case Canary 90.0.803.0 should be fine.

@champnic champnic self-assigned this Mar 2, 2021
@triod315
Copy link
Author

triod315 commented Mar 7, 2021

Hi @champnic, thank you for your response, you can see my usage of EnsureCoreWebView2Async() in my sample in file TestWindow.xaml.cs. This file contains a method

public async void TryInitWebView2() 
{
       await webView.EnsureCoreWebView2Async();
}

And I am trying to use this function in the console app

static void Main(string[] args)
{
        WpfLibrary.TestWindow testWindow = null;
        Thread thread = new Thread(()=> {
              testWindow = new WpfLibrary.TestWindow();
              testWindow.TryInitWebView2();
              testWindow.ShowDialog();
       });
       thread.SetApartmentState(ApartmentState.STA);
       thread.Start();
       thread.Join();
}

After that, I got an error, described above.
But I think WebView should working without EnsureCoreWebView2Async() because webView defined in xaml file.
In windows forms it works good, but in wpf no.

@champnic champnic added the tracked We are tracking this work internally. label Mar 9, 2021
@champnic
Copy link
Member

champnic commented Mar 9, 2021

Thanks for the added info. I've added this bug to our backlog to take a look!

@GihanSoft
Copy link

GihanSoft commented Sep 19, 2021

I have same problem. It works fine if windows class in same assembly as Main (startup project). But it throw if it is in other assembly.

@jackyg2jpd93
Copy link

I Have Same Problem Too. I can use Winforms to Work WebView2,But WPF is blank.....
To use Method "EnsureCoreWebView2Async()" is panding
so I use WindowsFormsHost in WPF(Use Winforms to Work WebView2 then embedded in WPF)

@AndyT-MS
Copy link

AndyT-MS commented Oct 4, 2021

Hi @triod315 -- I strongly suspect that you have the same problem as #1577, which is that your WebView2 control is trying to initialize before your app's event loop is running. See my comment on that thread for additional details and a potential workaround. Perhaps there's another possible workaround for you that involves using Application.StartupUri as the original post in that thread discusses, though I'm not sure whether you can set it to a xaml/window/uri in another library.

@AndyT-MS
Copy link

AndyT-MS commented Oct 4, 2021

Hi @GihanSoft -- There are a couple of issues that can often appear as though the problem is that the WebView2-related window is in a library assembly rather than the app's assembly. Two such issues are discussed in #730 and #1418. Take a look through those discussions and see if either of them might apply to your situation.

@AndyT-MS
Copy link

AndyT-MS commented Oct 4, 2021

Hi @jackyg2jpd93 -- Unfortunately "WebView2 control is blank" can indicate a wide variety of potential issues. Calling await EnsureCoreWebView2Async() explicitly should at least throw an exception that should provide more detail on exactly what is going wrong. Searching this repo's issues for the exception's type and/or message should help you find more information about your problem.

@champnic
Copy link
Member

champnic commented Mar 4, 2023

This should be fixed now (as of a while ago). If you are still seeing this problem please let me know and what versions you are using. Thanks!

@champnic champnic closed this as completed Mar 4, 2023
@triod315
Copy link
Author

@champnic, @AndyT-MS, It's probably too late for my response, but thanks for the fix and support; I appreciate it :).

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

5 participants