程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 UWP 中重新排序 ListView 项目会弄乱内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 UWP 中重新排序 ListView 项目会弄乱内容?

开发过程中遇到在 UWP 中重新排序 ListView 项目会弄乱内容的问题如何解决?下面主要结合日常开发的经验,给出你关于在 UWP 中重新排序 ListView 项目会弄乱内容的解决方法建议,希望对你解决在 UWP 中重新排序 ListView 项目会弄乱内容有所启发或帮助;

我正在使用 ListVIEws 在 UWP 中制作看板列表。如下图所示,将项目重新排序几次会导致其中一项或多项内容出错。 进一步重新排序将使内容来回正确和错误,并且在重新加载页面时一切都恢复正常,这意味着没有数据更改,而只是图像控件显示错误的图像。 (它也可能发生在任何其他控件上)

作为参,图像是我在 Image 控件的 Loaded 事件中加载的本地文件,ListVIEw 只是将 CanReorderItems 和 AllowDrop 设置为 true。

在 UWP 中重新排序 ListView 项目会弄乱内容

这是 XAML 的外观

 <ListVIEw x:name="LVIEw"  MinWIDth="240" GrID.Row="1" Itemssource="{x:Bind List.Tasks}"  ReorderMode="Enabled" CanReorderItems="True" AllowDrop="True" CanDragItems="True" SELEctionMode="None"  IsItemClickEnabled="True" ScrollVIEwer.VerticalScrollbarVisibility="HIDden" ScrollVIEwer.VerticalScrollmode="Enabled" ScrollVIEwer.IsverticalRailEnabled="True" ItemClick="LVIEw_ItemClick">
        <ListVIEw.ItemContainerStyle>
          ...
        </ListVIEw.ItemContainerStyle>

        <ListVIEw.ItemTemplate>
            <DataTemplate x:DataType="mongo:Task">
                <GrID padding="12 0" >
                    <GrID.RowDeFinitions>
                        ...
                    </GrID.RowDeFinitions>


                    <border CornerRadius="4 4 0 0" margin="-12 0" >
                        <Image x:name="Cover" MaxHeight="160" Stretch="UniformToFill" Tag="{x:Bind Attachments}" Loaded="Image_Loaded" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                    </border>

...

这里是 Loaded 事件

private async voID Image_Loaded(object sender,RoutedEventArgs E)
{
    var img = sender as Image;
    
    if (img.source is object) return;

    var attachments = img.Tag as ObservableCollection<TaskAttachment>;
    if (attachments is null) return;
    var cover = attachments.Where(_a => _a.is_cover).FirstOrDefault();

    if (cover is object && cover.type == "image")
    {
        var path = BrandBoxSetTings.Instance.server_path + "projects\\" + cover.path;
        Output.Writeline(path);
        var file = await Storagefile.GetfileFromPathAsync(path);
        
        using (IRandomAccessstream fileStream = await file.openAsync(windows.Storage.fileAccessMode.Read))
        {
            // Set the image source to the SELEcted bitmap
            BitmAPImage bitmAPImage = new BitmAPImage();

            await bitmAPImage.SetsourceAsync(fileStream);
            img.source = bitmAPImage;
        }
    }
}

编辑:值得注意的是,即使其中一张卡片最初没有图像,重新排序也会使其具有图像。

任何帮助将不胜感激

解决方法

好的,所以我尝试将 ItemsPanel 更改为 StackPanel,现在似乎可以正常工作了。

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizationStackPanel/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

编辑: 它似乎也可以通过将面板设置为 VirtualizationStackPanel

大佬总结

以上是大佬教程为你收集整理的在 UWP 中重新排序 ListView 项目会弄乱内容全部内容,希望文章能够帮你解决在 UWP 中重新排序 ListView 项目会弄乱内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。