设为首页 收藏本站
查看: 1270|回复: 0

重新想象 Windows 8 Store Apps (53)

[复制链接]

尚未签到

发表于 2015-5-22 08:03:43 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定  
作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 绑定


  • 与 ObservableCollection 绑定
  • 与 CollectionViewSource 绑定
  • 与 VirtualizedFilesVector 绑定
  • 对 VirtualizedItemsVector 绑定
  
示例
1、演示如何绑定 ObservableCollection 类型的数据
Binding/BindingObservableCollection.xaml






















  Binding/BindingObservableCollection.xaml.cs



/*
* 演示如何绑定 ObservableCollection 类型的数据
*
* ObservableCollection - 在数据集合进行添加项、删除项、更新项、移动项等操作时提供通知
*     CollectionChanged - 当发生添加项、删除项、更新项、移动项等操作时所触发的事件(事件参数:NotifyCollectionChangedEventArgs)
*/
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XamlDemo.Model;
namespace XamlDemo.Binding
{
public sealed partial class BindingObservableCollection : Page
{
private ObservableCollection _employees;
public BindingObservableCollection()
{
this.InitializeComponent();
this.Loaded += BindingObservableCollection_Loaded;
}
void BindingObservableCollection_Loaded(object sender, RoutedEventArgs e)
{
_employees = new ObservableCollection(TestData.GetEmployees());
_employees.CollectionChanged += _employees_CollectionChanged;
listView.ItemsSource = _employees;
}
void _employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
/*
* e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举)
*     Add, Remove, Replace, Move, Reset
* e.OldItems - Remove, Replace, Move 操作时影响的数据列表
* e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引
* e.NewItems - 更改中所涉及的新的数据列表
* e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引
*/
}
private void btnDelete_Click_1(object sender, RoutedEventArgs e)
{
_employees.RemoveAt(0);
}
private void btnUpdate_Click_1(object sender, RoutedEventArgs e)
{
Random random = new Random();
// 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee
_employees.First().Name = random.Next(1000, 10000).ToString();
// 此处的通知来自 ObservableCollection
_employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };
}
}
}
  
2、演示如何绑定 CollectionViewSource 类型的数据,以实现数据的分组显示
Binding/BindingCollectionViewSource.xaml



























  Binding/BindingCollectionViewSource.xaml.cs



/*
* 演示如何绑定 CollectionViewSource 类型的数据,以实现数据的分组显示
*
* CollectionViewSource - 对集合数据启用分组支持
*     Source - 数据源
*     View - 获取视图对象,返回一个实现了 ICollectionView 接口的对象
*     IsSourceGrouped - 数据源是否是一个被分组的数据
*     ItemsPath - 数据源中,子数据集合的属性名称
*     
* ICollectionView - 支持数据分组
*     CollectionGroups - 组数据集合
*     
*
* 注:关于数据分组的应用还可参见:XamlDemo/Index.xaml 和 XamlDemo/Index.xaml.cs
*/
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace XamlDemo.Binding
{
public sealed partial class BindingCollectionViewSource : Page
{
public BindingCollectionViewSource()
{
this.InitializeComponent();
this.Loaded += BindingCollectionViewSource_Loaded;
}
void BindingCollectionViewSource_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
XElement root = XElement.Load("SiteMap.xml");
var items = LoadData(root);
// 构造数据源
CollectionViewSource groupData = new CollectionViewSource();
groupData.IsSourceGrouped = true;
groupData.Source = items;
groupData.ItemsPath = new PropertyPath("Items");
// 绑定 ICollectionView 类型的数据,以支持分组
listView.ItemsSource = groupData.View;
}
// 获取数据
private List LoadData(XElement root)
{
if (root == null)
return null;
var items = from n in root.Elements("node")
select new GroupModel
{
Title = (string)n.Attribute("title"),
Items = LoadData(n)
};
return items.ToList();
}
class GroupModel
{
public string Title { get; set; }
public List Items { get; set; }
}
}
}
  
