tool development 10 - mvvm, tool chains

33
Tool Development Chapter 10: MVVM, Tool Chains Nick Prühs

Upload: nick-pruehs

Post on 19-May-2015

288 views

Category:

Technology


1 download

DESCRIPTION

Chapter 10 of the lecture Tool Development taught at SAE Institute Hamburg. Introduction to the MVVM pattern and advanced data binding concepts such as data conversion and data validation.

TRANSCRIPT

Page 1: Tool Development 10 - MVVM, Tool Chains

Tool DevelopmentChapter 10: MVVM, Tool Chains

Nick Prühs

Page 2: Tool Development 10 - MVVM, Tool Chains

5 Minute Review Session

• What is the difference between Globalization and Localization?

• Name a few globalization best practices!

• What are the main advantages of unit testing?

• What are the most important limits of unit tests?

• Explain the process of Test Driven Development!

• What are the upsides and downsides of TDD?

2 / 58

Page 3: Tool Development 10 - MVVM, Tool Chains

Assignment Solution #9

DEMO

3 / 58

Page 4: Tool Development 10 - MVVM, Tool Chains

Objectives

• To understand the implications of the MVVM pattern

• To get an overview of approaches for tool chains

4 / 58

Page 5: Tool Development 10 - MVVM, Tool Chains

Model-View-Controller

• Architectural pattern that separates data from its visual representation• Model: Data, such as names, phone numbers, or health

points.

• View: Visual representation of that data, such as console output, UI textfields or health bars.

• Controller: Layer that separates model from view, serving as interface for any interaction with the data.

5 / 58

Page 6: Tool Development 10 - MVVM, Tool Chains

Model-View-Controller

6 / 58

Page 7: Tool Development 10 - MVVM, Tool Chains

Model-View-Controller

• Allows exchanging views or modifying the model without breaking existing functionality.• For instance, write console client first and GUI client

after.

• Greatly improves your application architecture through separation of concerns.• Anybody always knows where to look in your code.

7 / 58

Page 8: Tool Development 10 - MVVM, Tool Chains

Model-View-View Model

• Architectural pattern that separates data from its visual representation• Model: Data, such as names, phone numbers, or health

points.

• View: Visual representation of that data, such as console output, UI textfields or health bars.

• View Model: Responsible for exposing data from the model in such a way that those objects are easily managed and consumed

8 / 58

Page 9: Tool Development 10 - MVVM, Tool Chains

Model-View-View Model

9 / 58

Page 10: Tool Development 10 - MVVM, Tool Chains

Model-View-View Model

• Attempts to gain both the advantages of separation of functional development provided by MVC as well as leveraging the advantages of data bindings• binds data as close to the pure application model as

possible

• uses inherent data checking features to validate any incoming data

10 / 58

Page 11: Tool Development 10 - MVVM, Tool Chains

Where Is The Controller?

• Substituted by bindings• Synchronize the view model and view

• Key enablers of the pattern

• Controller sometimes included anyway• Ongoing area of discussion regarding the standardization

of the MVVM pattern

11 / 58

Page 12: Tool Development 10 - MVVM, Tool Chains

Drawbacks of MVVM

• Overkill for simple UI operations

• Can result in considerable memory consumption in very large applications

• Where to put event handlers for e.g. button clicks?

12 / 58

Page 13: Tool Development 10 - MVVM, Tool Chains

Data Conversion

• Sometimes, you might be willing to bind a property to a value of a different type (e.g. Color to Brush)

• WPF allows to you implement the IValueConverterinterface for• changing data from one type to another

• translating data based on cultural information

• modifying other aspects of the presentation

13 / 58

Page 14: Tool Development 10 - MVVM, Tool Chains

Data Conversion

XAML

14 / 58

<Window x:Class="DataConversionExample.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:DataConversionExample"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525">

<Window.Resources>

<local:NotConverter x:Key="NotConverter"/>

</Window.Resources>

<CheckBox

Content="Prohibited"

IsChecked="{Binding Allowed, Converter={StaticResource NotConverter}}">

</CheckBox>

</Window>

Page 15: Tool Development 10 - MVVM, Tool Chains

Data Conversion

C#

15 / 58

class NotConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

bool boolValue;

if (value == null || !bool.TryParse(value.ToString(), out boolValue)) return null;

return !boolValue;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

return new NotImplementedException();

}

}

Page 16: Tool Development 10 - MVVM, Tool Chains

Data Validation

• Most of the time, you need to validate that the user has entered the expected information in the correct format.

• Validation checks can be based on type, range, format, or other application-specific requirements.

• WPF data binding allows you to associate ValidationRules with your bindings.

16 / 58

Page 17: Tool Development 10 - MVVM, Tool Chains

Data Validation

XAML

17 / 58

<Application x:Class="DataValidationExample.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:DataValidationExample"StartupUri="MainWindow.xaml">

