I'm developing a WinUI 3 desktop application using the "Empty App, packaged with Windows Application Packaging Project (WinUI 3 in Desktop)" template in Visual Studio on Windows 10 (build 19045).
Everything works fine when I run the app in Debug mode from Visual Studio. However, after I create the package and install the app manually, the ListView does not appear, even though the data source (ObservableCollection) is correctly populated.
Here's my MainWindow.xaml:
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="App2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="App2">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<ListView x:Name="WorkList"
SelectionMode="Single"
ItemsSource="{x:Bind Persone}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Persona">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{x:Bind Nome, Mode=OneWay}"/>
<TextBlock Grid.Column="1"
Text="{x:Bind Cognome, Mode=OneWay}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Click="Button_Click">
Bottone a
</Button>
<TextBlock x:Name="TextBlock"/>
</StackPanel>
</Window>
Code-behind (MainWindow.xaml.cs):
public sealed partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<Persona> _persone = new();
public ObservableCollection<Persona> Persone
{
get => _persone;
set
{
if (_persone != value)
{
_persone = value;
OnPropertyChanged(nameof(Persone));
}
}
}
public MainWindow()
{
InitializeComponent();
Persone.Add(new Persona { Nome = "Mario", Cognome = "Rossi" });
Persone.Add(new Persona { Nome = "Franco", Cognome = "Rossi" });
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBlock.Text = Persone.Count.ToString();
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class Persona
{
public string Nome { get; set; }
public string Cognome { get; set; }
}
The ObservableCollection is populated correctly (verified via the button click), but the ListView is not shown after installation.
My Package.appxmanifest uses:
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>
Confirmed Persone is populated after startup
Checked if the problem is with x:Bind or DataTemplate
No runtime exceptions are thrown.
Why is the ListView not displaying after installing the packaged app, while it works fine in Debug mode from Visual Studio? Is there a known issue with binding or x:DataType in WinUI 3 packaged apps?
ItemsSource="{x:Bind Persone, Mode=OneWay}"?