Invalid cast from 'WinRT.IInspectable' to 'System.Collections.Generic.IEnumerable`1[Windows.Storage.IStorageFile]' using WinUI
See original GitHub issueCasting a collection of StorageFile
s 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
- Download the WinUI 3 Project Templates extension
- 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)
- Possibly: edit the non-package project file to make sure it lists the correct
TargetFramework
for me it listsnet5.0-windows10.0.18362.0
while I only have 10.0.19041.0 installed. (Issue: #4062) - Add the following code to e.g.
OnLauched
inApp.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();
}
});
- Run the package project (enabling break on CLR exceptions recommended)
- Observe the
InvalidCastException
inDynamicInterfaceCastableHelpers.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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 😉
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.