presentation - windows app development - ii - mr. chandan gupta

61

Upload: mobilenepal

Post on 14-Jul-2015

179 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Presentation - Windows App Development - II - Mr. Chandan Gupta
Page 2: Presentation - Windows App Development - II - Mr. Chandan Gupta

Agenda Windows 8/Windows Phone

In this module, we

will....

Extension methods

Portable Class Libraries

MVVM Architecture

Data Binding Linked files

A common user experience

#if conditionals

In this module:

Page 3: Presentation - Windows App Development - II - Mr. Chandan Gupta
Page 4: Presentation - Windows App Development - II - Mr. Chandan Gupta
Page 5: Presentation - Windows App Development - II - Mr. Chandan Gupta

Common Structure

Page 6: Presentation - Windows App Development - II - Mr. Chandan Gupta

Model-View-ViewModel

View

ViewModel

Commands

Data Binding

Model

Page 7: Presentation - Windows App Development - II - Mr. Chandan Gupta

MVVM Benefits

• Reuse Model and View-Model code

• Test the ViewModel with unit tests

• Maintainability

• Can show design-time data in Expression Blend and the Visual Studio designer

Page 8: Presentation - Windows App Development - II - Mr. Chandan Gupta

Model – View - ViewModel

public class MyModelItem{

public int Id { get; set; }

[DataMember(Name = "text")]public string Text { get; set; }

[DataMember(Name = "complete")]public bool Complete { get; set; }

}

public class MyModelItem{

public int Id { get; set; }

[DataMember(Name = "text")]public string Text { get; set; }

[DataMember(Name = "complete")]public bool Complete { get; set; }

}

Page 9: Presentation - Windows App Development - II - Mr. Chandan Gupta

Data Service Calls in the ViewModel

private MobileServiceCollectionView<TodoItem> items;private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();

private void RefreshTodoItems(){

items = todoTable.Where(todoItem => todoItem.Complete == false).ToCollectionView();

}

private async void InsertTodoItem(TodoItem todoItem){

await todoTable.InsertAsync(todoItem);items.Add(todoItem);

}

Page 10: Presentation - Windows App Development - II - Mr. Chandan Gupta

Extract Model and Service Calls

Page 11: Presentation - Windows App Development - II - Mr. Chandan Gupta

Good MVVM Practice

Page 12: Presentation - Windows App Development - II - Mr. Chandan Gupta

2

DataBinding(INotifyPropertyChanged)

Page 13: Presentation - Windows App Development - II - Mr. Chandan Gupta

Data Binding in the ViewModel

// Create INotifyPropertyChanged property called VisibleItemsprivate void MobileServiceCollectionView<TodoItem> RefreshTodoItems(){

items = todoTable.Where(todoItem => todoItem.Complete == false).ToCollectionView();

_visibleItems.Clear();foreach (var item in items){

_visibleItems.Add(item);}NotifyPropertyChanged("VisibleItems");

}

Page 14: Presentation - Windows App Development - II - Mr. Chandan Gupta

Improved Structure

Page 15: Presentation - Windows App Development - II - Mr. Chandan Gupta

“Add as Link”

Page 16: Presentation - Windows App Development - II - Mr. Chandan Gupta

“Add as Link”

Page 17: Presentation - Windows App Development - II - Mr. Chandan Gupta

Common APIs in Windows 8 and Windows

Phone 8

Page 18: Presentation - Windows App Development - II - Mr. Chandan Gupta

Common APIs

Page 19: Presentation - Windows App Development - II - Mr. Chandan Gupta

Hardware Implementation: Accelerometer

Page 20: Presentation - Windows App Development - II - Mr. Chandan Gupta

Hardware Implementation: Accelerometer

<Canvas x:Name="hostCanvas" Loaded=“canvas_Loaded">

Accelerometer _accel;private void canvas_Loaded(object sender, RoutedEventArgs e){

_accel = Accelerometer.GetDefault();if (_accel != null)

_accel.ReadingChanged += _accel_ReadingChanged;}

