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

[WinUI3][Debug] Frame cann't navigate with the type from reflection from another assembly when not reference the project or assembly directly #8601

Open
SongOfYouth opened this issue Jul 3, 2023 · 3 comments
Labels
feature proposal New feature proposal team-Markup Issue for the Markup team

Comments

@SongOfYouth
Copy link

SongOfYouth commented Jul 3, 2023

Describe the bug

I have a custom class named BasePage (empty class) which inherited the Page class, and another class named MainPage inherited the BasePage, when i create a instance of MainPage by reflection,i got a error: Microsoft.UI.Xaml.Markup.XamlParseException:“XAML parsing failed.”, but if i refer the project which the MainPage in,and reflect it or create it by new,it works.

Steps to reproduce the bug

create a custom page with xaml, then create it's instance by reflection at another project.

NuGet package version

WinUI 3 - Windows App SDK 1.3.2: 1.3.230602002

Windows version

Windows 11 (22H2): Build 22621

@SongOfYouth SongOfYouth added the bug Something isn't working label Jul 3, 2023
@SongOfYouth SongOfYouth changed the title [WinUI3] Reflection not support xaml control [WinUI3] Reflection not support custom xaml control Jul 7, 2023
@SongOfYouth SongOfYouth reopened this Jul 7, 2023
@SongOfYouth
Copy link
Author

SongOfYouth commented Jul 12, 2023

tried some times, I find the reason. The compiler will generate a XamlTypeInfo.g.cs file when building, which the XamlTypeInfoProvider of other project will be builded in when reference other project with ProjectReference, and this section is missing when reflect a assembly directly. My solution code is :
in App.cs:

var appProviderProperty = this.GetType().GetProperty("_AppProvider", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var appProvider = appProviderProperty!.GetValue(this)!;
var appProviderProviderProperty = appProvider.GetType().GetProperty("Provider", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var appProviderProvider = appProviderProviderProperty!.GetValue(appProvider);
var othersProperty = appProviderProvider!.GetType().GetProperty("OtherProviders", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var otherProviders = othersProperty!.GetValue(appProviderProvider) as List<Microsoft.UI.Xaml.Markup.IXamlMetadataProvider>;
var originType = Assembly.Load("MyAssembly").GetType("MyAssembly.MyAssembly_XamlTypeInfo.XamlMetaDataProvider");
var provider = Activator.CreateInstance(originType!) as Microsoft.UI.Xaml.Markup.IXamlMetadataProvider;
otherProviders!.Add(provider!);

Is it possible to provide a frendly way to do it in future?

@bpulliam bpulliam added the team-Markup Issue for the Markup team label Aug 9, 2023
@SongOfYouth SongOfYouth changed the title [WinUI3] Reflection not support custom xaml control [WinUI3][DeBug] Reflection not support custom xaml control Nov 28, 2023
@SongOfYouth SongOfYouth changed the title [WinUI3][DeBug] Reflection not support custom xaml control [WinUI3][Debug] Reflection not support custom xaml control Nov 28, 2023
@JesseCol JesseCol added feature proposal New feature proposal and removed bug Something isn't working labels Feb 6, 2024
@SongOfYouth
Copy link
Author

image
System.AccessViolationException:“Attempted to read or write protected memory. This is often an indication that other memory is corrupt.” occured when call frame.NavigateTo(type) whitch the type is from another assembly and it not be refderenced any where.

@SongOfYouth SongOfYouth changed the title [WinUI3][Debug] Reflection not support custom xaml control [WinUI3][Debug] Frame cann't navigate the type from reflection from another assembky when not reference the project or assembly Aug 23, 2024
@SongOfYouth SongOfYouth changed the title [WinUI3][Debug] Frame cann't navigate the type from reflection from another assembky when not reference the project or assembly [WinUI3][Debug] Frame cann't navigate with the type from reflection from another assembky when not reference the project or assembly Aug 23, 2024
@SongOfYouth
Copy link
Author

What's more, i start the app with Unpackaged.

@SongOfYouth SongOfYouth changed the title [WinUI3][Debug] Frame cann't navigate with the type from reflection from another assembky when not reference the project or assembly [WinUI3][Debug] Frame cann't navigate with the type from reflection from another assembly when not reference the project or assembly directly Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature proposal New feature proposal team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

3 participants