MAUI Images not loading in VS 2022 17.5 - works in 17.4 (Unpackaged on Windows)

TerryH 55 Reputation points
2023-03-19T15:18:28.7133333+00:00

I noticed after upgrading Visual Studio to 17.5.1 from 17.4.4 that images were no longer showing up in my CollectionView items consistently. Some of them may show, but it seems somewhat random how many will show, if at all. I'm using a svg image file in my resources.

After rolling back to 17.4, it worked fine. I waited until 17.5.2 was released and still have the same issue. I just made some simple modifications to the MAUI sample project "MauiApp2".

I changed the .csproj project file to build to a folder (unpackaged non-MSIX):


In the .csproj file

<!-- This was added -->
<WindowsPackageType>None</WindowsPackageType>

My "launchSettings.json"

{
  "profiles": {
    "Windows Machine": {
      "commandName": "Project", <-- Changed this from "MsixPackage" to "Project"
      "nativeDebugging": false,
      "hotReloadEnabled": false
    }
  }
}

Modified the "CollectionView" in MainPage to show a label and an image button referencing the image from resources:


        <CollectionView Grid.Row="2" Grid.ColumnSpan="2"
                        ItemsSource="{Binding Items}"
                        SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="{x:Type x:String}">

                    <Grid Padding="0,5">
                        <Frame>
                            <HorizontalStackLayout>
                                <Label Text="{Binding .}"
                                       FontSize="18" />
                                <ImageButton Source="{AppThemeBinding Dark=refresh_pink.png, Light=refresh_pink.png}"
                                             HeightRequest="32"
                                             ToolTipProperties.Text="This image only shows up some of the time"/>
                            </HorizontalStackLayout>
                        </Frame>
                    </Grid>
                </DataTemplate>

            </CollectionView.ItemTemplate>

        </CollectionView>

I added a collection to the MainViewModel to bind to ItemsSource :


   public MainViewModel()
    {
        Items = new ObservableCollection<string>();
 
        AddABunchOfThings();
    }

    private void AddABunchOfThings()
    {
        Items.Add("One");
        Items.Add("Two");
        Items.Add("Three");
        Items.Add("Four");
        Items.Add("Five");
        Items.Add("Six");
        Items.Add("Seven");
        Items.Add("Eight");
        Items.Add("Nine");
        Items.Add("Ten");
    }

    [ObservableProperty]
    ObservableCollection<string> items;

That's pretty much it. Images load consistently in 17.4.4, but from 17.5.1 it consistently fails to show the images. In the screenshot below, the image displayed properly for just one of the items.
VS.2022.17.5.2_ImagesDontLoad

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,888 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,232 questions
{count} votes

1 answer

Sort by: Most helpful
  1. TerryH 55 Reputation points
    2023-07-03T17:11:56.63+00:00

    I found a work around for this issue. I took all of the XAML for the ItemTemplate and moved it into a "ContentView" of its own, define it in the resources and then reference it in the CollectionView.ItemTemplate. It seems happy this way and all the images load properly at startup. Everything else is still the same.

    <CollectionView ItemsSource="{Binding MyItems}"
    ItemTemplate="{StaticResource myItemTemplateView}"/>

    1 person found this answer helpful.
    0 comments No comments