marb2000 · GitHub

This issue is a duplication of the microsoft/microsoft-ui-xaml#4111 that stevenwdv created and it seems a C#/WinRT projection issue.

Casting a collection of StorageFiles to a collection of IStorageFiles and using it fails at runtime (this may also be the case with other interfaces, not sure. This only happens when using Desktop WinUI 3.

Steps to reproduce the bug

  1. Create a new C# "Blank App, Packaged (WinUI in Desktop)"
  2. Add the following code to e.g. OnLauched in App.xaml.cs:
Task.Run(async () =>
{
    try
    {
        var folder = ApplicationData.Current.LocalFolder;
        var files  = await folder.GetFilesAsync();
        _ = ((IReadOnlyList<IStorageFile>) files).ToList();
    }
    catch (InvalidCastException ex)
    {
        Debugger.Break();
    }
});
  1. Run the package project (enabling break on CLR exceptions recommended)
  2. Observe the InvalidCastException in DynamicInterfaceCastableHelpers.IsInterfaceImplemented

Expected behavior
Should work without exception.

Version Info
[Microsoft.WinUI 3.0.0-preview4.210210.4]

Windows app type:

UWP Win32
No Yes
Windows 10 version Saw the problem?
Insider Build (20H2: 19042.746) Yes
May 2020 Update (19041) Yes
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

Read the original on github.com ↗