0

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?

2
  • Your ListView is probably collapsed; it has no "Height". A given Height; or inside a "star" Grid; or a StackPanel with a given Height; etc. would (probably) work better. StackPanels / ListViews tend to collapse (horizontally and vertically) when you don't account for when they're loaded relative to their containers. Commented Jun 16 at 18:52
  • Does it work on Release mode? Have you tried ItemsSource="{x:Bind Persone, Mode=OneWay}"? Commented Jun 17 at 0:42

1 Answer 1

0

ist's normal that it doesn't display, it's because you are putting you're ListView inside a StackPanel with no given Height. try putting it in a Grid instead.

<?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">

    <Grid  HorizontalAlignment="Center" VerticalAlignment="Center">
      <Grid.RowDefinitions> 
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
      <Grid.RowDefinitions> 
        <ListView x:Name="WorkList" Grid.Row="0"
              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" Grid.Row="1">
            Bottone a
        </Button>
        <TextBlock x:Name="TextBlock" Grid.Row="2"/>
    </Grid>
</Window>
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.