CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

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

Open marb2000 opened this issue 4 years ago • 10 comments

This issue is a duplication of the https://github.com/microsoft/microsoft-ui-xaml/issues/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

marb2000 avatar Feb 20 '21 06:02 marb2000

@marb2000 I am following your repro steps, and when I build the packaging project it fails with the error below. Did you experience this?

Error Unable to find WinRT class registrations for WinUI. Please ensure that the Microsoft.WinUI package restored successfully.	WinUIApp (Package)	C:\gh\Repros\GH_747\WinUIApp\WinUIApp (Package)\build\Microsoft.WinUI.AppX.targets	31	

j0shuams avatar Mar 19 '21 18:03 j0shuams

Sorry @j0shuams, I didn't aware you send this to me. I can create a repro for you if needed.

marb2000 avatar Apr 26 '21 21:04 marb2000

@marb2000 It is okay, I got a repro thanks. I have an idea for a solution but it will require some work. I will update on when I will be focusing on this.

j0shuams avatar Apr 26 '21 23:04 j0shuams

@marb2000 Thanks for your patience on this. I am working on a solution. This has been a difficult one, as WinRT does not support covariance and we are trying to add ad-hoc support for it. Hoping to have a solution in this week.

j0shuams avatar May 17 '21 19:05 j0shuams

This looks like it might be fixed by a .NET change: https://github.com/dotnet/runtime/issues/58619

manodasanW avatar Sep 03 '21 18:09 manodasanW

Sounds like the mentioned change is a prerequisite and in addition to that we will need some changes too.

manodasanW avatar Sep 03 '21 23:09 manodasanW

I'm running into a similar issue right now trying to cast from WinRT.IInspectable to Windows.UI.Xaml.DependencyObject. Do you think that is the same root problem?

scout208 avatar Jan 25 '22 16:01 scout208

@scout208 Could be, are you trying to use covariance? I just checked and the issue is still there for the version I'm using (but I'm not using .NET 6 or VS 2022 yet if that matters).

stevenwdv avatar Jan 25 '22 23:01 stevenwdv

@stevenwdv Thanks for the reply. I am using .NET 6. Actually, I think my issue was more closely related to this one https://github.com/microsoft/CsWinRT/issues/977 and the solution provided there worked for me. I was trying to pass the PrimaryCommands property of a CommandBar to a method and when on UWP it would always get passed as a DependencyObject for some reason on WinUI it gets passed as WinRT.IInspectable. Not really sure why that is.

Anyways, I just added (IObservableVector<ICommandBarElement>)(object) before the object in the method I passed it to and I was able to get it to do what I needed to do.

scout208 avatar Jan 25 '22 23:01 scout208

Internal bug: https://microsoft.visualstudio.com/OS/_workitems/edit/37676724

I hit a very similar issue, invalid cast from WinRT.IInspectable, when I was trying to access IReadOnlyList of displayAreas

Steps to repro:

  1. Create Blank app, Packaged (WinUI3 in Desktop) C#
  2. During myButton_Click event, call the following apis: IReadOnlyList<Microsoft.UI.WIndowing.DisplayArea> displayAreas = Microsoft.UI.WIndowing.DisplayArea.FindAll(); if(displayAreas != null) { // Now try to access display areas displayAreas.Count or displayAreas[0] int displayAreasCount = displayAreas.Count; // This line throws an invalid cast from WinRT.IInspectable exception }
  3. When the button is clicked the invalid cast from WinRT.IInspectable exception is raised.

deepthi-msft avatar Feb 04 '22 20:02 deepthi-msft