上篇UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一) 讲到该控件的需要和设计过程。

这篇讲讲开发过程中一些重要问题解决。

1.支持ISupportIncrementalLoading,实现HasMoreItems属性和LoadMoreItemsAsync方法

因为我们上篇里面讲过,需要把源数据分成一个一个的Group作为GirdView的源,

所以LoadMoreItemsAsync方法里面我做了以下的实现:

        public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
IAsyncOperation<LoadMoreItemsResult> result = rowAdapter.LoadMoreItemsAsync(count);
if (rowAdapter.Count > )
{
for (int i = this.Count; i < rowAdapter.Count; i++)
{
if (rowAdapter.SourceList.Count / rowAdapter.rowItemsCount > i)
{
var item = this.ElementAtOrDefault(i);
if (item == null)
{
this.Insert(i, rowAdapter[i]);
}
}
}
} return result;
}

应该还是比较清楚的,当源数据里面的个数超过了RowItemsCount的时候,我们才插入Item。

也就是说。当满15个Item的时候我们才插入第1个Row Item,当满30个Item的时候我们插入第2个Row Item......

可能有人会说,如果源数据不是15个整数,那怎么办呢??嗯,我也遇到了这个问题,当然,HasMoreItems属性也需要作相应的实现

        public bool HasMoreItems
{
get
{
var hasMoreItems = rowAdapter.HasMoreItems; if (!hasMoreItems)
{
if (rowAdapter.Count > && this.Count < rowAdapter.Count)
{
for (int i = this.Count; i < rowAdapter.Count; i++)
{
//sometime it will miss some indexs in LoadMoreItemsAsync method,
//if hasMoreItems is false, that means not more items,
//so at that monment we should add the missed items.
//if (rowAdapter.SourceList.Count / rowAdapter.rowItemsCount <= i)
{
var item = this.ElementAtOrDefault(i);
if (item == null)
{
this.Insert(i, rowAdapter[i]);
}
}
}
}
}
return hasMoreItems;
}
}

当HasMoreItems为false的时候,就是说这个源不会有更多的数据了,所以这时候我们应该把剩余的Item都加入到下一个Row Item里面去。

2.当Window size 改变的时候,实现不同的可变大小结构。

public class ResizeableItems : List<ResizeableItem>

每一种结构,我用一个ResizeableItem来表示。

比如window 最小的时候这种结构。

                list = new List<Resizable>();
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = }); var c1 = new ResizeableItem() { Columns = , Items = list, Min = windowMinwidth + +, Max = windowMinwidth + rangwidth * };
_resizeableItems.Add(c1);

设置GirdView里面的每个Item的宽高比2:1,设置ResizeableItem的最小和最大值,这个意思就是当window 到达MIn和Max的这个区间的时候就使用这个结构。

然后也设置出其它的结构样式。(PS:我这里还没找到Get 最小window 宽度的办法,好像只能设置,如果有办法的朋友请留言一下)

                 _resizeableItems = new ResizeableItems();

                //ApplicationView.GetForCurrentView().SetPreferredMinSize(new Windows.Foundation.Size(200, 200));

                double windowMinwidth = ;
double windowMaxwidth = DeviceInfo.DeviceScreenSize.Width;
double rangwidth = (windowMaxwidth - windowMinwidth) / 4.0; #region 4
var list = new List<Resizable>();
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = }); var c4 = new ResizeableItem() { Columns = , Items = list, Min = windowMinwidth + rangwidth * + , Max = double.PositiveInfinity };
_resizeableItems.Add(c4);
#endregion #region 3
list = new List<Resizable>();
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = }); var c3 = new ResizeableItem() { Columns = , Items = list, Min = windowMinwidth + rangwidth * + , Max = windowMinwidth + rangwidth * };
_resizeableItems.Add(c3);
#endregion #region 2
list = new List<Resizable>();
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = }); var c2 = new ResizeableItem() { Columns = , Items = list, Min = windowMinwidth + rangwidth * + , Max = windowMinwidth + rangwidth * };
_resizeableItems.Add(c2);
#endregion #region 1
list = new List<Resizable>();
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = });
list.Add(new Resizable() { Width = , Height = }); var c1 = new ResizeableItem() { Columns = , Items = list, Min = windowMinwidth + +, Max = windowMinwidth + rangwidth * };
_resizeableItems.Add(c1);
#endregion

这样我们就设置好了,到达多少size的时候使用什么结构模板了。。

