16
Software Architecture Pattern M-V-VM (Model-View-ViewModel) Friday, July 26, 2013

Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

Embed Size (px)

DESCRIPTION

Bài giới thiệu nhanh về mô hình kiến trúc phần mềm MVVM (Model-View-ViewModel).

Citation preview

Page 1: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

Software Architecture Pattern

M-V-VM(Model-View-ViewModel)

Friday, July 26, 2013

Page 2: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

View

ViewModel

Commands

Data

Binding

Model

Page 3: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)
Page 4: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

Model

public class Person{

public Person() { }public string FirstName { get; set; }public string LastName { get; set; }public string Age { get; set; }

}

Page 5: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

ViewModel

public class MainViewModel : INotifyPropertyChanged{

public event PropertyChangedEventHandler PropertyChanged;private void NotifyPropertyChanged(String propertyName){

PropertyChangedEventHandler handler = PropertyChanged;if (null != handler){

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

}}

Page 6: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

ViewModel

private Person _selectedPerson;public Person SelectedPerson{

get { return _selectedPerson; }set{

if (_selectedPerson != value) {_selectedPerson = value;NotifyPropertyChanged("SelectedPerson");

}}

}

Page 7: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

View

<TextBox Text="{Binding FirstName, Mode=TwoWay}" />

Page 8: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

View

<ListViewItemsSource="{Binding ListOfPerson}"SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" />

Page 9: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

View

ViewModel

Commands

Data

Binding

Model

Page 10: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

• The Mode property determines how changes are synchronized

between the target control and data source– OneTime – Control property is set once to the data value and any subsequent changes

are ignored

– OneWay – Changes in the data object are synchronized to the control property, but

changes in the control are not synchronized back to the data object

– TwoWay – Changes in the data object are synchronized to the control property and vice-

versa

<TextBlock x:Name="FirstName" Text="{Binding FirstName, Mode=OneWay}"/>

Page 11: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

• ViewModels implement INotifyPropertyChanged

public class ProfileViewModel : INotifyPropertyChanged{

private Person _person;public Person Person{

get { return _person; }set {

if (value != _person){_person = value;NotifyPropertyChanged("Person");

}}

}

public event PropertyChangedEventHandler PropertyChanged;private void NotifyPropertyChanged(String propertyName){

if (null != PropertyChanged) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

}}

Page 12: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

<Button Command="{Binding EditCommand}"

CommandParameter="{Binding SelectedPerson}"

Content="Edit" HorizontalAlignment="Center"

VerticalAlignment="Center"/>

class EditPersonCommand : ICommand

{

ViewModel _viewModel;

public EditPersonCommand(ViewModel viewModel)

{

_viewModel = viewModel;

}

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)

{

return true;

}

public void Execute(object person)

{

_viewModel.EditPerson(person as Person);

}

}

Page 13: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

• 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 14: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

http://caliburnmicro.codeplex.com/http://mvvmlight.codeplex.com/

Rob EisenbergLaurent Bugnion

Page 15: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

http://msdn.microsoft.com/en-us/library/gg405484%28v=pandp.40%29.aspx

http://msdn.microsoft.com/en-us/magazine/jj651572.aspx

Page 16: Giới thiệu nhanh về mô hình MVVM (Model-View-ViewModel)

Software Architecture Design

M-V-VM(Model-View-ViewModel)

18/07/2013