question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WinUI 2's XamlMetadataProvider provides incorrect UnderlyingType for System.Windows.Input.ICommand

See original GitHub issue

Describe the bug WinUI 2’s metadata provider, Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider, contains incorrect type information for System.Windows.Input.ICommand type which can cause crashes.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create a new blank C# UWP app
  2. Add a reference to the Microsoft.UI.Xaml/WinUI 2 NuGet package (and add XamlControlsResources to App.xaml as normal)
  3. Add the following code to the app’s constructor in App.xaml.cs:
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            // Get the IXamlType for ICommand from WinUI 2's metadata provider, which is incorrectly created
            // with an invalid UnderlyingType property causing a crash when accessed
            var winuiXMP = new Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider();
            var winuiType = winuiXMP.GetXamlType("System.Windows.Input.ICommand");
            var winuiUnderlyingType = winuiType.UnderlyingType; //Crashes
        }
  1. Run the app
  2. The app will crash on the last line when attempting to access winuiType.UnderlyingType

In reality, this can occur in the following setup:

  • A C# application uses a property/field of type System.Windows.Input.ICommand in markup
  • The C# application references WinUI 2
  • The C# application references a C# control library, and the control library does not use System.Windows.Input.ICommand anywhere

Expected behavior winuiUnderlyingType.UnderlyingType returns the System.Type corresponding to System.Windows.Input.ICommand (or maybe winuiXMP.GetXamlType("System.Windows.Input.ICommand") should return null - see the “Additional Context” section)

Screenshots

Version Info

NuGet package version: [Microsoft.UI.Xaml 2.5.0]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

The WinUI 2 metadata provider might need to provide type information for Windows.UI.Xaml.Input.ICommand instead of System.Windows.Input.ICommand, since the Windows.UI.Xaml version is what exists in native code where WinUI 2’s metadata provider is written and should be projected to the System.Windows version.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
RealTommyKleincommented, Jul 23, 2021

@michael-hawker - it’s definitely the same issue (the callstack contains Windows.UI.Xaml.Markup.IXamlType.get_UnderlyingType), and this bug would affect both Debug and Release builds.

1reaction
RealTommyKleincommented, Jul 12, 2021

Yes, I believe the solution under the Additional Context section could resolve it (WinUI 2’s metadata provider has an IXamlType for ICommand, but its IXamlType.FullName property should be in the native Windows.UI.Xaml.Input.ICommand namespace instead of the managed System.Windows.Input.ICommand namespace).

Read more comments on GitHub >

github_iconTop Results From Across the Web

The type 'System.Windows.Input.ICommand' exists in both ' ...
The message is accurate, ICommand indeed exists in both assemblies for .NET 4.5. The problem is your reference to System.dll, your project is ......
Read more >
ICommand Interface (Windows.UI.Xaml.Input) - UWP
Defines the command behavior of an interactive UI element that performs an action when invoked, such as sending an email, deleting an item,...
Read more >
XamlUICommand Class (Microsoft.UI.Xaml.Input)
Provides a base class for defining the command behavior of an interactive UI element that performs an action when invoked (such as sending...
Read more >
Can I creamte my own ICommand replacement
So, I tried to create my own ICommand class and I named it ICommandX (lines 24-31, Class.cs file) . If I change the...
Read more >
Make Command Properties assignable to System.Windows ...
Assigning a Command to a shared view-model library using System.Windows.Input.ICommand results in the compilation error "Invalid binding ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found