53
Building Windows Phone Applications Nguyen Tuan | Microsoft Certified Trainer

04.Navigation on Windows Phone

Embed Size (px)

DESCRIPTION

Windows Phone 8 Programming

Citation preview

Page 1: 04.Navigation on Windows Phone

Building Windows PhoneApplications

Nguyen Tuan | Microsoft Certified Trainer

Page 2: 04.Navigation on Windows Phone

Agenda• Controls:• Border.• Button.• Canvas.• Checkbox.• RadioButton.• HyperlinkButton.• TextBox.• PasswordBox.• TextBlock.

• Lists, List Items:• Lists and List Items• List Controls and capabilities • Creating list items• Grouped lists and grouping navigation• Long lists and best practices

Page 3: 04.Navigation on Windows Phone

controls

Page 4: 04.Navigation on Windows Phone

Controls

• Reusable units for presentation

• Defined UI/Interaction patterns

• Input

• Feedback

• Consistency with OS

• Familiarity to the user

Page 5: 04.Navigation on Windows Phone

Content model

• ContentControl

• ButtonBase• Button, RepeatButton, ToggleButton RadioButton, CheckBox

• UserControl

• ItemsControl

• ListBox, DataGrid, TreeView, ToolBar, TabControl, GridView (Win8), ListView

Page 6: 04.Navigation on Windows Phone

Core Controls

ToggleSwitch

CheckBox

Button

Hyperlink Button

Radio buttons

ProgressBar(indeterminate)

ProgressBar(determinate)

ProgressRing

Slider

Page 7: 04.Navigation on Windows Phone

Core Controls (part 2 )

TextBox

PasswordBox

TextBox(Multiline)

ComboBox(with header)

ListBox

Image

Popup

Page 8: 04.Navigation on Windows Phone

Core Controls (part 3 )

Date & Time pickers

WebView

Page 9: 04.Navigation on Windows Phone
Page 10: 04.Navigation on Windows Phone

styles and templates

Page 11: 04.Navigation on Windows Phone

A collection of property values

• Grouped in a Style object

• Usually defined as a resource

Styles

Page 12: 04.Navigation on Windows Phone

Styles

<Style />

This is a text

And another

Page 13: 04.Navigation on Windows Phone

Extending Styles (BasedOn)

Page 14: 04.Navigation on Windows Phone

Lookless ControlsControls have behavior

Visuals separate from implementation

• Defined in Xaml

• Defined by a designer

• Controls usually have a default “style”

Page 15: 04.Navigation on Windows Phone

Templates

Page 16: 04.Navigation on Windows Phone
Page 17: 04.Navigation on Windows Phone

TemplateBinding• Template should honor control properties

• Background, Padding, etc.

• TemplateBinding connects properties

• Projects control properties onto the screen

Page 18: 04.Navigation on Windows Phone

UserControl• Logical grouping of items.

• Can be reused.

• Has XAML front end and code-behind.

Page 19: 04.Navigation on Windows Phone

Demo:PhoneToolkitSample

Page 20: 04.Navigation on Windows Phone

visual state manager

Page 21: 04.Navigation on Windows Phone

Visual States

[TemplateVisualState(GroupName = "CommonStates", Name = "Pressed")][TemplateVisualState(GroupName = "CommonStates", Name = "MouseOver")][TemplateVisualState(GroupName = "CommonStates", Name = "Normal")][TemplateVisualState(GroupName = "CommonStates", Name = "Disabled")][TemplateVisualState(GroupName = "FocusStates", Name = "Unfocused")][TemplateVisualState(GroupName = "FocusStates", Name = "Focused")]public class Button : ButtonBase{

… }

Page 22: 04.Navigation on Windows Phone

Visual states

Click

Page 23: 04.Navigation on Windows Phone

VisualStateManager

Page 24: 04.Navigation on Windows Phone
Page 25: 04.Navigation on Windows Phone

• ListView

• GridView

• ListBox (last resort)

Controls for Lists of Things

Page 26: 04.Navigation on Windows Phone

ListView• Vertical list of items

• Good for:• Simple lists

• Grouped lists

Page 27: 04.Navigation on Windows Phone

GridView• Grid of items

• Usually image-based items

Page 28: 04.Navigation on Windows Phone

List Control Templates• HeaderTemplate

• FooterTemplate

• ItemTemplate

• ItemContainerStyle

• ItemsPanel

• GroupStyle• ContainerStyle

• HeaderTemplate

Page 29: 04.Navigation on Windows Phone

List Headers and Footers• HeaderTemplate displays at top of list

• FooterTemplate displays at bottom of list

• Scroll with list content (not sticky)

Reference:http://msdn.microsoft.com/en-us/library/windows/apps/hh465319.aspx

Page 30: 04.Navigation on Windows Phone

List Item Templates

• ItemTemplate• displays item data

Page 31: 04.Navigation on Windows Phone

List Item Templates• ItemTemplate

• displays item data

• ItemContainerStyle• displays item state

Page 32: 04.Navigation on Windows Phone

List Item Templates• ItemTemplate

• displays item data

• ItemContainerStyle• displays item state

• ItemsPanel• manages item-by-item layout

<ItemsPanelTemplate x:Key="SampleItemsPanel"><ItemsStackPanel Orientation="Vertical" />

</ItemsPanelTemplate>

<ItemsPanelTemplate x:Key="SampleItemsPanel"><ItemsWrapGrid Orientation=“Horizontal" />

