Www.it-ebooks - Blackball

[Pages:273]THE EXPERT'S VOICE? IN WPF

Pro

WPF and Silverlight MVVM

Effective Application Development with Model-View-ViewModel

Harness the power of WPF and Silverlight to produce clean, testable, maintainable code

Gary McLean Hall

it-

it-

Pro WPF and Silverlight MVVM

Effective Application Development with Model-View-ViewModel

Gary McLean Hall

i it-

Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel Copyright ? 2010 by Gary McLean Hall All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3162-2 ISBN-13 (electronic): 978-1-4302-3163-9 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Nathan Kannan Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick,

Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editors: Mary Behr and Sharon Terdeman Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-, or visit . For information on translations, please e-mail rights@, or visit . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales?eBook Licensing web page at /bulksales. The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at .

ii

it-

For my wife, Victoria

iii it-

Contents at a Glance

Contents ................................................................................................................v About the Author ...................................................................................................x About the Technical Reviewer..............................................................................xi Acknowledgments............................................................................................... xii Introduction .......................................................................................................xiii Chapter 1: Overview of WPF and Silverlight .........................................................1 Chapter 2: DataBinding .......................................................................................21 Chapter 3: Model-View Separation .....................................................................55 Chapter 4: The ViewModel ..................................................................................81 Chapter 5: Events and Commands ....................................................................111 Chapter 6: Validation.........................................................................................129 Chapter 7: Unit Testing......................................................................................145 Chapter 8: Data Access Layer ...........................................................................163 Chapter 9: Application Support .........................................................................185 Chapter 10: Sample Application........................................................................205 Index .................................................................................................................243

iv it-

Contents

Contents at a Glance ............................................................................................iv About the Author ...................................................................................................x About the Technical Reviewer..............................................................................xi Acknowledgments............................................................................................... xii Introduction........................................................................................................ xiii

Chapter 1: Overview of WPF and Silverlight .........................................................1 WPF and Silverlight ......................................................................................................... 1 What Is WPF? .........................................................................................................................................1 What Is Silverlight? ................................................................................................................................5 Differences Between WPF and Silverlight..............................................................................................7 Multi-Target Platforms...........................................................................................................................9 XAML ............................................................................................................................. 13 Declarative User Interfaces..................................................................................................................13 XAML Elements ....................................................................................................................................14 XAML Features .....................................................................................................................................16 User Experience vs. User Interface ............................................................................... 18 Summary....................................................................................................................... 19

Chapter 2: DataBinding .......................................................................................21 The Power of DataBinding with XAML .......................................................................... 21 Dependency Objects ............................................................................................................................21 Dependency Properties........................................................................................................................23 Binding Sources ................................................................................................................................... 26

v

it-

CONTENTS

Binding Modes .....................................................................................................................................28 The DataContext...................................................................................................................................30 Advanced DataBinding .................................................................................................. 31 Binding Parameters .............................................................................................................................31 Data Conversion ...................................................................................................................................35 ObjectDataProvider .............................................................................................................................. 42 Debugging DataBindings......................................................................................................................46 Templating ...........................................................................................................................................49 Summary....................................................................................................................... 53 Chapter 3: Model-View Separation .....................................................................55 Separation of Concerns................................................................................................. 55 Dependencies ...................................................................................................................................... 55 MVVM Alternatives ...............................................................................................................................59 The Model ..................................................................................................................... 66 Encapsulation....................................................................................................................................... 66 Don't Repeat Yourself (DRY).................................................................................................................67 You Ain't Gonna Need It (YAGNI) .......................................................................................................... 69 The Law of Demeter.............................................................................................................................70 Test-Driven Development ....................................................................................................................71 The View ....................................................................................................................... 73 Data Representation ............................................................................................................................ 73 User Input.............................................................................................................................................73 Data Binding.........................................................................................................................................74 Separating Model from View......................................................................................... 75 The Mediator Pattern ...........................................................................................................................75 Other Model-View-X Implementations .................................................................................................77 You Gotta Keep 'Em Separated ............................................................................................................ 78 Summary....................................................................................................................... 79 Chapter 4: The ViewModel ..................................................................................81 First ViewModel ............................................................................................................ 81

vi

it-

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download