最后我们只需要在控件的MeasureOverrid中去设置,GridView的Item的样式就好了.

        protected override Size MeasureOverride(Size availableSize)
{
if (!PlatformIndependent.IsWindowsPhoneDevice)
{
OnMeasureOverride(availableSize);
}
return base.MeasureOverride(availableSize);
} private void OnMeasureOverride(Size availableSize)
{
if (ItemsSource != null && ItemsSource is IResizeableItems && availableSize != Size.Empty)
{
var resizeableItem = ResizeableItems.GetItem(availableSize.Width); if (resizeableItem != null)
{
resizeableItem.ItemWidth = (int)(availableSize.Width / resizeableItem.Columns - ); foreach (var item in this.Items)
{
var gridviewItem = this.ContainerFromItem(item) as ListViewItem;
//not null, it's in viewport, so it need to update.
if (gridviewItem != null && gridviewItem.ContentTemplateRoot != null)
{
var gridview = gridviewItem.ContentTemplateRoot as VariableSizedGridView;
gridview.ResizeableItem = null;
gridview.ResizeableItem = resizeableItem; }
}
} }
}

3.ListViewItem默认模板的修改
在使用这个的时候因为其实是个ListView,所以当你点击到GridView外面的时候就点击到了ListLViewItem上面,会有些你不想要的效果,比如PointerDown。

第一次用VS拿到ListViewItem的模板的时候发现没地方可能修改这个动画呢???

再查了下MSDN,发现原来,ListViewItem是有2套模板的,

Default style

When the ListView's ItemsPanel is an ItemsStackPanel (the default) or ItemsWrapGrid, this template is used to show the data items. This template uses aListViewItemPresenter instead of a UIElement tree to improve grid performance.

这个模板跟我从VS里面拿到的是一样的。

<!-- Default style for Windows.UI.Xaml.Controls.ListViewItem -->
<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Margin" Value="0,0,18,2"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
ContentTransitions="{TemplateBinding ContentTransitions}"
Padding="{TemplateBinding Padding}"
SelectionCheckMarkVisualEnabled="True"
CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}"
CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"
CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
FocusBorderBrush="{ThemeResource ListViewItemFocusBorderThemeBrush}"
PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
PointerOverBackground="{ThemeResource ListViewItemPointerOverBackgroundThemeBrush}"
SelectedBorderThickness="{ThemeResource ListViewItemCompactSelectedBorderThemeThickness}"
SelectedBackground="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}"
SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}"
SelectedPointerOverBackground="{ThemeResource ListViewItemSelectedPointerOverBackgroundThemeBrush}"
SelectedPointerOverBorderBrush="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
PointerOverBackgroundMargin=""
ContentMargin="" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

When the ListView's ItemsPanel is not an ItemsStackPanel (the default) or ItemsWrapGrid, this template is used to show the data items. This template uses aUIElement tree and visual states instead of a ListViewItemPresenter.

这个模板我就不贴了,比较多,MSDN地址

总结:

虽然说这个控件已经满足了Boss的需求,但是我还是觉得有一些需要改进的。

1.UI Virtualized

如果说GridView里面的Item个数被用户设置的很多,这个必定是很占用内存的。

实际Debug,也发现,Live Visual Tree里面有3个ListViewItem.

感觉内存中不需要这么多个ListViewItem 来循环回收利用,我的猜想是微软做了上下各一个Item的缓存来提高Scrolling的流畅。

但对于这个控件来说,内存里面的UI Item 会有3*15个,因为我们知道VariableSizedWrapGrid是不支持UI虚拟化的。

2.没有对Insert,Delete进行处理。

这个控件还有一个限制,就是源是一个固定个数或者是ISupportIncrementalLoading,如果在过程中add,insert,delete的话,暂时没有进行处理。

只对Reset这种情况进行了处理。

        private void ObservableRowAapter_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Remove:
break;
case NotifyCollectionChangedAction.Replace:
break;
case NotifyCollectionChangedAction.Reset:
if (this.Count>)
{
this.Clear();
}
break;
default:
break;
}
}

希望有好想法的童鞋能留言,大家讨论共同进步。。

为了部落!

开源有益,GitHub源代码地址

UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)的更多相关文章

  1. UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一)

    Boss的需要时这样的,Item是可变大小的,同时根据不同的Window size,来确定Item的结构和大小Window 小的时候是 大的时候是这样的: 当然这size变化的过程中也允许其他结构,我 ...

  2. 算法Sedgewick第四版-第1章基础-008一用数组实现栈(泛型、可变大小)

    package algorithms.ADT; /*************************************************************************** ...

  3. Xamarin.Forms 现已开启对 UWP 的支持

    Xamarin.Forms 现已升级到 2.0.0.6482 , 正式开启了对 UWP 的支持. 要创建 UWP 项目, 必须是 VS2015, WIN8.1 下也可以, 但是只有 Windows 1 ...

  4. 关于理解《C++ 对象模型》中:把单一元素的数组放在末尾,struct可以拥有可变大小的数组

    这一章在第19页,写的好深奥,我竟然没看明白在说什么--之后再看了几遍,终于明白了. 原文: C程序员的巧计有时候却成为c++程序员的陷阱.例如把单一元素的数组放在一个struct的末尾,于是每个st ...

  5. 64位平台支持大于2 GB大小的数组

    64位平台支持大于2 GB大小的数组 64位平台.NET Framework数组限制不能超过2GB大小.这种限制对于需要使用到大型矩阵和向量计算的工作人员来说,是一个非常大问题. 无论RAM容量有多大 ...

  6. 移动设备应用程序中支持多个屏幕大小和 DPI 值

    支持多个屏幕大小和 DPI 值的指导原则 要部署独立于平台的应用程序,应了解不同的输出设备.设备可以具有不同的屏幕大小或分辨率以及不同的 DPI 值或密度. Flex 工程师 Jason SJ 在他的 ...

  7. 使struct对象拥有可变大小的数组——(C++深度探索)

    首先摘录<Inside The C++ Object Model>中的一段话: 把单一元素的数组放在一个struct的尾端,于是每个 struct objects 可以拥有可变大小的数组: ...

  8. 0x04 Python logger 支持多进程日志按大小分割

    目录 支持多进程日志按大小分割 多进程日志大小分割handler配置实例 支持多进程日志按大小分割 由于python内置模块logging.handlers.RotatingFileHandler是不 ...

  9. 如何查看自己的电脑 CPU 是否支持硬件虚拟化

    引言 在你安装各种虚拟机之前,应该先测试一下自己的电脑 CPU 是否支持硬件虚拟化. 如果你的电脑比较老旧,可能不支持硬件虚拟化,那么将无法安装虚拟机软件. 如何查看自己 CPU 是否支持硬件虚拟化 ...

随机推荐

  1. coreseek增量索引

    1.在多数情况下,因为Coreseek索引速度高达10MB/s,所以只需要创建一个索引源即可满足需求,但是在数据量随时激增的大型应用中(如SNS.评论系统等),单一的索引源将会给indexer造成极大 ...

  2. linux下安装postgresql

    环境:Linux localhost.localdomain 2.6.32-431 GNU/Linux x86_64 Postgresql版本:postgresql.9.5.3 添加开启自启设置:ht ...

  3. NoSQL指南

    一.数据库发展 1.早期出现的数据库包括平面文件数据管理系统.分层数据管理系统和网络数据管理系统,分别对应的数据结构是线性表.树和图. 平面文件数据管理系统是使用磁带对数据进行顺序存储的,带来的问题不 ...

  4. Oracle函数组的使用

    --1.组函数--COUNT():用来统计记录的条数 如果没有记录,返回 0--COUNT函数可以根据一列或多列进行计算,没有排重功能--统计EMP表一共有多少条记录select count(empn ...

  5. R可视化lend_club 全球最大的P2P平台数据75W条

    lend_club 全球最大的P2P平台2007~2012年贷款数据百度云下载. 此文章基于R语言做简单分析. rm(list=ls()) #清除变量 gc() #释放内存 step1 考虑到后续分析 ...

  6. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  7. php打印中文乱码

    php文档的文本格式都设置成 utf-8 格式 在代码中添加 header("content-type:text/html; charset=utf-8");

  8. Notepad++源码编译及其分析

    Notepad++是一个小巧精悍的编辑器,其使用方法我就不多说了,由于notepad++是使用c++封装的windows句柄以及api来实现的,因此对于其源码的研究有助于学习如何封装自己简单的库(当然 ...

  9. LPC1768\1769之中断优先级与中断优先级组

    一.背景 USB在持续通信几十万次后,会出现USB IN中断丢失几次的情况,分析是中断优先级不够高,导 致USB中断在排队,然而排队还未完成,又有新的USB中断发生,致使其中断丢失.LPC1769的所 ...

  10. 使用jstack分析cpu消耗过高的问题

    我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2    18   ...