简体   繁体   English

WinUI 3 ItemsRepeater 在 ScrollViewer 中滚动并使用 UniformGridLayout 时重新排序/删除项目

[英]WinUI 3 ItemsRepeater re-orders/removes items when scrolling within a ScrollViewer and using UniformGridLayout

Wanted to post my issue I have with WinUI 3 in .NET 6 here too, maybe it's not a bug and I am doing something wrong.也想在这里发布我在 .NET 6 中遇到的 WinUI 3 问题,也许这不是错误,我做错了什么。

First of all a link to the issue on Github: https://github.com/microsoft/microsoft-ui-xaml/issues/4516首先是 Github 上的问题链接: https://github.com/microsoft/microsoft-ui-xaml/issues/4516

A sample project can be found here: https://github.com/brechtb86/microsoft-ui-xaml-issues示例项目可以在这里找到: https://github.com/brechtb86/microsoft-ui-xaml-issues

Describe the bug描述错误

When using an ItemsRepeater in a ScrollViewer on a Page within a NavigationView with a custom user control in the ItemTemplate and you start to scroll up and down, the items are re-ordered or even removed.当在 NavigationView 中的页面上的 ScrollViewer 中使用 ItemsRepeater 时,在 ItemTemplate 中使用自定义用户控件,并且您开始上下滚动时,项目会重新排序甚至删除。 The items are just simple objects and a UserControl bound to some properties of that object.这些项目只是简单的对象和绑定到 object 的某些属性的 UserControl。

Steps to reproduce the bug重现错误的步骤

Open the page "ItemsRepeater Scroll Issue (re-order/remove)".打开页面“ItemsRepeater Scroll Issue (re-order/remove)”。 Items are ordered correctly.物品订购正确。 Start scrolling up and down.开始上下滚动。 Items are re-ordered and sometimes removed.项目被重新排序,有时被删除。 Switch to another page.切换到另一个页面。 Switch back to page "ItemsRepeater Scroll Issue (re-order/remove)".切换回页面“ItemsRepeater Scroll Issue (re-order/remove)”。 Items are ordered correctly again.物品再次正确订购。 Rinse and repeat.冲洗并重复。

Expected behavior预期行为

Items should stay in the same order.项目应保持相同的顺序。

I'm using the MVVM pattern, I'm using DI to manage my viewmodels with Ninject.我正在使用 MVVM 模式,我正在使用 DI 通过 Ninject 管理我的视图模型。 The project is very simple:该项目非常简单:

ItemsRepeaterScrollIssuePage.xaml: ItemsRepeaterScrollIssuePage.xaml:

<Page
x:Class="WinUI3Issues.ItemsRepeaterScrollIssuePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3Issues"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:WinUI3Issues.Models"
xmlns:userControls="using:WinUI3Issues.UserControls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Path=ItemsRepeaterScrollIssueViewModel, Source={StaticResource ViewModelLocator}}">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Start scrolling" />
    <ScrollViewer Grid.Row="1">
        <ItemsRepeater ItemsSource="{Binding Path=DummyObjects}">
            <ItemsRepeater.Layout>
                <UniformGridLayout Orientation="Horizontal" ItemsStretch="Fill" MinColumnSpacing="24" MinRowSpacing="24"></UniformGridLayout>
            </ItemsRepeater.Layout>
            <ItemsRepeater.ItemTemplate>
                <DataTemplate x:DataType="models:DummyObject">
                    <Grid>
                        <userControls:DummyObjectControl Title="{Binding Path=Name}"></userControls:DummyObjectControl>
                    </Grid>
                </DataTemplate>
            </ItemsRepeater.ItemTemplate>
        </ItemsRepeater>
    </ScrollViewer>
  </Grid>
</Page>

DummyObjectControl:虚拟对象控件:

<UserControl
x:Class="WinUI3Issues.UserControls.DummyObjectControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3Issues.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Border Height="288" Width="192" BorderThickness="1" BorderBrush="{ThemeResource TextBoxBorderThemeBrush}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Text="{x:Bind Path=Title, Mode=OneWay}"></TextBlock>
    </Grid>
</Border>

ItemsRepeaterScrollIssueViewModel: ItemsRepeaterScrollIssueViewModel:

public class ItemsRepeaterScrollIssueViewModel : BaseViewModel
{
  public ObservableCollection<DummyObject> DummyObjects { get; set; }

  public ItemsRepeaterScrollIssueViewModel()
  {
      this.DummyObjects = new ObservableCollection<DummyObject>();

      for (var i = 1; i <= 100; i++)
      {
          this.DummyObjects.Add(new DummyObject(){ Name = $"Object_{i}"});
      }
  }
}

DummyObject.cs:虚拟对象.cs:

public class DummyObject
{
    public string Name { get; set; }
}

ViewModelLocator.cs: ViewModelLocator.cs:

public class ViewModelLocator
{
    public ItemsRepeaterScrollIssueViewModel ItemsRepeaterScrollIssueViewModel => IocKernel.Get<ItemsRepeaterScrollIssueViewModel>();
    public XamlCommandBindingIconSourceIssueViewModel XamlCommandBindingIconSourceIssueViewModel => IocKernel.Get<XamlCommandBindingIconSourceIssueViewModel>();
}

Do any of you know if I might be doing something wrong or if this is a legit bug in WinUI 3?你们中有人知道我是否做错了什么,或者这是否是 WinUI 3 中的一个合法错误?

OK, I got help on Github, the issue was that my dependency properties were not marked as static, another issue was that I didn't set the x:Bind mode to OneWay in my UserControl.好的,我在 Github 上得到了帮助,问题是我的依赖属性没有标记为 static,另一个问题是我没有在我的 UserControl 中将 x:Bind 模式设置为 OneWay。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM