与众不同 windows phone (34) - 8.0 新的控件: LongListSelector
作者:webabcd
介绍
与众不同 windows phone 8.0 之 新的控件
- 新的控件 - LongListSelector
示例
演示 LongListSelector 控件的应用
1、提供数据
Controls/CityInfo.txt
=澳门=aomen
=阿巴嘎=abagaqi
=阿坝=aba
=阿城=acheng
=阿尔山=aershan
=阿合奇=aheqi
=阿克苏=akesu
=阿克陶=aketao
=阿拉尔=alaer
=阿拉山口=alashankou
=阿右旗=alashanyouqi
=阿左旗=alashanzuoqi
=阿勒泰=aletai
=阿里=ali
=阿鲁旗=aluqi
=阿荣旗=arongqi
=阿图什=atushi
=阿瓦提=awati
=安达=anda
=安定=anding
=安多=anduo
=安福=anfu
=安国=anguo
=安化=anhua
=安吉=anji
=安康=ankang
Controls/CityInfo.cs
namespace Demo.Controls
{
/// <summary>
/// city 信息实体类
/// </summary>
public class CityInfo
{
/// <summary>
/// city 标识
/// </summary>
public string CityId { get; set; }
/// <summary>
/// city 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// city 拼音
/// </summary>
public string Pinyin { get; set; } /// <summary>
/// 获取城市的拼音首字母
/// </summary>
public static string GetFirstPinyinKey(CityInfo cityInfo)
{
char indexLetter = char.ToUpper(cityInfo.Pinyin[]); if (indexLetter < 'A' || indexLetter > 'Z')
{
indexLetter = '#';
} return indexLetter.ToString();
}
}
}
Controls/CityInfoInGroup.cs
using System.Collections.Generic; namespace Demo.Controls
{
/// <summary>
/// 为 LongListSelector 提供数据的实体类
/// </summary>
public class CityInfoInGroup : List<CityInfo>
{
public CityInfoInGroup(string indexLetter)
{
IndexLetter = indexLetter;
} // 某组数据的拼音首字母
public string IndexLetter { get; set; } public bool HasItems { get { return Count > ; } }
}
}
Controls/CityData.cs
using System;
using System.Collections.Generic;
using System.Windows.Resources;
using System.IO;
using System.Linq; namespace Demo.Controls
{
/// <summary>
/// 城市数据(为 LongListSelector 提供数据)
/// </summary>
public class CityData
{
// 按拼音首字母分组城市数据
private static readonly string _groupLetters = "#abcdefghijklmnopqrstuvwxyz"; private static List<CityInfoInGroup> _data;
private static List<CityInfo> _cities; /// <summary>
/// 获取全部城市数据 for LongListSelector
/// </summary>
public static List<CityInfoInGroup> GetData()
{
if (_data == null)
{
_data = new List<CityInfoInGroup>();
_cities = new List<CityInfo>(); Dictionary<string, CityInfoInGroup> groups = new Dictionary<string, CityInfoInGroup>(); foreach (char c in _groupLetters.ToUpper())
{
CityInfoInGroup group = new CityInfoInGroup(c.ToString());
_data.Add(group);
groups[c.ToString()] = group;
} StreamResourceInfo resource = App.GetResourceStream(new Uri("Controls/CityInfo.txt", UriKind.Relative));
StreamReader sr = new StreamReader(resource.Stream);
string line = sr.ReadLine();
while (line != null)
{
var ary = line.Split('=');
var cityInfo = new CityInfo { CityId = ary[], Name = ary[], Pinyin = ary[] };
_cities.Add(cityInfo);
groups[CityInfo.GetFirstPinyinKey(cityInfo)].Add(cityInfo); line = sr.ReadLine();
}
} return _data;
} /// <summary>
/// 获取包含指定关键字的城市数据 for LongListSelector
/// </summary>
public static List<CityInfoInGroup> GetData(string searchKey)
{
searchKey = searchKey.ToLower(); List<CityInfoInGroup> result = new List<CityInfoInGroup>();
List<CityInfoInGroup> data = GetData(); foreach (CityInfoInGroup ciig in data)
{
List<CityInfo> childData = ciig.Where(p => p.Name.Contains(searchKey) || p.Pinyin.Contains(searchKey)).ToList();
if (childData != null)
{
CityInfoInGroup resultCiig = new CityInfoInGroup(ciig.IndexLetter);
resultCiig.AddRange(childData); result.Add(resultCiig);
}
} return result;
}
}
}
2、使用数据
Controls/LongListSelectorDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.Controls.LongListSelectorDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True"> <phone:PhoneApplicationPage.Resources>
<!--
LongListSelector 的 GroupHeader 模板
-->
<DataTemplate x:Key="GroupHeaderTemplate">
<Grid Margin="{StaticResource PhoneTouchTargetOverhang}" Background="Transparent">
<Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" HorizontalAlignment="Left">
<TextBlock Text="{Binding IndexLetter}" Foreground="{StaticResource PhoneForegroundBrush}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</Border>
</Grid>
</DataTemplate> <!--
LongListSelector 的 Item 模板
-->
<DataTemplate x:Key="ItemTemplate">
<Grid Margin="{StaticResource PhoneTouchTargetOverhang}" Background="Transparent">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" />
<TextBlock Text="{Binding Pinyin}" Style="{StaticResource PhoneTextLargeStyle}" Margin="10 0 0 0" />
</StackPanel>
</Grid>
</DataTemplate> <phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
<phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/> <!--
JumpList(弹出的索引窗口) 的 样式
-->
<Style x:Key="JumpListStyle" TargetType="phone:LongListSelector">
<Setter Property="GridCellSize" Value="113,113"/>
<Setter Property="LayoutMode" Value="Grid" />
<Setter Property="ItemTemplate">
<Setter.Value >
<DataTemplate >
<Grid>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="113" Height="113" Margin="6">
<TextBlock Text="{Binding IndexLetter}" Foreground="{Binding Converter={StaticResource ForegroundConverter}}"
FontWeight="Bold" FontSize="48" Padding="6" VerticalAlignment="Center" />
</Border>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources> <Grid x:Name="LayoutRoot" Background="Transparent">
<phone:LongListSelector Name="longListSelector" ItemsSource="{Binding}"
IsGroupingEnabled="True" HideEmptyGroups="True" LayoutMode="List"
SelectionChanged="longListSelector_SelectionChanged"
JumpListStyle="{StaticResource JumpListStyle}"
GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}"
ItemTemplate="{StaticResource ItemTemplate}">
</phone:LongListSelector>
</Grid> </phone:PhoneApplicationPage>
Controls/LongListSelectorDemo.cs
/*
* 演示 LongListSelector 控件的应用
*/ using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Windows.Media;
using System.Diagnostics;
using System.Threading.Tasks; namespace Demo.Controls
{
public partial class LongListSelectorDemo : PhoneApplicationPage
{
public LongListSelectorDemo()
{
InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
await Task.Run(() =>
{
this.longListSelector.Dispatcher.BeginInvoke(() =>
{
// 指定 LongListSelector 的数据源
longListSelector.ItemsSource = CityData.GetData();
});
}); base.OnNavigatedTo(e);
} private void longListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = longListSelector.SelectedItem; // 滚动到指定的 item
longListSelector.ScrollTo(selectedItem);
}
}
}
OK
[源码下载]
与众不同 windows phone (34) - 8.0 新的控件: LongListSelector的更多相关文章
- 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask
[源码下载] 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirec ...
- 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile
[源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...
- 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展
[源码下载] 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展 作者:webabcd 介绍与众不同 windows ph ...
- 与众不同 windows phone (44) - 8.0 位置和地图
[源码下载] 与众不同 windows phone (44) - 8.0 位置和地图 作者:webabcd 介绍与众不同 windows phone 8.0 之 位置和地图 位置(GPS) - Loc ...
- 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复
[源码下载] 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 作者:webabcd 介绍与众不同 win ...
- 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件
[源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...
- 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议
[源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...
- 与众不同 windows phone (39) - 8.0 联系人和日历
[源码下载] 与众不同 windows phone (39) - 8.0 联系人和日历 作者:webabcd 介绍与众不同 windows phone 8.0 之 联系人和日历 自定义联系人存储的增删 ...
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
[源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...
随机推荐
- Debug Assertion Failed! Expression: _pFirstBlock == pHead
点击Abort之后,查看调用栈,发现异常在函数return时被时产生,进一步看是vector的析构函数被调用时产生,以前没开发过C++项目,没什么经验,这个错误让我很困惑,第一,我电脑上并没有f盘:第 ...
- Show Linux Package Sort By Size
ArchLinux: ~ $ pacsysclean Debian: ~ $ sudo apt-get install debian-goodies ~ $ dpigs -H
- 结合使用saiku、mondrian workbentch建立多维查询报表
1.简介 前几篇博客已经介绍了saiku.mondrian.MDX和模式文件他们之间的关系,那么如何将它们串联起来,供产品人员使用哪?下面咱们一步一步的实现 2.建立数据表 建表语句参考:http:/ ...
- [翻译]使用Swift在Xcode中创建自定义控件
使用Swift在Xcode中创建自定义控件 原文 IBDesignable and IBInspectable With IBDesignable and IBInspectable, develop ...
- 活学活用,webapi HTTPBasicAuthorize搭建小型云应用的实践
HTTP使用BASIC认证,WebAPI使用[HTTPBasicAuthorize]标记控制器就是使用了BASIC认证. BASIC认证的缺点HTTP基本认证的目标是提供简单的用户验证功能,其认证过程 ...
- 基于安卓高仿how-old.net实现人脸识别估算年龄与性别
前几段微软推出的大数据人脸识别年龄应用how-old.net在微博火了一把,它可以通过照片快速获得照片上人物的年龄,系统会对瞳孔.眼角.鼻子等27个“面部地标点"展开分析,进而得出你的“颜龄 ...
- 二十、【.Net开源】EFW框架核心类库之WebService服务
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.1:http://pan.baidu.com/s/1qWJjo3U EFW框架实例源代码下载:http://pan.baid ...
- 原生js提交表单
/********************* 表单提交 ***********************/ function ajax(options) { options = options || { ...
- 【原创】14. MYSQL++之SSQLS(原理解析)
从之前所介绍的SSQLS的介绍中我们可以感受到,SSQLS的精髓应该在sql_create_#这个宏,他所创建出来的这个结构体将会是突破的关键,所以我将会从以下顺序入手. 1. sql_create_ ...
- undefined symbol libiconv_open 完全解决方案
我在另一篇关于Ubuntu+Sendmail+Dovecot+Openwebmail 邮件服务器搭建完全解决方案文章完成后,我的邮件服务器也搭建完成了, 事实上也正在运行中, 但是有网友依据我的操作步 ...