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.

Invalid cast from 'WinRT.IInspectable' to 'System.Collections.Generic.IEnumerable`1[Windows.Storage.IStorageFile]' using WinUI

See original GitHub 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 WinUI on Desktop, not with UWP.

Steps to reproduce the bug

  1. Download the WinUI 3 Project Templates extension
  2. Create a new C# “Blank App, Packaged (WinUI in Desktop)” via the template (two projects will be created, one for the code, one for the package)
  3. Possibly: edit the non-package project file to make sure it lists the correct TargetFramework for me it lists net5.0-windows10.0.18362.0 while I only have 10.0.19041.0 installed. (Issue: #4062)
  4. 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-preview3.201113.0]

Windows app type:

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
stevenwdvcommented, Feb 6, 2021

@StephenLPeters No, I actually explicitly mentioned that issue in the steps but it’s not the issue I report here. Please read my report again more slowly 😉

0reactions
github-actions[bot]commented, Jul 28, 2023

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid cast from 'WinRT.IInspectable' to ' ...
Casting a collection of StorageFile s to a collection of IStorageFiles and using it fails at runtime (this may also be the case...
Read more >
c# - Invalid cast from 'WinRT.IInspectable' to 'System. ...
Why does the following Desktop UWP (or WinUI or WinRT? I get confused by the terminology) code compile but throw an exception?
Read more >
C++/WinRT DataTemplateSelector : Unable to cast object
I want to create a Template Selector in WinUI 3 using c++/WinRT. For example, if the type of a person is "Employee" i...
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