Page 21: Presentation - Windows App Development - II - Mr. Chandan Gupta

Hardware Implementation: Accelerometer

void _accel_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args){

double _accelX = args.Reading.AccelerationX;double _accelY = args.Reading.AccelerationY;// Update the position of the ellipse

}

Page 22: Presentation - Windows App Development - II - Mr. Chandan Gupta

Threading Difference

Page 23: Presentation - Windows App Development - II - Mr. Chandan Gupta

Threading

Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{

double _accelX = args.Reading.AccelerationX;double _accelY = args.Reading.AccelerationY;// Update ellipse location

});

Deployment.Current.Dispatcher.BeginInvoke(() =>{

double _accelX = args.Reading.AccelerationX;double _accelY = args.Reading.AccelerationY;//Update ellipse location

});

Page 24: Presentation - Windows App Development - II - Mr. Chandan Gupta

#if Complier Conditionals

Page 25: Presentation - Windows App Development - II - Mr. Chandan Gupta

#if Conditional Blocks

#if NETFX_COREDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {#endif

#if WINDOWS_PHONEDeployment.Current.Dispatcher.BeginInvoke(() => {#endif

Page 26: Presentation - Windows App Development - II - Mr. Chandan Gupta

#if Conditional Blocks

Page 27: Presentation - Windows App Development - II - Mr. Chandan Gupta

Threading

#if NETFX_COREDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {#elseDeployment.Current.Dispatcher.BeginInvoke(() => {#endif

double _accelX = args.Reading.AccelerationX;double _accelY = args.Reading.AccelerationY;

Page 28: Presentation - Windows App Development - II - Mr. Chandan Gupta

Extension Methods

Page 29: Presentation - Windows App Development - II - Mr. Chandan Gupta

Web Service

Page 30: Presentation - Windows App Development - II - Mr. Chandan Gupta

HttpWebResponse and HttpWebRequest

var request = (HttpWebRequest)WebRequest.Create(autoCompleteUri);

HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

// retrieve data using StreamReader

Page 31: Presentation - Windows App Development - II - Mr. Chandan Gupta

HttpWebResponse and HttpWebRequest

var request = (HttpWebRequest)WebRequest.Create(autoCompleteUri);request.BeginGetResponse(new AsyncCallback(AutoCompleteCallback), request);}

private void AutoCompleteCallback(IAsyncResult callback){

HttpWebRequest request = (HttpWebRequest)callback.AsyncState;HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callback);// retrieve data using StreamReader

}

Page 32: Presentation - Windows App Development - II - Mr. Chandan Gupta

Extension Methods

public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request){

var taskComplete = new TaskCompletionSource<HttpWebResponse>();request.BeginGetResponse(asyncResponse =>{

HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;

HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);

taskComplete.TrySetResult(someResponse);}, request);

return taskComplete.Task;}

Page 33: Presentation - Windows App Development - II - Mr. Chandan Gupta

HttpWebResponse and HttpWebRequest

#if WINDOWS_PHONEusing MyExtensionClass#endif

var request = (HttpWebRequest)WebRequest.Create(autoCompleteUri);

HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

// retrieve data using StreamReader

Page 34: Presentation - Windows App Development - II - Mr. Chandan Gupta

Translating User

Experience

Page 35: Presentation - Windows App Development - II - Mr. Chandan Gupta

Web Service

Page 36: Presentation - Windows App Development - II - Mr. Chandan Gupta

UI Differences and XAML

Page 37: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 38: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 39: Presentation - Windows App Development - II - Mr. Chandan Gupta

User Experience Considerations

One-handed touch most common

Guaranteed hardware, such as camera and accelerometer

Avoid multiple columns of content

Scroll vertically for more content

Very limited room on the app bar

Hardware back button

No semantic zoom

• Windows 8

One or two-handed touch, mouse

No guarantee of any specific hardware, must check at runtime

Rows and columns of content are normal

Scroll horizontally for more content