<Application.Resources><ControlTemplate x:Key="ErrorLabel">

<StackPanel><Border BorderBrush="Red" BorderThickness="1">

<AdornedElementPlaceholder Name="MyAdorner" /></Border><TextBlock

Foreground="Red"FontSize="8pt"Text="{Binding ElementName=MyAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />

</StackPanel></ControlTemplate>

</Application.Resources></Application>

Page 18: Tool Development 10 - MVVM, Tool Chains

Data Validation

XAML

18 / 58

<Window x:Class="DataValidationExample.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:DataValidationExample"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525">

<TextBox Validation.ErrorTemplate="{StaticResource ErrorLabel}">

<TextBox.Text>

<Binding Path="Name">

<Binding.ValidationRules>

<local:StringNotEmptyValidationRule />

</Binding.ValidationRules>

</Binding>

</TextBox.Text>

</TextBox>

</Window>

Page 19: Tool Development 10 - MVVM, Tool Chains

Data Validation

C#

19 / 58

public class StringNotEmptyValidationRule : ValidationRule

{

public override ValidationResult Validate(object value, CultureInfo cultureInfo)

{

var s = value as string;

if (s == null || string.IsNullOrEmpty(s))

{

return new ValidationResult(false, "Must not be empty.");

}

return ValidationResult.ValidResult;

}

}

Page 20: Tool Development 10 - MVVM, Tool Chains

Data Bind for Unity

DEMO

20 / 58

Page 21: Tool Development 10 - MVVM, Tool Chains

Cancelling Events

• Many of the events in WPF can be cancelled by your code to implement special event handling.

• These events usually are provided twice by WPF, one for the actual event, and one for signaling that the event is about to occur• OnClosing

• OnClosed

21 / 58

Page 22: Tool Development 10 - MVVM, Tool Chains

Cancelling Events

WPF

22 / 58

<Window x:Class="CancelEventExample.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:CancelEventExample"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525" Closing="MainWindow_OnClosing">

Page 23: Tool Development 10 - MVVM, Tool Chains

Cancelling Events

C#

23 / 58

private void MainWindow_OnClosing(object sender, CancelEventArgs e)

{

var result = MessageBox.Show(

"Do you want to save your progress before exiting?",

"Unsaved Data",

MessageBoxButton.YesNo,

MessageBoxImage.Question,

MessageBoxResult.Yes);

if (result == MessageBoxResult.Yes)

{

e.Cancel = true;

// Show save file dialog.

}

}

Page 24: Tool Development 10 - MVVM, Tool Chains

Tool Chains

• Output of one tool is used as input for another• Photoshop -> Sprite Packer, Texture Compression

• Build Server -> Git, MSbuild, Unity, NUnit

• Visual Studio -> Post-build events

• Excel -> VBA

• Localization

• Unity Editor Scripts

24 / 58

Page 25: Tool Development 10 - MVVM, Tool Chains

Tool Chains

DEMO

25 / 58

Page 26: Tool Development 10 - MVVM, Tool Chains

Tool Chains

• Technical Requirements• Command-line parameters

• Non-blocking operation (i.e. must not require user input for proceeding)

• Robust error (code) handling

• User Requirements• Good understanding of file systems and working

directories

• Sometimes: Access to system environment variables

• Sometimes: Understanding of different OS

26 / 58

Page 27: Tool Development 10 - MVVM, Tool Chains

WPF Designer

DEMO

27 / 58

Page 28: Tool Development 10 - MVVM, Tool Chains

Review Session

28 / 58

Page 29: Tool Development 10 - MVVM, Tool Chains

What You Have Learned

Basic development process of good Desktop apps

Importance of good UI and UX design

Developing basic Desktop applications with WPF

How to approach common I/O tasks in .NET

Best practices of file and stream I/O in general

Advanced XML concepts such as XPath and XSLT

Overview of other common text-based file formats

29 / 58

Page 30: Tool Development 10 - MVVM, Tool Chains

What You Have Learned

How to use reflections to your advantage

How to create reactive UIs with worker threads

How to implement an undo/redo stack

How to interact with the Windows shell

How to globalize and localize WPF applications

How to properly set up automatic tests

How to work in teams using GitFlow

30 / 58

Page 31: Tool Development 10 - MVVM, Tool Chains

And now…

Go out and make some tools!

31 / 58

Page 32: Tool Development 10 - MVVM, Tool Chains

References

• Wikipedia. Model View ViewModel. http://en.wikipedia.org/wiki/Model_View_ViewModel, June 23, 2014.

• MSDN. Data Binding Overview. https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx, May 2015.

• MSDN. IValueConverter interface. https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.110).aspx, May 2015.

32 / 58

Page 33: Tool Development 10 - MVVM, Tool Chains

Thank you for your attention!

Contact

Mail

[email protected]

Blog

http://www.npruehs.de

Twitter

@npruehs

Github

https://github.com/npruehs

33 / 58