UWP开发入门(十)——通过继承来扩展ListView
本篇之所以起这样一个名字,是因为重点并非如何自定义控件,不涉及创建CustomControl和UserControl使用的Template和XAML概念。而是通过继承的方法来扩展一个现有的类,在继承的子类中增加属性和扩展行为。
我们在《UWP开发入门(七)——下拉刷新》中提到过嵌套ScrollViewer的实现思路,本篇我们对ListView的第一个扩展行为,即是摒弃嵌套的做法,而是通过访问ListView内部的ScrollViewer控件,来监听ViewChanged事件。
访问ListView内部的ScrollViewer,必定离不开VisualTreeHelper类中的以下两个方法:
public static DependencyObject GetChild(DependencyObject reference, System.Int32 childIndex); public static System.Int32 GetChildrenCount(DependencyObject reference);
可以将这两个方法进一步组合得到:
static T FindFirstChild<T>(FrameworkElement element) where T : FrameworkElement
{
int childrenCount = VisualTreeHelper.GetChildrenCount(element);
var children = new FrameworkElement[childrenCount]; for (int i = ; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;
children[i] = child;
if (child is T)
return (T)child;
} for (int i = ; i < childrenCount; i++)
if (children[i] != null)
{
var subChild = FindFirstChild<T>(children[i]);
if (subChild != null)
return subChild;
} return null;
}
该方法通过递归来遍历FrameworkElement内部的元素,并返回第一个符合类型的元素。ListViewEx的第一个扩展如下:
public class ListViewEx : ListView, INotifyPropertyChanged
{
private ScrollViewer _scrollViewer; public event EventHandler LoadHistoryEvent;
public event PropertyChangedEventHandler PropertyChanged; protected void OnProperyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
} public ListViewEx()
{
this.Loaded += ListViewEx_Loaded;
this.Unloaded += ListViewEx_Unloaded;
} private void ListViewEx_Unloaded(object sender, RoutedEventArgs e)
{
this.Unloaded -= ListViewEx_Unloaded;
if (_scrollViewer != null)
{
_scrollViewer.ViewChanged -= Sv_ViewChanged;
}
} private void ListViewEx_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
this.Loaded -= ListViewEx_Loaded;
_scrollViewer = FindFirstChild<ScrollViewer>(this);
if (_scrollViewer != null)
{
_scrollViewer.ViewChanged += Sv_ViewChanged;
}
} private async void Sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (e.IsIntermediate == false && _scrollViewer.VerticalOffset < )
{
_scrollViewer.ChangeView(null, , null);
await Task.Delay();
LoadHistoryEvent?.Invoke(this, EventArgs.Empty);
}
}
嗯嗯,可以看到优雅的 -= event和恰到好处的null check,啊啊!忍不住想点个赞!在ViewChanged事件监测到滚动条到达顶部后,果断触发ListViewEx内定义的LoadHistoryEvent来通知更新数据。
本篇如果仅增加一个LoadHistoryEvent,又会被人非议是在补完前篇,一篇拆成两篇写……那好吧,我们再给ListViewEx增加第二个扩展GoBottomVisiblity属性,顾名思义即是ListViewEx向上滚动几屏以后,显示一个GoBottom的按钮。
在ListViewEx的类中,首先定义属性GoBottomVisibility,然后同样是在ViewChanged事件中,计算是否显示GoBottom按钮。
public Visibility GoBottomVisiblity
{
get { return _goBottomVisiblity; }
set
{
_goBottomVisiblity = value;
this.OnProperyChanged();
}
}
private void Sv_ViewChanged2(object sender, ScrollViewerViewChangedEventArgs e)
{
if (e.IsIntermediate == false)
{
CheckGoBottomVisibility();
}
} private void CheckGoBottomVisibility()
{
if (_scrollViewer.VerticalOffset + _scrollViewer.ViewportHeight < _scrollViewer.ScrollableHeight)
{
GoBottomVisiblity = Visibility.Visible;
}
else
{
GoBottomVisiblity = Visibility.Collapsed;
}
}
代码没法全部贴上来,一个是太长了显得啰嗦,二是会被管理员说没内涵从首页删掉……
大体上本篇就是给ListView扩展了LoadHistoryEvent事件和GoBottomVisibility属性。最后说说怎么用,XAML里使用ListViewEx代替默认的ListView,会发现多出一个LoadHistoryEvent,挂上加载数据的事件就OK了。然后在列表的下部画一个三角箭头,Visibility绑定到ListViewEx的GoBottomVisibility属性上就收工了。

