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

XamlControlsResources causes crash when used from plain win32 desktop app #7606

Closed
AzAgarampur opened this issue Aug 13, 2022 · 13 comments
Closed
Labels
bug Something isn't working Crash whenever user reports a crash or app freeze needs-triage Issue needs to be triaged by the area owners no-issue-activity product-winui3 WinUI 3 issues team-Markup Issue for the Markup team transfer-repo

Comments

@AzAgarampur
Copy link

AzAgarampur commented Aug 13, 2022

Describe the bug

When trying to use WinUI 3 with a plain win32 app from scratch (unpackaged & no winui project template), instantiating XamlControlsResources to place into your Application's resources causes a crash.

Steps to reproduce the bug

  1. Download the example VS solution attached here
  2. Build and run
  3. Notice how there is a crash
  4. Comment out the code in the .ctor of the App class
  5. Rebuild and run
  6. Notice how everything works now (except lack of WinUI styles)

Expected behavior

Creating XamlControlsResources won't cause an exception and crash the app; this program should work just like every other unpackaged win32 desktop app using WinUI 3.

Screenshots

No response

NuGet package version

1.1.2

Packaging type

Unpackaged

Windows version

Windows 11 version 21H2 (22000)

IDE

Visual Studio 2022

Additional context

Zip file with VS solution:
Test.zip

@DarranRowe
Copy link

DarranRowe commented Aug 14, 2022

Did you actually try this out in an application that uses the WinUI templates? I ask, because the way you have attempted to add the resources causes it to throw a catastrophic failure even in an unpackaged WinUI3 application.
Adding resources to the resource directory fails in the application constructor. Presumably because the component that acts as the Xaml host isn't in a ready state until after the constructor completes. The Start static function is documented to handle the message queue for unpackaged apps, and this is what drives the DispatcherQueue.
I placed:
winrt::Microsoft::UI::Xaml::Controls::XamlControlsResources xc; Resources().MergedDictionaries().Append(xc);
into the OnLaunched handler and it worked. So try verifying this for yourself.
However, I do agree that just getting a catastrophic failure exception isn't exactly helpful.

@AzAgarampur
Copy link
Author

AzAgarampur commented Aug 15, 2022

Well I tried placing that code into the OnLaunched and it once again crashes with error:
Exception thrown at 0x00007FFA334C474C (KernelBase.dll) in Test.exe: WinRT originate error - 0x80004005 : 'Cannot find a resource with the given key: AcrylicBackgroundFillColorDefaultBrush.'.

Yeah, using it in a WinUI template works fine.

@riverar
Copy link
Contributor

riverar commented Aug 15, 2022

@AzAgarampur Instantiating XamlControlsResources requires a IXamlMetadataProvider implementation (last I checked) either by the XAML compiler (add app.xaml/etc. to your project) or by hand (good luck with that). WinUI 3 is quirky / undocumented, see also #5704

@btueffers btueffers transferred this issue from microsoft/WindowsAppSDK Aug 15, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Aug 15, 2022
@DarranRowe
Copy link

@AzAgarampur Getting it working from there doesn't actually require too much extra work. @riverar is correct in saying that the application requires an IXamlMetadataProvider implementation. But the fact is, as the name implies, it only needs to provide the metadata.
This means that the App class has to implement the IXamlMetadataProvider interface, but for the basic task of showing a standard Xaml window or controls, it is good enough to just fall through to the WinUI provided provider. The difficulty is when you are using custom controls, including having Xaml files in your project. If the project generates the XamlMetadataProvider class in the winrt::::implementation namespace then use that.
I attached a little project to this showing the basic provider at work, and it does get as far as showing the window. But be aware, Visual Studio will not generate the .xbf file for any xaml files in the Win32 desktop project. You can get around this in other ways, but it is annoying.
But the interesting thing about this is that this is the same issue you face getting a Xaml Islands application running. Using WinUI 2 in a Xaml Islands application required that you provide a custom application type to be able to load WinUI 2 properly. The same thing is true for the experimental release of Xaml Islands for the Windows App SDK/WinUI 3.
TestApplication.zip

@riverar
Copy link
Contributor

riverar commented Aug 15, 2022

While this is true, in my experience you won't get far with a more complete implementation of IXamlMetadataProvider sadly. It's one of the reasons the Rust implementation of the Windows App SDK has not progressed very far and I will be putting on hold. The reality is XAML is not designed/documented for use outside of MSBuild + Visual Studio + XAML compiler for C#/C++. If you try to venture off this path, you're going to be running into a lot of walls.

@gabbybilka gabbybilka added product-winui3 WinUI 3 issues bug Something isn't working labels Aug 19, 2022
@lyahdav
Copy link

lyahdav commented Sep 23, 2022

@DarranRowe thanks for providing that test app, this is very helpful since I'm trying to get a WinUI 3 app building without Visual Studio. But when I run your solution, on app launch I get error:

Exception thrown at 0x00007FFE85E14FD9 (KernelBase.dll) in TestApplication.exe: WinRT originate error - 0x80070005 : 'Access is denied.'.

Here's the full output: https://gist.github.com/lyahdav/00db0cdcaae9eb2602b2a4a45f79677e
And here's the call stack: https://gist.github.com/lyahdav/2a574c318c15655c11083f73b613955f

Any idea what's going on?

@lyahdav
Copy link

lyahdav commented Sep 23, 2022

Oh wait, I think that's due to WinAppSDK 1.1.4 bug: microsoft/WindowsAppSDK#2918. I'll try to downgrade and report back.

@lyahdav
Copy link

lyahdav commented Sep 23, 2022

Yeah that was it. I downgraded the WindowsAppSDK version to 1.0.4 and I also had to install the 1.0.4 runtime from here: https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/downloads#windows-app-sdk-10. Then the app launched.

@bpulliam bpulliam added the Crash whenever user reports a crash or app freeze label Oct 13, 2022
@bpulliam bpulliam added the team-Markup Issue for the Markup team label Oct 30, 2022
@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@sotanakamura
Copy link

I would like to share a step by step guide to create a WinUI 3 app from an empty win32 project. This issue helped me to apply the Winui3 theme. Thank you all.
https://github.com/sotanakamura/winui3-without-xaml

@riverar
Copy link
Contributor

riverar commented Feb 4, 2023

@sotanakamura You won't get far that way. You will need to include the WinUI 3 nuget to bring in required SxS manifests, the xaml compiler, etc.

@sotanakamura
Copy link

sotanakamura commented Feb 4, 2023

@riverar Thank you for your reply. In that guide I wrote about installing WinUI 3 and the C++/WinRT nuget packages. Is it still having problems? I am not a Windows app developer, I am just playing with WinUI 3. That guide is not for product development. If that guide still has problems, please let me know.

@github-actions
Copy link

github-actions bot commented Aug 4, 2023

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
@tomaszkot
Copy link

I have similar issue: microsoft/WindowsAppSDK-Samples#328

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Crash whenever user reports a crash or app freeze needs-triage Issue needs to be triaged by the area owners no-issue-activity product-winui3 WinUI 3 issues team-Markup Issue for the Markup team transfer-repo
Projects
None yet
Development

No branches or pull requests

9 participants