与众不同 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 介绍重新想象 ...
随机推荐
- android之AlertDialog 点击其它区域自己主动消失
遇到一个问题记录下来,在开发中使用了AlertDialog,想点击屏幕其它区域的时候让这个dialog消失,一開始不做不论什么设置,在小米手机能够正常显示,可是在三星中却有问题.后来发现少了一个属性: ...
- 解决Win7旗舰版开机后无线网络识别非常慢的问题
最近电脑开机后WIFI识别和连接非常慢,不知何故.查看百度安全卫士的优化记录,发现其禁用了 Network List Service,将该服务设为自动启动,重启服务后,问题解决.PS:如此优化太可恶!
- Mac系统下,在android studio中使用Github版本管理
1.下载并安装github客户端http://git-scm.com/download/ 2.打开android studio,测试github是否使用ok 点击"test",如果 ...
- 配置Tomcat的访问日志格式化输出
博客搬家,本文新地址:http://www.zicheng.net/article/9 本文描述如何配置tomcat的访问日志,按我们的要求输出指定的日志格式. 且在Nginx+Tomcat的配置 ...
- Java开发者值得关注的7款新工具
云计算.大数据地快速发展催生了不少热门的应用及工具.作为老牌语言Java,其生态圈也出来了一些有关云服务.监控.文档分享方面的工具.本文总结了7款较新的Java工具,大家不妨看下. 1. JClari ...
- GCC 编译使用动态链接库 LD
可以把当前路径加入 /etc/ld.so.conf中然后运行ldconfig,或者以当前路径为参数运行ldconfig 2.把当前路径加入环境变量LD_LIBRARY_PATH中3. 如果你觉得不会引 ...
- SAP ECC CO 配置
SAP ECC 6.0 Configuration Document Controlling (CO) Table of Content TOC \o \h \z 1. Enterprise Stru ...
- [转载]IE678兼容性前缀区分
符合一贯简单的风格,只需记住两个符号 _ 和 * 看例子: #main { background:black; *background:red; _background:blue; } 第一句会被所有 ...
- WorkbookDesigner mvc里面返回file
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- WinStore控件之TextBox
1 TextBox简单实例 内容摘要:包含文本的选中,弹出什么类型的键盘,回车隐藏键盘, <Grid Name="root" Background="Transpa ...