<Grid>
<local:ListViewEx x:Name="listViewEx" ItemsSource="{x:Bind Items}" LoadHistoryEvent="ListViewEx_LoadHistoryEvent"></local:ListViewEx>
<Grid Margin="10" VerticalAlignment="Bottom" HorizontalAlignment="Center"
Tapped="Grid_Tapped" Visibility="{x:Bind listViewEx.GoBottomVisiblity,Mode=OneWay}">
<Ellipse Fill="LightGray" Width="30" Height="30"></Ellipse>
<Polyline Stroke="White" Points="10,10 15,20 20,10"></Polyline>
</Grid>
</Grid>
完整代码放在GayHub上,地址:https://github.com/manupstairs/UWPSamples
非常感谢各位捧场,能够点开页面看到这里,拜谢了!Orz
UWP开发入门(十)——通过继承来扩展ListView的更多相关文章
- UWP开发入门(十六)——常见的内存泄漏的原因
本篇借鉴了同事翔哥的劳动成果,在巨人的肩膀上把稿子又念了一遍. 内存泄漏的概念我这里就不说了,之前<UWP开发入门(十三)——用Diagnostic Tool检查内存泄漏>中提到过,即使有 ...
- UWP开发入门(四)——自定义CommandBar
各位好,再次回到UWP开发入门系列,刚回归可能有些不适应,所以今天我们讲个简单的,自定义CommandBar,说通俗点就是自定义类似AppBarButton的东西,然后扔到CommandBar中使用. ...
- UWP开发入门系列笔记之(一):UWP初览
标签: 随着微软Build2015带来的好消息,Win10正式版发布的日子已经离我们越来越近了,我们也终于欣喜地看到:一个统一的Windows平台对于开发人员来说充满了吸引力,这局棋下的好大的说--于 ...
- UWP开发入门(25)——通过Radio控制Bluetooth, WiFi
回顾写了许久的UWP开发入门,竟然没有讲过通过Windows.Devices.Radios.Radio来控制Bluetooth和WiFi等功能的开关.也许是因为相关的API设计的简单好用,以至于被我给 ...
- UWP开发入门(十一)——Attached Property的简单应用
UWP中的Attached Property即附加属性,在实际开发中是很常见的,比如Grid.Row: <Grid Background="{ThemeResource Applica ...
- UWP开发入门(七)——下拉刷新
本篇意在给这几天Win10 Mobile负面新闻不断的某软洗地,想要证明实现一个简单的下拉刷新并不困难.UWP开发更大的困难在于懒惰,缺乏学习的意愿.而不是“某软连下拉刷新控件都没有”这样的想法. 之 ...
- UWP开发入门(一)——SplitView
接下来会写一个UWP(Universal Windows Platform)开发入门的系列,自己学习到哪里,有什么心得总结,就会写到哪里.本篇对适用于顶层导航的SplitView控件展开讨论. 首先S ...
- UWP开发入门(二十三)——WebView
本篇讨论在UWP开发中使用WebView控件时常见的问题,以及一些小技巧. WebView是实际开发中常用的控件,很多大家抱怨的套网页的应用都是通过WebView来实现的.这里要澄清一个问题,套网页的 ...
- UWP开发入门(二十一)——保持Ui线程处于响应状态
GUI的程序有时候会因为等待一个耗时操作完成,导致界面卡死.本篇我们就UWP开发中可能遇到的情况,来讨论如何优化处理. 假设当前存在点击按钮跳转页面的操作,通过按钮打开的新页面,在初始化过程中存在一些 ...
随机推荐
- Meet Solr
you should have a solid understanding of Solr's query and indexing capabilities, including how to pe ...
- 使用MVC实现登录功能
首先,从底层开始即Models: (1)通用数据访问类(封装数据访问类方法):SqlHelper类 使用命名空间:using System.Data; using System.Data.SqlCli ...
- 当前触发事件的两种方式(onclick) 和 ('id') 获取
1. <input type='text' onclick = 'Clickon(this)'> <script> function Clickon(num){ num.sty ...
- Luajit-2.1.0-beta1的发布和生成arm64用bytecode的解脱
前情提要:由于苹果要求2015年2月1日上架的新app必须支持64位的arm64,旧的app也得在6月1日支持64位,来源.于是unity3d弄出了il2cpp这种花式的玩法来进行64位支持,而对于当 ...
- 牛X的FieldBlur
[牛X的FieldBlur] Use Field Blur to build a gradient of blurs, by defining multiple blur points with di ...
- 265. Paint House II 房子涂色K种选择的版本
[抄题]: There are a row of n houses, each house can be painted with one of the k colors. The cost of p ...
- Mysql索引会失效的几种情况分析(转)
出处:http://www.jb51.net/article/50649.htm 索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这 ...
- 使用WCF实现消息推送
1.协议 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- shell 用环境变量的值修改properties文件
假设有如下属性文件 demo.properties user.name=test user.password=123456 ............................... 需求:先需要 ...
- Hello_Depth_Perception 任务二:Project Tango采集深度感知数据
Java API Depth Perception Tutorial深度感知教程 Configuration 配置信息 In order to use depth perception, your T ...