listviewwinui-3argumentexception.net-9.0native-aot

WinUI 3.0 AOT - ListView ArgumentException for any Input List


I am using WinUI 3.0 with Net 9.0 and am trying to use Native AOT, but i always get an ArgumentException while using ListView or ItemsRepeater.

The code is working fine without AOT.

<?xml version="1.0" encoding="utf-8"?>
<UserControl
    x:Class="TestProject.UserControls.Home.AOTSourceTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestProject.UserControls.Home"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <UserControl.Resources>

        <DataTemplate x:Key="ItemTemplate" x:DataType="x:String">
            <TextBlock Text="{x:Bind}" />
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ListView
            Grid.Row="1"
            x:Name="Repeater"
            ItemTemplate="{StaticResource ItemTemplate}"
            >
        </ListView>
    </Grid>
</UserControl>
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace TestProject.UserControls.Home
{
    public sealed partial class AOTSourceTest : UserControl
    {
        public AOTSourceTest()
        {
            this.InitializeComponent();

            Repeater.Loaded += AOTSourceTest_Loaded;
        }

        private void AOTSourceTest_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
        {

            Repeater.ItemsSource = new List<string> { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
        }
    }
}
<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows10.0.26100.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>TestProject</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <PublishAot>true</PublishAot>
    <DisableRuntimeMarshalling>false</DisableRuntimeMarshalling>
    <PublishTrimmed>false</PublishTrimmed>
    <EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
System.ArgumentException: Value does not fall within the expected range.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IItemsControlMethods.set_ItemsSource(IObjectReference _obj, Object value)
   at Microsoft.UI.Xaml.Controls.ItemsControl.set_ItemsSource(Object value)
   at TestProject.UserControls.Home.AOTSourceTest.AOTSourceTest_Loaded(Object sender, RoutedEventArgs e)
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

I tried to use ItemsRepeater, but it also has an ArgumentException.
Argument 'source' is not a supported vector.

Tried ObservableList and string array, but it did not help.


Solution

  • There are at least two ways to fix this issue in the .csproj (you can use one or the other or both):

    1. add this in <PropertyGroup> XML element

      <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
      
    2. add an explicit package reference to the C#/WinRT package (current version is 2.2.0) to an <ItemGroup> XML element

      <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
      

    I'm not sure why you don't get a compilation warning because AOT compilation usually requires unsafe blocks to be enabled. It may be a bug, seems similar to this: https://github.com/microsoft/CsWinRT/issues/1891