Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException in ItemsControl with custom container #9855

Closed
hmg1000 opened this issue Jul 25, 2024 · 3 comments
Closed

InvalidCastException in ItemsControl with custom container #9855

hmg1000 opened this issue Jul 25, 2024 · 3 comments
Labels
area-C#/WinRT bug Something isn't working fix-released The fix has been in a release (experimental, preview, stable, or servicing). team-Reach Issue for the Reach team
Milestone

Comments

@hmg1000
Copy link

hmg1000 commented Jul 25, 2024

Describe the bug

Since version 1.6.240701003-experimental2 I'm getting an InvalidCastException in a custom ItemsControl when iterating over its container elements.

Steps to reproduce the bug

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System.Linq;

namespace TestWinUI;

internal class CustomItemsControl : ItemsControl
{
    public CustomItemsControl()
    {
        this.Loaded += TestControl_Loaded;
    }

    private void TestControl_Loaded(object sender, RoutedEventArgs e)
    {
        // Throws invalid cast exception:
        List<CustomItem> items = this.Items
            .Select((item, index) => this.ContainerFromIndex(index))
            .OfType<CustomItem>()
            .ToList();

        // This works:
        CustomItem customItem = (CustomItem)this.ContainerFromIndex(0);
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new CustomItem();
    }
}

internal class CustomItem : ContentControl
{
}

MainWindow.xaml:

<Window
    x:Class="TestWinUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="using:TestWinUI"
    mc:Ignorable="d">

    <local:CustomItemsControl x:Name="control"/>
</Window>

MainWindow.xaml.cs:

using Microsoft.UI.Xaml;

namespace TestWinUI;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        this.control.ItemsSource = new string[] { "1" };
    }
}

Expected behavior

No InvalidCastException should be thrown

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6 Experimental 2: 1.6.240701003-experimental2

Windows version

No response

Additional context

This is the stacktrace of the InvalidCastException:

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at WinRT.IObjectReference.As[T](Guid iid)
at System.Collections.Generic.IEnumeratorImpl1.Make_IEnumeratorObjRef() at System.Collections.Generic.IEnumeratorImpl1.get_iEnumeratorObjRef()
at System.Collections.Generic.IEnumeratorImpl1.Windows.Foundation.Collections.IIterator<T>.get_HasCurrent() at ABI.System.Collections.Generic.FromAbiEnumerator1.MoveNext()
at System.Linq.Enumerable.d__2292.MoveNext() at System.Linq.Enumerable.<OfTypeIterator>d__661.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TestWinUI.CustomItemsControl.TestControl_Loaded(Object sender, RoutedEventArgs e) in D:\Repos\TestWinUI\TestWinUI\CustomItemsControl.cs:line 18
at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.b__1_0(Object sender, RoutedEventArgs e)
at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

@hmg1000 hmg1000 added the bug Something isn't working label Jul 25, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jul 25, 2024
@hmg1000 hmg1000 changed the title InvalidCastException in ItemsControl with Custom Container InvalidCastException in ItemsControl with custom container Jul 25, 2024
@JesseCol JesseCol added team-Reach Issue for the Reach team area-C#/WinRT and removed needs-triage Issue needs to be triaged by the area owners labels Jul 29, 2024
@Scottj1s
Copy link
Member

Scottj1s commented Aug 1, 2024

@hmg1000 Thanks for reporting this. A fix will be available in the next C#/WinRT release (thanks @manodasanW)

@Scottj1s Scottj1s closed this as completed Aug 1, 2024
@hmg1000
Copy link
Author

hmg1000 commented Aug 16, 2024

@Scottj1s This is still an issue in Windows App SDK 1.6 Preview 1. Will the fix be included in the final version?

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Aug 16, 2024
@codendone
Copy link
Contributor

@hmg1000 Have you tried adding a direct nuget package reference to Microsoft.Windows.CsWinRT 2.1.1? 1.6-preview1 references an older version. The final 1.6 will use 2.1.1 (or later).

@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Aug 22, 2024
@bpulliam bpulliam added this to the WinAppSDK 1.6 milestone Aug 23, 2024
@codendone codendone added the fix-released The fix has been in a release (experimental, preview, stable, or servicing). label Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-C#/WinRT bug Something isn't working fix-released The fix has been in a release (experimental, preview, stable, or servicing). team-Reach Issue for the Reach team
Projects
None yet
Development

No branches or pull requests

5 participants