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

Unable to use XamlReader.Load unless type is already used in a XAML file #7206

Open
1 of 2 tasks
mrlacey opened this issue Jun 7, 2022 · 3 comments
Open
1 of 2 tasks
Labels
area-Parser bug Something isn't working product-winui3 WinUI 3 issues team-Markup Issue for the Markup team wct

Comments

@mrlacey
Copy link
Contributor

mrlacey commented Jun 7, 2022

Describe the bug

It's not possible to use XamlReader to parse XAML in an application if the types being parsed aren't already included in a XAML file that was compiled into the app.

This may seem like a very niche case but it is very problematic for some testing and library scenarios.

Steps to reproduce the bug

  1. Create a new "Blank App, Packaged (WinUI 3 in Desktop)" and call it XamlReaderRepo
  2. Update all NuGet packages to the latest versions (v1.1.0 of Microsoft.WindowsAppSDK)
  3. Add this class to the project:
    public class MyCoolElement : FrameworkElement
    {
    }
  1. In MainWindow.xaml.cs, replace the event handler with the following:
        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            var pg = Microsoft.UI.Xaml.Markup.XamlReader.Load(@"<Page
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
                    xmlns:local=""using:XamlReaderRepo"">
                    <local:MyCoolElement />
                </Page>") as FrameworkElement;
            myButton.Content = pg.ToString();
        }
  1. Run the app, click the button, see it throws a XamlParseException.
  2. In MainWindow.xaml, add the following immediately under the Button but above the closing StackPanel tag:
<local:MyCoolElement />
  1. Run the app again, click the button, see the text changes.

Expected behavior

It should be possible to parse a type from a string without having to use it in a XAML file first.

I assume this is failing as some type information is being generated at compile time for everything that is in a .xaml file. Is there a way to trigger this for types not defined in xaml and compiled into the assembly?
Is there some metadata or attributes that can be added to a type definition to enable this? Or some way of specifying namespaces, or assemblies that contain types that the code might want to load as a XAML string.

Based on #4161 (comment) I thought it would be possible to put a [Windows.UI.Xaml.Data.Bindable] attribute on MyCoolElement to enable the expected behavior it does not.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.0.3

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 11 (21H2): Build 22000

Additional context

Issue discovered as part of work for Windows Community Toolkit. ;)

Possibly also connected to #6299

@mrlacey mrlacey added the bug Something isn't working label Jun 7, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jun 7, 2022
@asklar
Copy link
Member

asklar commented Jun 8, 2022

I would imagine this is currently by design as xaml compiler needs to generate xaml type info for non-framework types (such as anything under local:). Perhaps something C#-specific could be done to reify the xamltypeinfo at runtime but I don't believe anything like this exists today. @RealTommyKlein to keep me honest.

@krschau krschau added area-Parser team-Markup Issue for the Markup team labels Jun 8, 2022
@bpulliam bpulliam added the product-winui3 WinUI 3 issues label Oct 12, 2022
@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@github-actions
Copy link

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.

@mrlacey
Copy link
Contributor Author

mrlacey commented Jul 29, 2023

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Parser bug Something isn't working product-winui3 WinUI 3 issues team-Markup Issue for the Markup team wct
Projects
None yet
Development

No branches or pull requests

5 participants