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

WebView2 created from C++/WinRT does not render #9268

Closed
Tracked by #2
kasperisager opened this issue Jan 20, 2024 · 3 comments
Closed
Tracked by #2

WebView2 created from C++/WinRT does not render #9268

kasperisager opened this issue Jan 20, 2024 · 3 comments
Labels
area-WebView bug Something isn't working team-Controls Issue for the Controls team

Comments

@kasperisager
Copy link

kasperisager commented Jan 20, 2024

Describe the bug

Consider the following C++ application:

struct App : public ApplicationT<App, IXamlMetadataProvider> {
  void
  OnLaunched (LaunchActivatedEventArgs const &) {
    Resources().MergedDictionaries().Append(XamlControlsResources());

    WebView2 web_view;
    web_view.Source(Uri(L"https://github.com"));

    Window window;
    window.Content(web_view);
    window.Activate();
  }

  XamlControlsXamlMetaDataProvider provider;

  IXamlType
  GetXamlType (hstring const &name) {
    return provider.GetXamlType(name);
  }

  IXamlType
  GetXamlType (TypeName const &type) {
    return provider.GetXamlType(type);
  }

  com_array<XmlnsDefinition>
  GetXmlnsDefinitions () {
    return provider.GetXmlnsDefinitions();
  }
};

int
main () {
  init_apartment();

  Application::Start([] (auto &&) { make<App>(); });
}

When run, the web view isn't rendered and I instead see a blank window. The NavigationStarting event also doesn't fire, but I do see a <exe>.WebView2 directory created.

Steps to reproduce the bug

  1. Compile and run the code above.

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.4: 1.4.231219000

Windows version

Windows 11 (22H2): Build 22621

Additional context

If relevant, this is the manifest used:

<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity name="App" version="1.0.0.0" />

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
    </windowsSettings>
  </application>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <maxversiontested Id="10.0.18362.1"/>
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
  </compatibility>
</assembly>
@kasperisager kasperisager added the bug Something isn't working label Jan 20, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 20, 2024
Copy link

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@kasperisager
Copy link
Author

I've updated my code example per #9265 (comment) and am still not seeing the web view.

@bpulliam bpulliam added area-WebView team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels Jan 22, 2024
@kasperisager
Copy link
Author

kasperisager commented Jan 22, 2024

The cause seems to be initialising COM for multi-threaded object concurrency as apartment-threaded object concurrency seems to fix it:

init_apartment(apartment_type::single_threaded);

It's actually right there in the documentation as well:

The WebView2 control is based on the Component Object Model (COM) and must run on a Single Threaded Apartments (STA) thread.

https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/threading-model

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

No branches or pull requests

2 participants