【WP8】LoopingSelector
WP8的WindowsPhoneToolkit工具包中有一个 LoopingSelector
可以想选择日期或时间一样进行选择
1、首先当然是引用WindowsPhoneToolkit
在Nuget控制台:
PM> Install-Package WPtoolkit
2、LoopingSelector 的数据源是 ILoopingSelectorDataSource类型的,我们先实现两个类继承该接口
public abstract class LoopingDataSourceBase : ILoopingSelectorDataSource
{
#region ILoopingSelectorDataSource Members public abstract object GetNext(object relativeTo); public abstract object GetPrevious(object relativeTo); private object _selectedItem;
public object SelectedItem
{
get { return _selectedItem; }
set
{
if (!Equals(_selectedItem, value))
{
object previousSelectedItem = _selectedItem;
_selectedItem = value; OnSelectionChanged(previousSelectedItem, _selectedItem);
}
}
} public event EventHandler<SelectionChangedEventArgs> SelectionChanged; protected virtual void OnSelectionChanged(object oldSelectedItem, object newSelectedItem)
{
var handler = SelectionChanged;
if (handler != null)
{
handler(this, new SelectionChangedEventArgs(new[] {oldSelectedItem}, new[] {newSelectedItem}));
}
} #endregion
}
LoopingDataSourceBase 抽象类
public class ListLoopingDataSource<T> : LoopingDataSourceBase
{
private IComparer<T> comparer;
private LinkedList<T> linkedList;
private NodeComparer nodeComparer;
private List<LinkedListNode<T>> sortedList; public IEnumerable<T> Items
{
get { return linkedList; }
set
{
SetItemCollection(value);
}
} public IComparer<T> Comparer
{
get { return comparer; }
set { comparer = value; }
} private void SetItemCollection(IEnumerable<T> collection)
{
linkedList = new LinkedList<T>(collection); sortedList = new List<LinkedListNode<T>>(linkedList.Count); LinkedListNode<T> currentNode = linkedList.First;
while (currentNode != null)
{
sortedList.Add(currentNode);
currentNode = currentNode.Next;
} IComparer<T> comparer = this.comparer;
if (comparer == null)
{
if (typeof (IComparable<T>).IsAssignableFrom(typeof (T)))
{
comparer = Comparer<T>.Default;
}
else
{
throw new InvalidOperationException(
"There is no default comparer for this type of item. You must set one.");
}
} nodeComparer = new NodeComparer(comparer);
sortedList.Sort(nodeComparer);
} public override object GetNext(object relativeTo)
{
int index = sortedList.BinarySearch(new LinkedListNode<T>((T) relativeTo), nodeComparer);
if (index < )
{
return default(T);
} LinkedListNode<T> node = sortedList[index].Next;
if (node == null)
{
node = linkedList.First;
}
return node.Value;
} public override object GetPrevious(object relativeTo)
{
int index = sortedList.BinarySearch(new LinkedListNode<T>((T) relativeTo), nodeComparer);
if (index < )
{
return default(T);
}
LinkedListNode<T> node = sortedList[index].Previous;
if (node == null)
{
node = linkedList.Last;
}
return node.Value;
} private class NodeComparer : IComparer<LinkedListNode<T>>
{
private readonly IComparer<T> comparer; public NodeComparer(IComparer<T> comparer)
{
this.comparer = comparer;
} #region IComparer<LinkedListNode<T>> Members public int Compare(LinkedListNode<T> x, LinkedListNode<T> y)
{
return comparer.Compare(x.Value, y.Value);
} #endregion
}
}
ListLoopingDataSource
注意,数据源如果是对象,必须实现IComparer<T>接口,否则会抛出异常,当然,也可以重写ListLoopingDataSource类
下面是数据源对象,我们这里定义为Person
public class Person : IComparable<Person>
{
public string Name { get; set; }
public int Age { get; set; } public int CompareTo(Person other)
{
//比较两个对象:这里只比较名字
return String.CompareOrdinal(Name, other.Name);
}
}
Person
3、接下来是数据绑定
<toolkitPrimitives:LoopingSelector
x:Name="LoopingSelector"
DataSource="{Binding Items}"
ItemMargin="2,3,3,2"
ItemSize="200,150"
FontSize="33" >
<toolkitPrimitives:LoopingSelector.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Age}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</toolkitPrimitives:LoopingSelector.ItemTemplate>
</toolkitPrimitives:LoopingSelector>
public partial class LoopSelectorPage : INotifyPropertyChanged
{
#region Items /// <summary>
/// The <see cref="Items" /> property's name.
/// </summary>
public const string ItemsPropertyName = "Items"; private ListLoopingDataSource<Person> _items; /// <summary>
/// 用于绑定到LoopingSelector的数据源对象
/// </summary>
public ListLoopingDataSource<Person> Items
{
get
{
return _items;
} set
{
if (_items == value)
{
return;
} _items = value;
RaisePropertyChanged(ItemsPropertyName);
}
} #endregion public LoopSelectorPage()
{
InitializeComponent();
LoadApplicationBar();
LoadData();
} private void LoadApplicationBar()
{
ApplicationBar = new ApplicationBar(); var appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative))
{
Text = "test"
};
appBarButton.Click += appBarButton_Click;
ApplicationBar.Buttons.Add(appBarButton); } private void appBarButton_Click(object sender, EventArgs e)
{
//改变数据源
Items = new ListLoopingDataSource<Person>
{
Items = new ObservableCollection<Person>
{
new Person{Name = "Cnblogs", Age = },
new Person{Name = "CodePlex", Age = },
new Person{Name = "CodeProject", Age = },
new Person{Name = "CSDN", Age = },
new Person{Name = "51CTO", Age = },
},
};
//注意,如果改变了数据源,必须设置其SelectedItem属性
Items.SelectedItem = Items.Items.First();
} private void LoadData()
{
Items = new ListLoopingDataSource<Person>
{
Items = new ObservableCollection<Person>
{
new Person{Name = "aaa", Age = },
new Person{Name = "bbb", Age = },
new Person{Name = "ccc", Age = },
new Person{Name = "ddd", Age = },
new Person{Name = "eee", Age = },
},
}; //注意,如果改变了数据源,必须设置其SelectedItem属性
Items.SelectedItem = Items.Items.First(); //也可以监听选择改变的事件
Items.SelectionChanged += Items_SelectionChanged; } void Items_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
MessageBox.Show(string.Format("add:{0}", ((Person)e.AddedItems[]).Name));
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
} #endregion
}
LoopSelectorPage.xaml.cs
好了,数据源的集合是用 IEnumerable<T>存放的,如果需要添加和删除该数据源,可以使用List<T>对象,或是ObservableCollection<T> 对象存储
【WP8】LoopingSelector的更多相关文章
- 【WP8】WebBrowser相关
2014年09月02日更新 今天用了一下WebBrowser,在使用过程中也遇到了一些问题,在这里做一下记录 虽然WebBrowser比较重,会比较影响性能(除非一定要用到它,否则尽量少用),但有时候 ...
- 【WP8】线程安全的StorageHelper
14-08-29 12:32更新:修复StorageHelper部分bug WP8以后提供了StorageFile的方式访问文件,StorageFile对文件的操作只提供了异步的支持,包括WP8.1 ...
- 【WP8】让TextBox文本支持滑动(Scroll)
通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...
- 【WP8】自定义配置存储类
之前在WP7升级到WP8的时候遇到配置不兼容的问题 情景:之前只有一个WP7版本,现在需要发布WP8版本,让用户可以从原来的WP7版本升级到WP8版本 一般情况下从WP7升级到WP8没什么问题 但是在 ...
- 【WP8】ResourceDictionary
WP8中引用资源字典 当我们定义的样式太多的时候,我们可以把样式分别定义在不同的文件中,然后通过 MergedDictionaries 应用到其他资源字典中,看下面Demo 我们可以把样式定义在多个文 ...
- 【WP8】为Webbrowser添加ScrollBar
在WP8中,控件WebBrowser没有提供对滚动条的支持,而在内置的IE中,却可以显示出滚动条(微软这是在坑我们吗),但如果在客户端使用Webbrowser的话,自己构造ScrollBar来显示 通 ...
- 【WP8】Uri关联启动第三方App
在WP8中支持启动第三方应用程序,比如在App1中可以打开App2,你可以在你的应用程序中直接打开QQ,也可以让其他开发者调用你的APP,例如:软件盒子 下面演示被调用方和调用方的使用方法,新建两个项 ...
- 【WP8】WP8调用官方API使用LED灯
在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...
- 【WP8】扩展CM的WindowManager
14-09-09更新:修复AppBar冲突bug 关于WindowManager,一直都很想写一篇博客分享一下,一直在忙别的,今天抽空把这个分享一下 在弹窗在移动开发是一个很常见的交互,很多时候我们都 ...
随机推荐
- Node.js学习笔记(1)--一个最简单的服务器请求
说明(2017-5-2 10:27:03): 1. 需要安装node,http://nodejs.cn/download/ 2. 安装完后,在cmd里输入node -v可以查看版本. 3. 代码foo ...
- Web基础 HTML CSS JS JQuery AJAX
1. Web基础 网页的骨骼HTML 什么是HTML超文本标记语言:Hyper Text Markup Language 这都不重要,重要的是:HTML是Web网页的基本组成部分HTML中定义的元素, ...
- android studio connot resolve
使用AS的时候,肯定会遇到这个问题. 我使用清除缓存都不能解决,不过无意中发现代码变了. final Button select = (Button) findViewById(android.R.i ...
- RabbitMQ文档翻译——Hello World!(上)
文章主要翻译自RabbitMQ官方文档,主要是为了练习英语翻译,顺便学习一下RabbitMQ
- destoon 添加一个新的模块
根目录rename,中config.inc.php文件/module/rename下两个文件,my.inc.php ,rename.class.php/module/rename/admin/三个文件 ...
- Redis 2.8.18 安装报错 error: jemalloc/jemalloc.h: No such file or directory解决方法
http://www.phperz.com/article/14/1219/42002.html ——————————————————————————————————————————————————
- webpack1--安装与初始化
webpack可用于打包所有的脚本,图片,样式表及其他资源. 今天来讲一下如何安装webpack 及 创建package.json.webpack依赖于npm和 node,所以本文是假设你已安装好了n ...
- Java并发框架??AQS中断的支持
线程的定义给我们提供了并发执行多个任务的方式,大多数情况下我们会让每个任务都自行执行结束,这样能保证事务的一致性,但是有时我们希望在任务执行中取消任务,使线程停止.在java中要让线程安全.快速.可靠 ...
- 扫描二维码或其他操作情况下返回界面,onActivityResult()不执行的问题
在使用第三方zxing扫描时,部分手机(好像都是4.4及以下版本的手机)扫描后不调用onActivityResult()返回结果. 调试发现zxing的扫描界面CaptureActivity 在注册时 ...
- R语言入门 一些基本的操作
1 R的下载.安转 R有很多的版本,支持目前主流的操作系统MAC.Linux和WINDOWS系列.因为我个人是在WINDOWS下用R的,所以在这里将只介绍WINDOWS下R的下载&安装. 下载 ...