MVVM Fundamentals

22

Transcript of MVVM Fundamentals

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 1/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 2/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 3/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 4/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 5/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 6/34

mod pattern

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 7/34

MODEL-VIEW-X

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 8/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 9/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 10/34

with a limited or no code-behin

along with business and validation logic.

The view model then provides data from the model in a form that the view can easily use

The view model also provides impcommands that a user of the application initiates in the view.

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 11/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 12/34

So why MVVM for WPF?

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 13/34

So why MVVM for WPF?

DependencyProperty and DependencyObject

resolved dynamically

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 14/34

If no local value is set, the dependenproperty navigates up the logical tree until it finds a value.

a built-in change notification me

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 15/34

• // Dependency Property

// .NET Property wrapper

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 16/34

Attached Properties

<Canvas> <Button Canvas.Top="20" Canvas.Left="20" Content="Cl

me!"/> </Canvas> 

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 17/34

Notifications for binding to normalproperties

• ObservableCollection<T> Class:

 (implements

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 18/34

public class Person : INotifyPropertyChanged

{private string name;

// Declare the event

public event PropertyChangedEventHandler PropertyChanged;

……………………………………….. 

public string PersonName

{get { return name; }

set {

name = value;

// Call OnPropertyChanged whenever the property is updated OnPropertyChanged("PersonName");

}

}

// Create the OnPropertyChanged method to raise the event

protected void OnPropertyChanged(string name){

PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null){

handler(this, new PropertyChangedEventArgs(name));}

}

}

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 19/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 20/34

the object that invokes a command from the logic that exthe command. This allows for multiple and disparate sourinvoke the same command logic, and it allows the commato be customized for different targets.

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 21/34

indicate whether an available.

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 22/34

ICommand

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 23/34

RelayCommand

<Button Content=“BOO" Height="30" Command="{BindinPath=BOOCommand}"></Button>

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 24/34

How do we do it all?

NO CODE-BEHIND, NO USAGREFFERENCES OF THE ViewModel

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 25/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 26/34

Threading

DON’T FREEZE THE UI 

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 27/34

belongs to a thread

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 28/34

• You can have one thread per Window but in practice, a Wapplication has one single Dispatcher and one thread to hUI (The UI thread/Main Thread).

• Don’t touch the UI from the wrong thread 

• The application can only be responsive (the UI working) wUI thread is free and running the message loop

• Don’t block the UI thread 

• How?

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 29/34

Async Work

• Dispatcher.BeginInvoke – pass a delegate to the method sends a message to the dispatcher and when that messagpicked up, the delegate will be invoked on the UI thread.

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 30/34

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 31/34

• ThreadPool

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 32/34

automaticaevent on the UI thread

BackgroundWorker BackgroundWorker

DoWorkEventHandler

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 33/34

MVVM in a threading scenario

8/10/2019 MVVM Fundamentals

http://slidepdf.com/reader/full/mvvm-fundamentals 34/34

More advanced topics

• Threading

• Unit Testing

• ToolKits