3、演示如何绑定 VirtualizedFilesVector
Binding/BindingVirtualizedFilesVector.xaml






















  Binding/BindingVirtualizedFilesVector.xaml.cs



/*
* 演示如何绑定 VirtualizedFilesVector
*
* 本 Demo 演示了如何将图片库中的文件绑定到 GridView
*/
using Windows.Storage;
using Windows.Storage.BulkAccess;
using Windows.Storage.FileProperties;
using Windows.Storage.Search;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Binding
{
public sealed partial class BindingVirtualizedFilesVector : Page
{
public BindingVirtualizedFilesVector()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
QueryOptions queryOptions = new QueryOptions();
queryOptions.FolderDepth = FolderDepth.Deep;
queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
queryOptions.SortOrder.Clear();
SortEntry sortEntry = new SortEntry();
sortEntry.PropertyName = "System.FileName";
sortEntry.AscendingOrder = true;
queryOptions.SortOrder.Add(sortEntry);
// 一个用于搜索图片库中的文件的查询
StorageFileQueryResult fileQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);
// 创建一个 FileInformationFactory 对象
var fileInformationFactory = new FileInformationFactory(fileQuery, ThumbnailMode.PicturesView, 160, ThumbnailOptions.UseCurrentScale, true);
// 获取 VirtualizedFilesVector
itemsViewSource.Source = fileInformationFactory.GetVirtualizedFilesVector();
}
}
}
  
4、演示如何绑定 VirtualizedItemsVector
Binding/BindingVirtualizedItemsVector.xaml





























  Binding/BindingVirtualizedItemsVector.xaml.cs



/*
* 演示如何绑定 VirtualizedItemsVector
*
* 本 Demo 演示了如何将图片库中的顶级文件夹和顶级文件绑定到 GridView,同时演示了如何 runtime 时选择模板
*/
using Windows.Storage;
using Windows.Storage.BulkAccess;
using Windows.Storage.FileProperties;
using Windows.Storage.Search;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Binding
{
public sealed partial class BindingVirtualizedItemsVector : Page
{
public BindingVirtualizedItemsVector()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
QueryOptions queryOptions = new QueryOptions();
queryOptions.FolderDepth = FolderDepth.Shallow;
queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
queryOptions.SortOrder.Clear();
SortEntry sortEntry = new SortEntry();
sortEntry.PropertyName = "System.IsFolder";
sortEntry.AscendingOrder = false;
queryOptions.SortOrder.Add(sortEntry);
SortEntry sortEntry2 = new SortEntry();
sortEntry2.PropertyName = "System.ItemName";
sortEntry2.AscendingOrder = true;
queryOptions.SortOrder.Add(sortEntry2);
// 一个用于搜索图片库中的顶级文件夹和顶级文件的查询
StorageItemQueryResult itemQuery = KnownFolders.PicturesLibrary.CreateItemQueryWithOptions(queryOptions);
// 创建一个 FileInformationFactory 对象
var fileInformationFactory = new FileInformationFactory(itemQuery, ThumbnailMode.PicturesView, 160, ThumbnailOptions.UseCurrentScale, true);
// 获取 VirtualizedItemsVector
itemsViewSource.Source = fileInformationFactory.GetVirtualizedItemsVector();
}
}
// 继承 DataTemplateSelector 以实现 runtime 时选择模板
public class FileFolderInformationTemplateSelector : DataTemplateSelector
{
// 显示文件时的模板
public DataTemplate FileInformationTemplate { get; set; }
// 显示文件夹时的模板
public DataTemplate FolderInformationTemplate { get; set; }
// 根据 item 的类型选择指定的模板
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
var folder = item as FolderInformation;
if (folder == null)
return FileInformationTemplate;
else
return FolderInformationTemplate;
}
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69337-1-1.html 上篇帖子: Windows 8 常用第三方SDK使用概览 下篇帖子: Windows 8 应用开发技术资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表