WinUI 2's XamlMetadataProvider provides incorrect UnderlyingType for System.Windows.Input.ICommand
See original GitHub issueDescribe 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:
- Create a new blank C# UWP app
- Add a reference to the Microsoft.UI.Xaml/WinUI 2 NuGet package (and add
XamlControlsResourcestoApp.xamlas normal) - 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
}
- Run the app
- 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.ICommandin 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.ICommandanywhere
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:
- Created 2 years ago
- Reactions:1
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
@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.Yes, I believe the solution under the
Additional Contextsection could resolve it (WinUI 2’s metadata provider has anIXamlTypeforICommand, but itsIXamlType.FullNameproperty should be in the nativeWindows.UI.Xaml.Input.ICommandnamespace instead of the managedSystem.Windows.Input.ICommandnamespace).