与众不同 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 介绍重新想象 ...
随机推荐
- 使用sphinx生成Python文档
发现找不到matplotlib.sphinxext.mathmpl: 可以直接easy_install matplotlib,也可以去这里下载安装包 发现exception: matplotlib r ...
- 解决cxf+spring发布的webservice,types,portType和message以import方式导入
用cxf+spring发布了webservice,发现生成的wsdl的types,message和portType都以import的方式导入的.. 原因:命名空间问题 我想要生成的wsdl在同个文件中 ...
- Swift XML解析库 - SwiftyXMLParser
经过在CocoaPods中筛选以后,发这个这个比较好用,整理出来 如果有需要可以在Pods命令端输入: pod search xml 这样会搜索出很多相关Xml的第三方库 SwiftyXMLParse ...
- iOS开发问题之Could not instantiate class named NSLayoutConstraint
这个问题在ios6.0之前的版本中运行会出现,因为使用Xcode 4.5之后的版本新建项目默认是选中AutoLayout的,但这个特性是在iOS6.0之后的版本中才支持的. 解决办法是选中.stroy ...
- 关于imp无法导出空表
前天在业务库中导出完整库时,再导入到新库时发现部分表丢失. 看日志后分析是部分空表没有导出.查google知,11G中新特性,当表无数据时,不分配segment,以节省空间.而使用exp命令时,无Se ...
- LoadRunner 12试用
LoadRunner 12试用 http://blog.csdn.net/testing_is_believing/article/details/23611845
- 更新jar包中的MANIFEST.MF
当前目录存在lib.jar和MANIFEST.MF文件,在当前目录下执行: jar -uvfm lib.jar MANIFEST.MF 如果lib.jar里存在META-INF/MANIFEST.MF ...
- Oracle的model语句入门-转
Model语句是Oracle 10g的新功能之一. 本文通过一些简单的例子帮助理解Model语句的用法,复杂使用场景请参考其他文章. 环境:当然需要Oracle 10g以上,本人是在11g上测试的. ...
- swift 类和结构体
1:类和结构体定义 类和结构体分别通过关键字class 和struct定义. swift的编码风格是类class和结构体struct名字使用大写字母开头的匈牙利表示法,相反的.类的方法和属性则用小写字 ...
- 130 个你需要了解的 vim 命令
基础 :e filename Open filename for edition :w Save file :q Exit Vim :q! Quit without saving :x Write f ...