与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom
作者:webabcd
介绍
与众不同 windows phone 8.1 之 新增控件
- AutoSuggestBox - 自动建议文本框
- ListView, GridView, SemanticZoom - 同 Windows Store Apps 中的控件
示例
1、演示 AutoSuggestBox 的应用
AutoSuggestBoxDemo.xaml
<Page
x:Class="Demo.Control.AutoSuggestBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo.Control"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid>
<StackPanel Orientation="Vertical"> <AutoSuggestBox Name="autoSuggestBox">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl}" Width="20" Height="20" />
<TextBlock Text="{Binding Title}" FontSize="20" />
</StackPanel>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox> <TextBlock Name="lblMsg" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
AutoSuggestBoxDemo.xaml.cs
/*
* AutoSuggestBox - 自动建议文本框(wp only)
* AutoMaximizeSuggestionArea - 建议框(即下拉部分)的区域是否最大化
* MaxSuggestionListHeight - 建议框(即下拉部分)的最大高度
* Header - AutoSuggestBox 的标头对象
* PlaceholderText - 占位文本
* Text - 文本框内显示的文本
* TextBoxStyle - 文本框的样式
* IsSuggestionListOpen - 建议框(即下拉部分)是否是打开状态
* TextMemberPath - 建议框中的对象映射到文本框中时的字段名(如果建议框中显示的只是字符串,就不用设置这个了)
* UpdateTextOnSelect - 单击建议框中的项时,是否将其中的 TextMemberPath 指定的值赋值给文本框
*
* SuggestionChosen - 在建议框(即下拉部分)中选择了某一项后触发的事件
* TextChanged - 文本框中的输入文本发生变化时触发的事件
*/ using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls; namespace Demo.Control
{
public sealed partial class AutoSuggestBoxDemo : Page
{
private ObservableCollection<SuggestionModel> _suggestions = new ObservableCollection<SuggestionModel>(); public AutoSuggestBoxDemo()
{
this.InitializeComponent(); autoSuggestBox.Header = "AutoSuggestBox";
autoSuggestBox.PlaceholderText = "AutoSuggestBox"; // 数据源有 Title 字段和 ImageUrl 字段,当用户在建议框中选中某一个对象时,会将 Title 字段指定的值赋值给文本框
autoSuggestBox.TextMemberPath = "Title";
// 用户选中建议框中的对象时,是否将 TextMemberPath 指定的值赋值给文本框
autoSuggestBox.UpdateTextOnSelect = true; // 将 AutoSuggestBox 的数据源设置为一个对象集合
autoSuggestBox.ItemsSource = _suggestions; autoSuggestBox.TextChanged += autoSuggestBox_TextChanged;
autoSuggestBox.SuggestionChosen += autoSuggestBox_SuggestionChosen;
} void autoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
_suggestions.Clear(); // 根据用户的输入,修改 AutoSuggestBox 的数据源
for (int i = ; i < ; i++)
{
_suggestions.Add(new SuggestionModel()
{
Title = (sender.Text + "_" + i.ToString()),
ImageUrl = "/Assets/Kid.png"
});
}
}
} void autoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
lblMsg.Text = "选中的是:" + ((SuggestionModel)args.SelectedItem).Title;
} class SuggestionModel
{
public string Title { get; set; }
public string ImageUrl { get; set; }
}
}
}
2、ListView, GridView, SemanticZoom 的用法同 Windows Store Apps 中的控件,本例演示如何通过他们替代原 LongListSelector 控件(在 wp8.1 中已经没有 LongListSelector 了)
MainPage.xaml
<Page
x:Class="Demo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page.Resources>
<!--索引视图的背景色,Enabled代表有效索引,Disabled代表无效索引(比如此group内无item)-->
<JumpListItemBackgroundConverter x:Key="BackgroundConverter" Enabled="Blue" Disabled="Red"/>
<!--索引视图的前景色,Enabled代表有效索引,Disabled代表无效索引(比如此group内无item-->
<JumpListItemForegroundConverter x:Key="ForegroundConverter" Enabled="White" Disabled="Gray"/> <!--索引视图的 ItemTemplate-->
<DataTemplate x:Key="IndexTemplate">
<Border Padding="5">
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="400" Height="58" HorizontalAlignment="Left">
<!--绑定 Group.Title,即此对应的 group 的 Title 字段-->
<TextBlock Text="{Binding Group.Title}" Foreground="{Binding Converter={StaticResource ForegroundConverter}}"
FontSize="24" Padding="6" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate> <!--详细视图的 ItemTemplate-->
<DataTemplate x:Key="ItemTemplate">
<StackPanel VerticalAlignment="Top">
<Grid Tag="{Binding Url}" Tapped="Grid_Tapped">
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" />
</Grid>
</StackPanel>
</DataTemplate> <!--详细视图的 HeaderTemplate-->
<DataTemplate x:Key="HeaderTemplate">
<Border Background="Transparent" Padding="5">
<!--
类似 PhoneAccentBrush 这类的本地资源,其定义在 generic.xaml
-->
<Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}"
BorderThickness="2" Height="58" Margin="0,0,18,0" HorizontalAlignment="Left">
<TextBlock Text="{Binding Title}" Foreground="{StaticResource PhoneForegroundBrush}"
FontSize="24" Padding="6" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate>
</Page.Resources> <Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="24,16,0,24">
<TextBlock Text="与众不同 windows phone 8.1 系列" Style="{ThemeResource TitleTextBlockStyle}" />
</StackPanel> <Grid Grid.Row="1">
<SemanticZoom>
<!--详细视图-->
<SemanticZoom.ZoomedInView>
<ListView Name="listView" ItemTemplate="{StaticResource ItemTemplate}">
<ListView.GroupStyle>
<!--如果此group下无item,是否隐藏此group的header-->
<GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource HeaderTemplate}"/>
</ListView.GroupStyle>
</ListView>
</SemanticZoom.ZoomedInView>
<!--索引视图-->
<SemanticZoom.ZoomedOutView>
<GridView Name="gridView" ItemTemplate="{StaticResource IndexTemplate}" Background="Black">
</GridView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
</Grid>
</Grid>
</Page>
MainPage.xaml.cs
/*
* 本系列教程的索引页,同时演示了用 ListView 和 SemanticZoom 取代 LongListSelector
*/ using System;
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;
using Windows.UI.Xaml.Navigation; namespace Demo
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; this.Loaded += MainPage_Loaded;
} void MainPage_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"); listView.ItemsSource = groupData.View; // item,即详细视图
gridView.ItemsSource = groupData.View.CollectionGroups; // group,即索引视图
} // 获取数据
private List<GroupModel> LoadData(XElement root)
{
if (root == null)
return null; var items = from n in root.Elements("node")
select new GroupModel
{
Title = (string)n.Attribute("title"),
Url = (string)n.Attribute("url"),
Items = LoadData(n)
}; return items.ToList();
} // 导航至目标页
private void Grid_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var className = (string)(sender as FrameworkElement).Tag;
var frame = Window.Current.Content as Frame;
frame.Navigate(Type.GetType(className));
}
} public class GroupModel
{
public string Title { get; set; }
public string Url { get; set; }
public List<GroupModel> Items { get; set; }
}
}
OK
[源码下载]
与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom的更多相关文章
- 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl
[源码下载] 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl 作者:webabcd 介绍与众不同 windows p ...
- 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout
[源码下载] 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout 作者:webabcd 介绍与众不同 windows ...
- 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout
[源码下载] 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout 作者:webabcd 介绍与众不同 wind ...
- Windows 8.1 应用再出发 - 几种新增控件(1)
Windows 8.1 新增的一些控件,分别是:AppBar.CommandBar.DatePicker.TimePicker.Flyout.MenuFlyout.SettingsFlyout.Hub ...
- Windows 8.1 应用再出发 - 几种新增控件(2)
本篇我们接着来介绍Windows 8.1 的新增控件,分别是:Flyout.MenuFlyout.SettingsFlyout.Hub 和 Hyperlink. 1. Flyout Flyout被称为 ...
- Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2)
上篇我们介绍了Windows 8.1 和 WinJS 中新增控件中的 AppBarCommand.BackButton.Hub.ItemContainer,本篇我们接着来介绍 NavBar.Repea ...
- 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar
[源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker
[源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...
- 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout
[源码下载] 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout 作者:webabcd 介绍重新想象 ...
随机推荐
- 关于meta知多少
本来打算写关于手机端的知识,想了想先从meta着手.接下来请大家看几个网站的例子. 一.天猫(http://m.tmall.com) <title>天猫触屏版</title> ...
- 命令行上的narrowing(随着输入逐步减少备选项)工具
前面在介绍zsh的时候,说过它的补全用来起比bash的Tab补全方便多了,在有多个备选项是你只要用光标键来挑选就是了,而不是全列出来提示你再多输入几个字符.而Emacs的anything / helm ...
- 安装mmseg出错 config.status: error: cannot find input file: src/Makefile.in
aclocallibtoolize --forceautomake --add-missingautoconfautoheadermake clean
- HTML5图片旋转
HTML5图片旋转 首先我们使用Cococs2dx-Js-Lite版,来创建一个工程,我们所需要的开发环境如下: 1,webstrom 2,google chrome浏览器 3,cocos2dx-Js ...
- 【Cocos2d-Js基础教学(3)各种基类的定义和使用】
在游戏开发过程中我们会遇到很多继承关系的处理,特别是层级之间的关系处理. 可能有的同学也做过类似的处理,比如: 游戏的显示层分级为: 底层Scene ,界面层Layer,页面层Page,弹框层Tip等 ...
- ikvm.net简介
ikvm.net是什么 http://www.ikvm.net/ ikvm.net是能够运行在mono和.net framework的java虚拟机.它包括了 在.net中实现的一个java虚拟机 j ...
- C# 调用 WebService 连接ORACLE 11g
这几天开发一个WebService遇到很多问题,记录下来顺便帮助一下以后遇到情况的人. 我是通过ADO.NET来连接ORACLE的,也可以用ORACLE提供的ODP.NET. 通过正常的连接后部署II ...
- 译:C#面向对象的基本概念 (Basic C# OOP Concept) 第二部分(封装,抽象,继承)
6.封装 封装就是对外部类隐藏成员或变量.我已经说过房子的保安仅仅被限制在房子的入口处,不需要知道屋内发生了什么.房主对保安隐藏了屋内所发生的任何事,以便更安全.隐藏和限制就被称为封装. 例如我们有两 ...
- DevExpress.XtraGrid winform试用分享
DevExpress.XtraGrid在winform里使用还挺麻烦,为了减少重复代码和代码复用,本人针对DevExpress.XtraGrid封装了一个Form的基类,其中涵盖了DevExpress ...
- toad 常用快捷键与配置
F8 调出以前执行的sql命令 F9 执行全部sql Ctrl+. 补全table_name Ctrl+t 补全table_name,或者显示字段 alt+ 箭头上下 看sql history Ctr ...