Significant room on the app bar

On-screen back button

Semantic zoom

Windows Phone 8

Design the UX separately for each platform!

Page 40: Presentation - Windows App Development - II - Mr. Chandan Gupta

• Avoid reusing XAML across Windows Phone 8 and Windows 8

• Major differences in the platforms make this difficult anyway:–XAML namespaces

–XAML controls

–User experience

–Screen space

–Page layout / orientation

XAML

Page 41: Presentation - Windows App Development - II - Mr. Chandan Gupta

• Windows.UI.Xaml.Controls contains Windows 8 controls• Microsoft.Phone.Controls and Microsoft.Phone.Shell contain WP8

controls• System.Windows.Controls contains Windows 8 controls and some shared

controls• Some controls are present on both platforms but in different namespaces

• Example: Windows.UI.Xaml.Controls.Canvas (Win8), System.Windows.Controls.Canvas (WP8)

XAML Namespaces and Controls

Page 42: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Controls

Page 43: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Controls

Page 44: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Controls

Page 45: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 46: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 47: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 48: Presentation - Windows App Development - II - Mr. Chandan Gupta

Translating UX

Page 49: Presentation - Windows App Development - II - Mr. Chandan Gupta

Translating UX – Details View

Page 50: Presentation - Windows App Development - II - Mr. Chandan Gupta

Different Form Factors Require Different UX

Page 51: Presentation - Windows App Development - II - Mr. Chandan Gupta

• Tiles are an entry point for Windows 8 and Windows Phone 8 apps–One primary tile that launches

the app normally–Also, secondary tiles can be

pinned to the Start screen• Create a “Deep link” that takes

the user to a specific page in the app

• Both platforms support live tiles, in which content is periodically updated

Tiles

Tiles on Windows 8 Tiles on Windows Phone 8

Page 52: Presentation - Windows App Development - II - Mr. Chandan Gupta

var tile = new SecondaryTile(item.UniqueId, // Tile IDitem.ShortTitle, // Tile short nameitem.Title, // Tile display nameitem.UniqueId, // Activation argumentTileOptions.ShowNameOnLogo, // Tile optionsuri // Tile logo URI

);

await tile.RequestCreateAsync();

CycleTileData tileData = new CycleTileData()

{

Title = group.Title,

SmallBackgroundImage = new

Uri(group.GetImageUri(),

UriKind.RelativeOrAbsolute),

CycleImages = list

};

ShellTile.Create(new Uri(navDataSource,

UriKind.Relative), tileData, true);

Both platforms support tiles, but the APIs are completely different

Tiles (cont.)

Windows 8 TilesWindows Phone 8 Tiles

Page 53: Presentation - Windows App Development - II - Mr. Chandan Gupta

• With webcams and cell phones, capturing photos and videos is prolific–Both Windows 8 and Windows Phone 8

have media capture APIs

Media Capture

Windows Phone 8 camera app

Windows 8 camera app

Page 54: Presentation - Windows App Development - II - Mr. Chandan Gupta

Media Capture (Windows 8)• Windows uses CameraCaptureUI to capture images and videos

• Windows.Media.Capture namespace

• Enable Webcam and Microphone in the manifest

private async void OnCapturePhoto(object sender, TappedRoutedEventArgs e)

{

var camera = new CameraCaptureUI();

var file = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (file != null)

{

// Do something with the result...

}

}

Page 55: Presentation - Windows App Development - II - Mr. Chandan Gupta

Media Capture (Windows Phone 8)Windows Phone uses CameraCaptureTask take photos

Microsoft.Phone.Tasks namespace

Enable ID_CAP_ISV_CAMERA and ID_CAP_MICROPHONE in the manifest

Recording video is more complicated, but possible

private readonly CameraCaptureTask cameraTask;public Init() {

cameraTask = new CameraCaptureTask();cameraTask.Completed += PhotoCaptured;

}public void TakePhoto() {

cameraTask.Show();}private async void PhotoCaptured (object sender, PhotoResult result) {

await Task.Run(() => {// Do something with the result...

});}

Page 56: Presentation - Windows App Development - II - Mr. Chandan Gupta

App Bar

The app bar is a good place to put frequently used commands

The Windows 8 app bar has few technical limitations

Certification standards may limit it

Phone has limited screen space

The app bar cannot take up too much space

Put additional commands on the menu

A Windows Phone 8 app bar with the menu expanded

A Windows 8 app bar with three buttons

Page 57: Presentation - Windows App Development - II - Mr. Chandan Gupta

App Bar

Windows Phone 8 App Bar

• One app bar at the bottom of the page

• Only four items allowed

• Put additional items on the menu

• No grouping

• ApplicationBar control inside

PhoneApplicationPage.ApplicationBar

• Set Mode to Default to show the app bar

when the page loads

• Set IsMenuEnabled to enable the menu

Windows 8 App Bar

• Two app bars: one bottom and one top

• Behaves like any container

• No menu

• Can group items in nested containers

• AppBar control inside Page.BottomAppBar or

Page.TopAppBar

• Set IsOpen to true to show the app bar when

the page loads

• Set IsSticky to true to force an app bar to

always remain open

Page 58: Presentation - Windows App Development - II - Mr. Chandan Gupta

App Bar (Windows 8)

<Page.BottomAppBar IsOpen="True"><AppBar x:Name="bottomAppBar" Opened="AppBar_Opened" Padding="10,0,10,0">

<Grid><StackPanel Orientation="Horizontal" HorizontalAlignment="Left">

<Button Style="{StaticResource EditAppBarButtonStyle}" Click="Edit_Click"/><Button Style="{StaticResource RemoveAppBarButtonStyle}" Click="Remove_Click"/><Button Style="{StaticResource AddAppBarButtonStyle}" Click="Add_Click"/>

</StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Right">

<Button Style="{StaticResource RefreshAppBarButtonStyle}" Click="Refresh_Click"/>

<Button Style="{StaticResource HelpAppBarButtonStyle}" Click="Help_Click"/></StackPanel>

</Grid></AppBar>

</Page.BottomAppBar>

Page 59: Presentation - Windows App Development - II - Mr. Chandan Gupta

App Bar (Windows Phone 8)

<phone:PhoneApplicationPage.ApplicationBar><shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Default" Opacity="1.0">

<shell:ApplicationBarIconButton x:Name="btnTakePicture"IconUri="/Assets/Icons/camera.png" Click="btnTakePicture_Click" Text="Take Picture"/>

<shell:ApplicationBarIconButton x:Name="btnShareTask" IconUri="/Assets/Icons/share.png"Click="btnShareShareTask_Click" Text="Share Image"/>

<shell:ApplicationBarIconButton x:Name="btnStartCooking"IconUri="/Assets/Icons/alarm.png" Click="btnStartCooking_Click" Text="Start Cooking"/>

<shell:ApplicationBarIconButton x:Name="btnPinToStart" IconUri="/Assets/Icons/like.png"Click="btnPinToStart_Click" Text="Pin To Start"/>

</shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar>

Page 60: Presentation - Windows App Development - II - Mr. Chandan Gupta

Launching Built-In AppsURI scheme Description

http:[URL] Launches the web browser and navigates to URL

mailto:[email address]Launches the email app and creates a new message.Note that the email is not sent until the user taps send.

ms-settings-accounts: Launches the Account Settings app.

ms-settings-airplanemode: Launches the Airplane Mode Settings app.

ms-settings-bluetooth: Launches the Bluetooth Settings app.

ms-settings-cellular: Launches the Cellular Settings app.

ms-settings-emailandaccounts: Launches the email and accounts settings app.

ms-settings-location: Launches the Location Settings app.

ms-settings-lock: Launches the Lock Screen settings app.

ms-settings-wifi: Launches the Wi-Fi Settings app.

Page 61: Presentation - Windows App Development - II - Mr. Chandan Gupta

Thank You..