windows NET8 + MAUI:当窗口调整大小时,ListView不调整大小

5uzkadbs  于 7个月前  发布在  Windows
关注(0)|答案(1)|浏览(105)

我正在用MAUI和NET8创建一个应用程序。我在Windows上遇到了一个有趣的问题。请考虑下面的屏幕截图


的数据
基本上,这是一个ListView

<ListView x:Name="listDictionaries" 
            x:DataType="vm:DictionaryListViewModel"
            ItemsSource="{Binding Dictionaries}" 
            ItemSelected="listDictionaries_ItemSelected"
            SelectionMode="Single" HasUnevenRows="True" 
            CachingStrategy="RecycleElement" 
            HorizontalOptions="Fill" VerticalOptions="Fill"
            IsRefreshing="{Binding IsRefreshing}"
            RefreshCommand="{Binding RefreshCommand}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="dt:Dictionary">
            <ViewCell>
                <!-- UI stuff -->
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

字符串
它正在工作。如果我调整窗口大小,则不可能以任何方式滚动ListView。此外,我在 MauiProgram.cs 中添加了这些行:

.ConfigureLifecycleEvents(events =>
{
#if WINDOWS
        events.AddWindows(windows => windows
                .OnWindowCreated(window =>
                {
                    window.SizeChanged += OnSizeChanged;
                }));
#endif
})


在文件的底部

#if WINDOWS
static void OnSizeChanged(object sender, 
    Microsoft.UI.Xaml.WindowSizeChangedEventArgs args)
{
    ILifecycleEventService service = MauiWinUIApplication.Current.Services.GetRequiredService<ILifecycleEventService>();
    service.InvokeEvents(nameof(Microsoft.UI.Xaml.Window.SizeChanged));
}
#endif


但它并不起作用。这里有一个活生生的例子,



我该怎么补救?

14ifxucb

14ifxucb1#

在这里分享两个解决方案。

选项1,使用**VerticalOptions=“FillAndExpand”**代替VerticalOptions=“Fill”

<ListView x:Name="listDictionaries" 
       ...
        HorizontalOptions="FillAndExpand" 
        VerticalOptions="FillAndExpand"
        ...
        >

字符串

Option2,使用Grid(而不是StackLayout)作为外部容器,

<Grid ...>
    <ListView 
         HorizontalOptions="Fill" 
         VerticalOptions="Fill">
     ...
</Grid>


以上两种方法都可以使ListView滚动。
希望对你有帮助。

相关问题