25
© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com SELA DEVELOPER PRACTICE JUNE 29 – JULY 3, 2014 Michael Haberman From XAML / C# to HTML5 / JS

XAML/C# to HTML5/JS

Embed Size (px)

DESCRIPTION

My Session on XAML/C# to HTML5/JS

Citation preview

Page 1: XAML/C#  to HTML5/JS

© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com

SELA DEVELOPER PRACTICEJUNE 29 – JULY 3, 2014

Michael Haberman

From XAML / C# to HTML5 / JS

Page 2: XAML/C#  to HTML5/JS

How to transition???Don’t be scared Lets take our knowledge and experience to HTML JSMainly MVVM

Page 3: XAML/C#  to HTML5/JS

The difference Lots of libraries (AngularJS,Jquery, BackBone, Knockout)

Doesn’t compile

Doesn’t have MVVM Features

Page 4: XAML/C#  to HTML5/JS

Lots of libraries – 20 MVVM Frameworks ?!

Page 5: XAML/C#  to HTML5/JS

The difference Lots of libraries (AngularJS,Jquery, BackBone, Knockout)

Doesn’t compile

Doesn’t have MVVM Features

Page 6: XAML/C#  to HTML5/JS

Type Script – lets make it compile!Compiles your Javascript Code!!Compile time errorsIntellisense

Page 7: XAML/C#  to HTML5/JS

The difference Lots of libraries (AngularJS,Jquery, BackBone, Knockout)

Doesn’t compile

Doesn’t have MVVM Features

Page 8: XAML/C#  to HTML5/JS

What MVVM Feature do we want?Data Binding

ConverterModeINotifyPropertyChanged

CommandPassing a parameter

Context Binding

Page 9: XAML/C#  to HTML5/JS

ComparisonXAML Knockout

Property changes Implement INotfiyPropertyChanged

Use KnockoutObservable

Collection changes Implement INotifyCollectionChanged

Use KnockoutObservableArray

Command Implement ICommand Access any method

Convert data Converter Computed

Binding Inline logic X V

Collection Iterate ItemsControl using DataTemplate Foreach binding using template

Access parent/root ViewModel Relative binding using RelativeSource

$parent / $root

Access another UI Element ElementBinding $element

Page 10: XAML/C#  to HTML5/JS

Demo

Lets see how to setup the environment

Page 11: XAML/C#  to HTML5/JS

Setting DataContext

KnockoutC#

var vm = new MySampleAPP.MainViewModel()ko.applyBindings(vm);DataContext = new MainViewModel();

Page 12: XAML/C#  to HTML5/JS

Binding an element

XAML

<TextBlock Text="{Binding FirstName}"/>

Knockout

<input type="text" data-bind="value: FirstName"/>

Page 13: XAML/C#  to HTML5/JS

Visibility bound to ViewModel

KnockoutXAML

data-bind="if: HasLoggedinUser”OR

data-bind="visible: Status() == 'Working'"

Visibility="{Binding HasLoggedinUser, Converter={StaticResource Converter}}"/>

Page 14: XAML/C#  to HTML5/JS

Commands

XAMLKnockout

<Button Command="{Binding ChangeUser}"/><input type="button" data-bind="click: ChangeUser, valueUpdate:'input'"/>

Page 15: XAML/C#  to HTML5/JS

Update Options

XAML Knockout

Default Afterkeydown

PropertyChanged Keyup

LostFocus Keypress

Explicit Input

Change

Page 16: XAML/C#  to HTML5/JS

Iterate through collections - XAML <ItemsControl ItemsSource="{Binding Collection}" > <TextBlock Text="{Binding Name}"/></ItemsControl>

Page 17: XAML/C#  to HTML5/JS

Knockout collections

Like ObservableCollection<T>

public Employees: KnockoutObservableArray<Employee> = ko.observableArray([]);

Page 18: XAML/C#  to HTML5/JS

Iterate thought collections - Knockout<table data-bind="if: HasLoggedinUser"> <tbody data-bind="foreach: Employees"> <tr> <td><span data-bind="text: Name"></span></td> </tr> </tbody></table>

Page 19: XAML/C#  to HTML5/JS

New style converters

Another way to update the UI via ViewModel’s Logic

self.SalaryState = ko.computed(() => {

…return “red”;

});

<span data-bind="text: Salary, style: { color: SalaryState()}" ></span>

Page 20: XAML/C#  to HTML5/JS

Context binding - Knockout

Within foreach access to ViewModel and passing the current item as parameter

<input type="button" value="Delete" data-bind="click: $parent.DeleteEmployee" />

Page 21: XAML/C#  to HTML5/JS

Knockout context bindingSimilar to relative source or element binding:

$parent - $parent[indexer]$root$element

Page 22: XAML/C#  to HTML5/JS

ComparisonXAML Knockout

Property changes Implement INotfiyPropertyChanged

Use KnockoutObservable

Collection changes Implement INotifyCollectionChanged

Use KnockoutObservableArray

Command Implement ICommand Access any method

Convert data Converter Computed

Binding Inline logic X V

Collection Iterate ItemsControl using DataTemplate Foreach binding using template

Access parent/root ViewModel Relative binding using RelativeSource

$parent / $root

Access another UI Element ElementBinding $element

Page 23: XAML/C#  to HTML5/JS

The difference Lots of libraries (AngularJS,Jquery, BackBone, Knockout)

Doesn’t compile

Doesn’t have MVVM Features

Page 24: XAML/C#  to HTML5/JS

SummaryHTML5 + JS + Knockout isn’t scary!Knockout provide an easy transition for XAML developersGo home and try it your self!

Page 25: XAML/C#  to HTML5/JS

Questions