0

When I populate a ListView (or ItemsView) with the ItemsSource being a List<ApplicationModel.Package> object, a crash will sometimes happen when loading the control.

The crash chance goes up by a lot if I have an Image that uses the Package.Logo as a source.

When I triggered the control, I expected it to show all of the installed apps, their icons, etc. Which it did successfully, until I tried to scroll... which is when I was hit with the crash.

Using this code:

<ContentDialog
    x:Class="Program.AllAppsList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TITLE"
    Style="{ThemeResource DefaultContentDialogStyle}">
    <Grid>
        <ListView ItemsSource="{x:Bind Packages}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <!-- High crash chance with this uncommented -->
                        <Image Width="64" Height="64" Margin="5">
                            <Image.Source>
                                <BitmapImage UriSource="{Binding Logo, TargetNullValue='ms-appx:///Assets/fallback.png', FallbackValue='ms-appx:///Assets/fallback.png'}"  />
                            </Image.Source>
                        </Image>
                        <StackPanel Orientation="Vertical" Margin="5">
                            <TextBlock Text="{Binding DisplayName, TargetNullValue='Unknown', FallbackValue='Unknown'}" FontWeight="Bold" />
                            <TextBlock Text="{Binding PublisherDisplayName, TargetNullValue='Unknown', FallbackValue='Unknown'}" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</ContentDialog>
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using Windows.ApplicationModel;
using Windows.Management.Deployment;

namespace Program
{
    public sealed partial class AllAppsList : ContentDialog
    {
        public List<Package> Packages { get; set; } = new List<Package>();

        public AllAppsList()
        {
            Packages = GetAllInstalledPackages();
            this.DataContext = this;
            this.InitializeComponent();
        }

        public static List<Package> GetAllInstalledPackages()
        {
            var pm = new PackageManager();
            return pm.FindPackagesForUser(WindowsIdentity.GetCurrent().User?.Value).ToList();
        }

        private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
        }

        private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
        }
    }
}
1
  • 3
    Catch the Exception using an "Unhandled Exception Handler", and then you can see better what it is and what is triggering it in more detail. Commented Sep 27, 2024 at 7:05

1 Answer 1

0

Try the following:

public AllAppsList()
{
    var packages = GetAllInstalledPackages();

    // Magic block of code. Not sure why it works.
    var list = new List<string>();
    foreach (var package in packages)
    {
        list.Add(package.DisplayName);
    }

    Packages = packages;
    this.DataContext = this;
    this.InitializeComponent();
}

Also, since it seems that some packages, espacially the ones installed in DevelopmentMode don't have a proper installed location, try filter them first:

public static List<Package> GetAllInstalledPackages()
{
    var pm = new PackageManager();
    return pm.FindPackagesForUser(WindowsIdentity.GetCurrent().User?.Value)
                 .Where(package => package.IsDevelopmentMode is false)
                 .ToList();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.