[源码下载]

与众不同 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的更多相关文章

  1. 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl

    [源码下载] 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl 作者:webabcd 介绍与众不同 windows p ...

  2. 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout

    [源码下载] 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout 作者:webabcd 介绍与众不同 windows ...

  3. 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout

    [源码下载] 与众不同 windows phone (51) - 8.1 新增控件: DatePickerFlyout, TimePickerFlyout 作者:webabcd 介绍与众不同 wind ...

  4. Windows 8.1 应用再出发 - 几种新增控件(1)

    Windows 8.1 新增的一些控件,分别是:AppBar.CommandBar.DatePicker.TimePicker.Flyout.MenuFlyout.SettingsFlyout.Hub ...

  5. Windows 8.1 应用再出发 - 几种新增控件(2)

    本篇我们接着来介绍Windows 8.1 的新增控件,分别是:Flyout.MenuFlyout.SettingsFlyout.Hub 和 Hyperlink. 1. Flyout Flyout被称为 ...

  6. Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2)

    上篇我们介绍了Windows 8.1 和 WinJS 中新增控件中的 AppBarCommand.BackButton.Hub.ItemContainer,本篇我们接着来介绍 NavBar.Repea ...

  7. 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

    [源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  8. 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker

    [源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...

  9. 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout

    [源码下载] 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout 作者:webabcd 介绍重新想象 ...

随机推荐

  1. 一篇文章,读懂 Netty 的高性能架构之道

    原文 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机 ...

  2. [Javascript] Limit Built Branches on Travis

    By default, Travis will build all branches, tags, and Pull Requests. Because we're building our mast ...

  3. 浅谈html5网页内嵌视频

    更好的阅读体验:浅谈html5网页内嵌视频 如今在这个特殊的时代下:flash将死未死,微软和IE的历史问题,html5标准未定,苹果和谷歌的闭源和开源之争,移动互联网的大势所趋,浏览器各自为战... ...

  4. MT写的对URL操作的两个方法

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 聊聊IO多路复用之select、poll、epoll详解

    本文转载自: http://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666538922&idx=1&sn=e6b436ef ...

  6. 那些年做过的 .NET Web 项目和 iOS 之路的一些思考

    从2010年3月份出来工作到2015年初,做过的大大小小的 .NET Web 项目如下: (1)售楼系统产品    「Role: Team Member」 (2)中弘合同管理系统    「Role: ...

  7. Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined

    I'm stucked in configuring my web.config file under a web forms project in order to get an instance ...

  8. Go语言TCP/UDP Socket编程

    1. TCP编程 TCPClient // TCPClient project main.go package main import ( "fmt" "net" ...

  9. js判定IE

    var ie=!-[1,]; 这句话对于多数前端来说都很熟悉,遇到判定是否是ie浏览器就用这个,但是对于由来以及为什么可能没有深入了解过. 短短6个bytes就做了判定.这个表达式是利用IE和标准浏览 ...

  10. undefined reference to `_imp___ZN8QWebViewC1EP7QWidget'

    add this line to your .pro file: QT += webkitwidgets