Применение шаблона проектирования MVVM при разработке...

Preview:

Citation preview

Braintreehttp://www.nrumyantsev.com/

2

3

4

5

Model View

Controller

6

Model View

Presentation Model

(ViewModel)

DataBinding

7

8

9

<DataTemplate x:Key="MapTemplate"><my:Pushpin Location="{Binding Location}" />

</DataTemplate>

...

<my:Map Name="map1"Height="494"HorizontalAlignment="Left"Margin="20,6,0,0"VerticalAlignment="Top"Width="444"Center="{Binding MapCenter, Mode=TwoWay}"ZoomLevel="{Binding MapZoomLevel, Mode=TwoWay}"><my:MapItemsControl x:Name="ListOfItems"

ItemTemplate="{StaticResource MapTemplate}"ItemsSource="{Binding LocationDataCollection}">

</my:MapItemsControl></my:Map>

10

<StackPanel Orientation="Horizontal" Height="22"><Label Width="50"> Options: </Label><Button x:Name="Search" Width="100"

Command="{Binding SearchCommand}" >Search</Button></StackPanel>

11

public class SearchCommand : ICommand{

private ViewModel viewModel;

public SearchCommand(ViewModel viewModelInstance){

viewModel = viewModelInstance;}

#region ICommand Memberspublic bool CanExecute(object parameter){

return true;}

public event EventHandler CanExecuteChanged{

add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }

}

public void Execute(object parameter){

viewModel.Search();}#endregion

}

http://unity.codeplex.com

12

13

IUnityContainer Container = new UnityContainer();

if (ViewModelBase.IsInDesignModeStatic){

Container.RegisterType<IDataService,Design.DesignDataService>();}else{

Container.RegisterType<IDataService, DataService>();}

Container.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager());

14

15

16

17

<phone:PhoneApplicationPagex:Class="MvvmLiteRTM.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:vm="clr-namespace:MvvmLiteRTM.ViewModel"DataContext="{Binding Main, Source={StaticResource Locator}}"shell:SystemTray.IsVisible="True“>

18

<Button Content=“ZoomIn" Height="72" Name=“ZoomInButton"><i:Interaction.Triggers>

<i:EventTrigger EventName="Click"><cmd:EventToCommand Command="{Binding ZoomInCommand}"/>

</i:EventTrigger></i:Interaction.Triggers>

</Button>

19

RelayCommand ZoomOutCommand = new RelayCommand(() =>{

MapZoomLevel--;}

);

20

View

ViewModelView

ViewModel

View

ViewModelMessages

Messages

+ callback

21

private void SendMessageWithCallback(){

Messenger.Default.Send(new NotificationMessageAction<string>("Hello, call me back",SendMessageCallback));

}

private void SendMessageCallback(string message){

...}

22

private NotificationMessageAction<string> _lastMessage;

public void MessageRecipient(){

Messenger.Default.Register<NotificationMessageAction<string>>(this, m =>{

MessageTextBox.Text = m.Notification;_lastMessage = m;

// Execute callback with parameter_lastMessage.Execute(MessageTextBox.Text);

});}

http://ultralightmvvm.codeplex.com/

http://mvvmlight.codeplex.com/

http://catel.codeplex.com/

http://caliburnmicro.codeplex.com/

http://www.japf.fr/silverlight/mvvm/index.html23

24

25

26

27

View Model

View

IFeature

Feature

Injection

28

29

30

http://create.msdn.com

http://silverlight.codeplex.com/

Model-View-ViewModel (MVVM) Explained

WPF Apps With The Model-View-ViewModel Design Pattern - Josh Smith

http://nrumyantsev.com/

me@nrumyantsev.com

31