</ItemsPanelTemplate>

Page 33: 04.Navigation on Windows Phone
Page 34: 04.Navigation on Windows Phone

Reordering• Items shift into floating UI

• Reordered with Tap-Hold + Drag

• Windows Phone

• Windows

• Grouped Lists cannot be reordered

MyListView.ReorderMode = ListViewReorderMode.Enabled;

MyListView.CanReorderItems = false;

Page 35: 04.Navigation on Windows Phone

MultiSelection• Displays checkboxes for multi-selection

MyListView.SelectionMode = ListViewSelectionMode.Multiple;

Page 36: 04.Navigation on Windows Phone
Page 37: 04.Navigation on Windows Phone

Grouping Data• Grouping requires data as a

CollectionViewSource

• Or a list of lists (no JumpList)

37

Page 38: 04.Navigation on Windows Phone

Grouping Data• JumpLists Require

- KeyedList

• or

- AlphaKeyGroup

Page 39: 04.Navigation on Windows Phone

Grouping Data – KeyedList- Start with a list of data

- Use a KeyedList class

- Format the data using the following LINQ query

var groupedItems =from item in itemsorderby item.SortPropertygroup item by item.SortProperty into itemsByPropertyselect new KeyedList<string, MyObject>(itemsByProperty);

List<KeyedList<string, SampleItem>> KeyedList = groupedItems.ToList();

Page 40: 04.Navigation on Windows Phone

Grouping Data – AlphaKeyGroup- Start with a list of data

- Create an AlphaKeyGroup class

- Create grouping with:

var alphaKeyGroup = AlphaKeyGroup<SampleItem>.CreateGroups(items, // basic list of items(SampleItem s) => { return s.Title; }, // the property to sort true); // order the items

// returns type List<AlphaKeyGroup<SampleItem>>

Page 41: 04.Navigation on Windows Phone

Grouping Data – CollectionViewSource

• Set DataContext to your page

• Place a CollectionViewSource in the Resources:

<CollectionViewSourcex:Key="ItemsGrouped"IsSourceGrouped="True"ItemsPath="InternalList"Source="{Binding MyKeyedList, Source={Binding}}"/>

<ListView ItemsSource="{Binding Source={StaticResource ItemsGrouped}}" />

Page 42: 04.Navigation on Windows Phone

Grouping List UI• Grouping Style and Template

<ListView.GroupStyle>

<GroupStyle HidesIfEmpty="True" >

<GroupStyle.HeaderTemplate>…<TextBlock Text="{Binding Key}" />…

<GroupStyle.HeaderTemplate>

</GroupStyle>

</ListView.GroupStyle>

Page 43: 04.Navigation on Windows Phone

Grouping List Navigation• Semntic Zoom

<SemanticZoom><SemanticZoom.ZoomedInView>

<!-- ListView or GridView --><!-- ItemsSource binds to

CollectionViewSource --></SemanticZoom.ZoomedInView><SemanticZoom.ZoomedOutView>

<!-- ListView or GridView --><!-- ItemsSource bound to

CollectionViewSource,Path=CollectionGroups -->

</SemanticZoom.ZoomedOutView></SemanticZoom>

Page 44: 04.Navigation on Windows Phone

• ZoomedInView – default view

• ZoomedOutView

• triggered by group item interaction

• overlays the screen (Flyout)

• transparent background

Semantic Zoom Design

Reference:http://msdn.microsoft.com/en-us/library/windows/apps/hh465319.aspx

Page 45: 04.Navigation on Windows Phone
Page 46: 04.Navigation on Windows Phone
Page 47: 04.Navigation on Windows Phone

Group layouts are virtualized

List controls renders elements near the viewport

Item Rendering and Group Virtualization

Page 48: 04.Navigation on Windows Phone

Progressive Item Rendering

Sed nec sodaleAlpine Ski House

Sed nec sodaleAlpine Ski House

Page 49: 04.Navigation on Windows Phone

ContainerContentChanging

Fires when item is realized

Items can be rendered in phases

<ListView ItemTemplate="{StaticResource SampleDataTemplate}"ContainerContentChanging="IncrementalUpdateHandler" >

private void IncrementalUpdateHandler(ListViewBase sender, ContainerContentChangingEventArgsargs){

args.Handled = true;if (args.Phase != 0)

throw new Exception("we will always be in phase 0");else{

// show a placeholder shapeargs.RegisterUpdateCallback(ShowText);

}}

Page 50: 04.Navigation on Windows Phone

ContainerContentChangingLater phases will be skipped if too much time is needed

private void ShowText(ListViewBase sender, ContainerContentChangingEventArgs args){

args.Handled = true;if (args.Phase != 1)

throw new Exception("we should always be in phase 1");else{

// show text from the templateargs.RegisterUpdateCallback(ShowImage);

}}

Page 51: 04.Navigation on Windows Phone

Phase priorities1. Simple shapes (placeholder visuals)

2. Key text (title)

3. Other text (subtitle)

4. Images

Page 52: 04.Navigation on Windows Phone

Performance tip

Do not use Visibility to show/hide UIElements

this will fire a new ContainerContentChanging event

Instead, use “Opacity=0”

Page 53: 04.Navigation on Windows Phone

References • http://msdn.microsoft.com/en-

us/library/windowsphone/develop/jj735581%28v=vs.105%29.aspx

• http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.semanticzoom.aspx