WinUI 3桌面打包应用安装后ListView无法显示项的问题求助
WinUI 3桌面打包应用安装后ListView无法显示项的问题求助
各位好,我现在用Visual Studio在Windows 10(19045版本)上开发WinUI 3桌面应用,选的是“Empty App, packaged with Windows Application Packaging Project (WinUI 3 in Desktop)”模板。在Visual Studio里用Debug模式运行的时候一切正常,但打包手动安装后,ListView就是不显示内容——我已经确认ObservableCollection数据源是正确填充的,点击按钮能看到集合计数是对的,但列表就是空的。
我的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>
对应的后台代码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; } }
我的Package.appxmanifest中的依赖配置是:
<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>
我已经做了这些排查:
- 确认Persone集合在启动后已经正确填充(通过按钮点击显示的计数验证)
- 排查过x:Bind和DataTemplate的配置,没发现明显错误
- 运行时没有抛出任何异常
有没有人遇到过类似的情况?为什么打包安装后的应用里ListView不显示项,而Debug模式下正常?这会不会是WinUI 3打包应用中绑定或者x:DataType相关的已知问题?
内容来源于stack exchange
相关产品推荐
相关产品推荐

