I have a WinUI 3 Class Library containing a page with a NavigationView
(MyFabulousPage.xaml), where clicking on any item navigates the frame to 'SamplePageA'. When I use the class library from a WinUI unpackaged application, all works as expected.
However, when I try to use the library from a WinForms application which hosts MyFabulousPage
in an island, navigation to SamplePageA
crashes with AccessViolationException
somewhere in the depths of WinRT.
I tried to add an app.manifest
file as suggested in this article, but it didn't resolve the issue.
Full source is available here: https://github.com/nikolayvpavlov/Session_WindowsDesktop2024
For a XAML application, there can be multiple IXamlMetadataProvider instances. By default all this is generated by the tooling and the application defers all XAML metadata resolving to the providers.
When you write your own custom application, the system will still ask at runtime for various information. In your project, since it's a Winforms application, the XAML metadata corresponding the the WinUI3 project is not exposed automatically by the tooling so the app crashes when trying to get it (error handling in UWP/WinUI3/XAML is currently very poor and that's a pitty...).
This XAML metadata provider resides in a file
XamlTypeInfo.g.cs
that you can have a look at and is located usually in the temp/generatedobj\Debug\net7.0-windows10.0.19041.0
(version can vary) folder.This is what this
XamlTypeInfo.g.cs
file looks like:The trick is you cannot reference it the
MyWinUILibrary.MyWinUILibrary_XamlTypeInfo.XamlMetaDataProvider
class directly, as there's a chicken vs egg problem (the project will not compile before the file is generated and it will only be generated when the project compiles...).So what you can do is load it by reflection at startup. Here is a modified version of your app that works better: