In Windows 8 Store Applications

Slides:



Advertisements
Similar presentations
MVVM Overview Frank Shoemaker MindCrafted Systems
Advertisements

Module 1: Creating an Application by Using Windows Presentation Foundation Overview of WPF Creating a Simple WPF Application Handling Events and Commands.
An Introduction to the Model-View-ViewModel Pattern Srsly? Another MV* Pattern? Srsly? Another MV* Pattern?
Dinko Jakovljević Microsoft Student Partner | BambooLab
WinRT Apps
Programming Based on Events
Web Development Using ASP.NET CA – 240 Kashif Jalal Welcome to week – 3-1 of…
Real world Windows Phone development Igor
Object-Oriented Analysis and Design
Building Silverlight Applications Using the MVVM pattern An Introduction by Peter Messenger Senior Developer – Qmastor
© 2014 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg.
Linq-to-Entities, Data Persisters, Master Datail Doncho Minkov Telerik School Academy Technical Trainer
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
.NET Database Technologies: Introduction to WPF and Entity Framework DataBinding.
Doncho Minkov Telerik School Academy academy.telerik.com Technical Trainer
CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last.
Target: dependency property Source: any public property CLR and WPF properties Target: dependency property Source: any public property CLR and WPF properties.
Windows Presentation Foundation. Agenda Introduction Developing Applications WPF and WF interoperability Custom Controls Styles and Templates Data Binding.
John Daintree Chief Architect Dyalog Windows Presentation Foundation.
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
Getting Started with Caliburn.Micro and Windows Phone 7 Gary Ewan Park Twitter: Blog:
Basic WPF Controls Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
LAYOUT CONTROLS. XAML Overview XAML : eXtensible Application Markup Language pronounced (ZAMEL) is a markup language used to design user interfaces XML-based.
Object Oriented Software Development 9. Creating Graphical User Interfaces.
Windows Phone MVVM and Unit Testing Step by Step Andy Wigley Windows Phone Development MVP, Mobile Software Consultant APPA Mundi Ltd WPH208.
Object Oriented Software Development 10. Persistent Storage.
Svetlin Nakov Telerik Corporation
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
TRAINING SESSIONS.NET Controls.  Standard Controls  Label  Textbox  Checkbox  Button, Image Button, Image control  Radio Button  Literal  Hyperlink.
Nikhil Kothari Software Architect Microsoft Corporation Session Code: WUX312.
ControlTemplate and DataTemplate Doncho Minkov Telerik School Academy Technical Trainer
Module 3 Designing and Developing a User Interface.
Windows 10 UWP MVVM In Depth
Module 4 Taking Control of the User Interface. Module Overview Sharing Logical Resources in an Application Creating Consistent User Interfaces by Using.
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Model View ViewModel Architecture. MVVM Architecture components.
Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th
Programming with MVVM Miguel A. Castro Architect -
/DEV/TM #1 Building Cross-Platform Apps with Xamarin and MvvmCross Flavius-Radu DEMIAN.
Part of the Microsoft.NET Framework 3.0 Tomer Shamam.NET Technologies Expert Sela Group
Data Binding, Binding Properties Doncho Minkov Telerik School Academy Technical Trainer
WPF Course Program, Evaluation, Exams Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Adam Schultz MVVM and WPF. MVVM Model, View, ViewModel A software architecture designed to separate out User Interface design, Business Logic, and Data.
State of the Art in Mobile Development - AndRES Käver, 2016
Real world Windows Phone development
Computing with C# and the .NET Framework
Ben Riga 02 | Basics of View Models Ben Riga
Avalon – Using Data In Your Applications
Build Windows 10 UWP MVVM Apps with Prism
Jim Fawcett CSE775 – Distributed Objects Spring 2011
XAML User Interface Creation in C#
Miguel A. Castro Architect IDesign
WPF AKEEL AHMED.
Introduction to the MVVM Pattern
Resources & Controls AKEEL AHMED.
An introduction to MVVM using WPF NISCHAL S
XAML Deep Dive for Windows & Windows Phone Apps Jump Start
Tech Ed North America /1/2018 4:27 PM Required Slide
Lists and List Items in Windows Runtime Apps
Page Navigation and Data Binding in Windows Runtime Apps
Chapter 8 Classes User-Defined Classes and ADTs
Web Development Using ASP .NET
TechEd /4/2019 3:19 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
XAML Deep Dive for Windows & Windows Phone Apps Jump Start
CSE775 DO supporting slides for VRTS Client Demo
Bringing existing managed code into Metro style apps
02 | Building Windows Store Apps with XAML Part 2
An Introduction to the Model-View-ViewModel Pattern
Presentation transcript:

In Windows 8 Store Applications Data Binding In Windows 8 Store Applications Doncho Minkov Senior Technical Trainer http://minkov.it Telerik Software Academy http://academy.telerik.com

Table of Contents Data Binding Concepts Simple Data Binding List Data Binding Look-up bindings INotifyPropertyChanged Behavior Binding

Data Binding Concepts

Data Binding Concepts Data binding is pulling information out of an object into another object or property Data binding means automatically change the value of a property when the value of another property is changed Many Windows applications are all about data Data binding is a top concern in a user interface technologies like WPF, WinRT or SL The XAML platform provides very powerful data binding mechanisms

Start from the Simple stuff Data Binding Start from the Simple stuff

Data Binding Binding a property to a data source property: The shorthand binding syntax: Binding between the Text property of the TextBox and an object called PropertyName PropertyName is a property of some object to be named later <TextBox ...> <TextBox.Text> <Binding Path="PropertyName" /> </TextBox.Text> </TextBox> <TextBox Text="{Binding PropertyName}" />

* 1/14/201807/16/96 Data Contexts (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Data Contexts In XAML every FrameworkElement and every FrameworkContentElement has a DataContext property DataContext is an object used as data source during the binding, addressed by binding Path If you don’t specify a Source property The XAML binding engine goes up the element tree in searching of a DataContext

Data Contexts (2) Two controls with the same logical parent can bind to the same data source Providing a DataContext value for both of the text box controls <!-- AppPage.xaml --> <Grid Name="GridMain"> … <TextBlock …>Name: </TextBlock> <TextBox Text="{Binding Path=Name}" … /> <TextBlock …>Vendor:</TextBlock> <TextBox Text="{Binding Path=Vendor}" … /> <Button Content="Purchase!" … /> </Grid>

Data Contexts (3) Setting an object as a value of the grid’s DataContext property in the AppPage constructor: public partial class AppPage : Page { Game game = new Game("StarCraft", "Blizzard"); public AppPage() InitializeComponent(); GridMain.DataContext = game; } …

Data Contexts Live Demo * 1/14/201807/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

List Data Binding

List Data Binding Windows 8 Store apps support a range of controls, used to visualize collections of data GridView, ListView, ComboBox, ItemsControl, ListBox, etc… Binding to items controls is done pretty much the same as to regular controls Instead of binding the Text property, bind the ItemsSource property

List Data Binding Binding a ComboBox to a collection of data: public class AppViewModel { public IEnumerable<GameViewModel> Games { get; set; } } ViewModel Set either the DisplayMemberPath <ListView ItemsSource="{Binding Games}" DisplayMemberPath="Name"/> View Or the ItemTemplate <ListView ItemsSource="{Binding Games}"> <ListView.ItemTemplate> … </ListView.ItemTemplate> </ListView> View

List Data Binding Live Demo

Observable Collections Items controls bind easily to collection of data Yet changes to the collection does not reflect to the binding Nothing alerts the data binding engine about changes in the ViewModel Observable collection do the trick An observable collection has a "update" event The data binding engine registers for the event Any change to the collection raises the event

Observable Collections: Sample Use ObservableCollection<T> field Expose IEnumerable<T> public property: public class AppViewModel { private ObservableCollection<GameViewModel> games; public IEnumerable<GameViewModel> Games get { return this.games; } set { this.games = value; } }

Observable Collections Live Demo

Look-up Bindings

Look-up Bindings Look-up bindings provide a way to bind elements to each other Bind a game's vendor to the selected item from a ComboBox <TextBox Text="{Binding Path=NewGame.Name, Mode=TwoWay}" <ComboBox ItemsSource="{Binding Vendors}" SelectedItem="{Binding Path=NewGame.Vendor, Mode=TwoWay}"/>

Look-up Bindings

INotifyPropertyChanged

INotifyProperyChanged The INotifyPropertyChanged interface introduces an event to notify about changes The PropertyChanged event The View attaches to the PropertyChanged event When the ViewModel calls PropertyChanged, the View knows to update its bindings

INotifyProperyChanged (2) The INotifyPropertyChanged interface contains only one event PropertyChanged(object sender, PropertyChangedEventArgs e) The point of this event is to be called when the data is changed Both Model and ViewModel should implement this interface In small project only the ViewModel can implement it

Implementing INotifyPropertyChanged The View automatically subscribes for the PropertyChanged event public class ViewModelBase: INotifyPropertyChanged { PropertyChanged(object sender,PropertyChangedEventArgs e) OnPropertyChanged(string propertyName) { if(this.PropertyChanged!=null) { var args=new PropertyChangedEventArgs(propertyName); PropertyChanged(this, args); } } }

INotifyPropertyChanged Live Demo

Behavior Binding

Abstracting the behavior of a ViewModel Behavior Binding Abstracting the behavior of a ViewModel

Behavior Binding WPF, Silverlight and WinRT does not provide a way to bind an event to a method in a DataContext/ViewModel No way to bind the Click event to a method All events are handled in the Code-behind Handling of events in the code-behind is not the XAML way Events somehow need to be handled in the ViewModel/DataContext

Behavior Binding XAML does not provide a way to bind an event to a behavior(method) in the DataContext Yet it does provide a way to bind to a property of the context The behavior need to be abstracted into an object Here comes the Command Design Pattern

The Command Design Pattern Abstract behavior into objects

The Command Design Pattern The Command Design Pattern wraps behavior into a Command Object Commands have method Execute, that invokes the given behavior In WPF, SL or WinRT, just implement the ICommand interface The ICommand interface provides: Execute() method CanExecute() method CanExecuteChanged EventHandler

ICommand Members ICommand has the following members: Method Execute() Invokes the abstracted behavior Method CanExecute() Validates if the command can be invoked

ICommand Implementation Two common ways to implement Icommand Create an inheritor for every command needed This is kinds of hard, time consuming and can escalate to spaghetti code Create a single implementation that gets its behavior Something like a "generic" command Gets its behavior from the constructor The so called Relay/Delegate Command

The DelegateCommand

The Delegate Command The DelegateCommand contains a behavior passed into creation //in PhonesStoreViewModel.cs public ICommand Next { if(this.nextCommand == null) this.nextCommand = new DelegateCommand(()=>{ …}); // new DelegateCommand(this.HandleNextCommand); } return this.nextCommand; Since now the behavior is wrapped in a object, the View can bind to it

DelegateCommand Live Demo

Data Binding http://academy.telerik.com

Homework Develop a Windows 8 Store application for the game Tic-Tac-Toe Use the MVVM design pattern Use data and behavior binding Develop a Windows 8 Store application for trivia. When the users starts a game, they get exactly 10 questions. The user can choose to get questions for a particular category, or from all categories Each question have a single correct answer and many wrong answers Questions are stored in an XML file