15
Pro Business Applications with Silverlight 4 Chris Anderson Apress*

Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

Pro Business Applicationswith Silverlight 4

Chris Anderson

Apress*

Page 2: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

Contents at a Glance iv

Contents v

About the Author xix

Acknowledgments xx

a Chapter 1: Introduction 1

Who This Book Is For 1

About This Book 2

About the Author 3

Silverlight Overview 4

What Is Silverlight? 4

A Short History of Silverlight 6

What Can Silverlight Bring to Your Business Application? 7

When Should You Not Use Silverlight? 8

Comparing Silverlight to Other Microsoft Platforms 9

Comparison with Adobe Flash/Flex 11

Business Applications Overview 11

Summary 12

Chapter 2: Getting Started with Silverlight 13

Required Tools 13

Visual Studio 13

Expression Blend 4 and SketchFlow 14

Page 3: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

m CONTENTS

Silverlight 4 Tools 15

WCF RIA Services 15

Silverlight Toolkit 15

SQL Server 2008 Express Edition 15

Silverlight Spy (and .NET Reflector) 16

Creating a Silverlight Application 16

Silverlight Application 17

Silverlight Navigation Application 18

Silverlight Business Application 18

Silverlight Class Library 18

WCF RIA Services Class Library '. 19

Silverlight Unit Test Application 19

Running and Exploring the Default Silverlight Business Application Project 19

Exploring the initial Silverlight Project Structure 22

Project Links 24

The Web Application Project Structure 26

The Silverlight Application Project Structure.... 28

Recommended Project Template Modifications 31

XAP Files 32

Summary 33

Chapter 3: An Introduction to XAML 35

Overcoming XAML's Steep Learning Curve 35

Why Learn XAML? 36

XAML Syntax, Document Structure, and Features 37

Core XAML Syntax 37

Creating an Object Hierarchy 38

Namespaces 39

Assigning Property Values to Controls 41

Page 4: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

a CONTENTS

Attached Properties 44

XAML Namespace Properties 45

Design-Time Properties 46

Markup Extensions 47

Namescopes 49

Controls 50

Base Control Classes,

51

Layout Controls 53

XAML vs. Windows Forms Controls' Property Names 57

Assigning Event Handlers 58

Creating a Simple User Interface 60

Resources and Resource Dictionaries 62

Styles 64

Templates 65

Data Binding 65

Binding to an Object 66

Binding to a Collection 69

Designing User Experiences... 70

The Designer/Developer Workflow 71

XAML User Interfaces: A Different Perspective 71

Summary 72

Chapter 4: The Navigation Framework 73

Getting Started with the Navigation Framework 73

Components of the Navigation Framework 75

The Frame Control 75

The Page Class 76

The NavigationService Object 76

The NavigationContext Object 77

Page 5: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Navigating Between Views 77

View URIs 77

Navigation Methods on the Frame Control 78

Navigation Methods on the NavigationService Object 78

Using a HyperlinkButton Control 79

Using the Source Property of the Frame Control 79

User-Initiated Navigation 79

Passing Data Between Views 80

Passing Data Using Query String Parameters 80

Reading Query String Parameters,

81

Passing Complex Data Types Between Views 82

Deep Links 82

URI Mapping to Enable Friendly URIs 83

Integrating with the Browser History 85

Handling Navigation Events 87

Frame Events 87

View Events 88

Caching Views 89

Visual Transition Effects 90

Alternative User Interface Frameworks 91

Summary 91

Chapter 5: Exposing Data from the Server: Using WGF RIA Services 93

WhatlsWCF RIA Services? 94

How the WCF RIA Services Code Generator Works 95

How Do You Use WCF RIA Services? 96

Linking Your Silverlight and Web Projects 97

Creating Your Domain Services 97

Creating Domain Operations on Your Domain Services 97

viii

Page 6: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Consuming the Domain Services 98

Getting Started 98

Creating a Data Access Layer Using the Entity Framework 101

Configuring the Database 101

About the Entity Framework 101

Creating an Entity Model 102

Domain Services 102

Understanding the Domain Service Life Cycle 103

Creating a Domain Service 104

Domain Operations 107

Query Operations 107

Insert/Update/Delete Operations 109

Invoke Operations 111

Custom Operations 111

Decorating Entities 112

Metadata Classes 113

Controlling Client Entity Generation 113

Validation 115

Presentation 118

Miscellaneous Data Annotations 118

Presentation Model Types 119

Creating the Presentation Model Class 120

Populating and Exposing Your Presentation Model Types 121

Updating Your Presentation Model Types 122

Sharing Code/Logic Across Tiers 123

Inspecting the Generated Code in the Silverlight Project 124

Encapsulating Logic in a Separate WCF RIA Services Class Library 125

Handling Server Errors 126

Page 7: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

« CONTENTS

Handling Data Concurrency Violations 127

Methods of Identifying Concurrency Violations 127

Configuring the Entity Model to Checkfor Concurrency Violations 128

Testing Your Solution 129

Resolving Conflicts in the Domain Service 130

Transactions 131

WCF RIA Services Toolkit 132

Alternative Communication Technologies 133

WCF Services 134

WCF Data Services,

137

HTTP Requests 138

Sockets 139

Summary 139

Chapter 6: Implementing Summary Lists 141

Exposing a Summary Collection from the Server 142

Collections and Collection Views 144

ObservableCollection<T> 144

Views 144

Consuming Data from the Server 146

Understanding the Domain Context 146

XAML-Based Approach 146

Code-Based Approach 148

Choosing the Right Approach 149

Explicitly Specifying a Domain Service Host 150

Using the Busylndicator Control 150

Displaying the Summary List 152

Retrieving the Data from the Server 153

Configuring and Customizing a Control for Displaying Data 153

x

Page 8: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

B CONTENTS

Choosing the Right Control 164

Manipulating Summary Lists 165

Querying Data Exposed by a Domain Operation 165

Filtering the Summary List 168

Sorting the Summary List 170

Grouping the Summary List 172

Paging the Summary List 175

Drilling Down on a Record 178

Opening Details in a New View 179

Opening Details in a Pop-Up Window 180

Displaying Details Using the DataGrid's Row Details 183

Implementing a Master/Details View 184

Handling Errors 184

Summary 187

Chapter 7: Building Data Entry Forms 189

Creating the Data Entry User Interface 189

Laying Out the Data Entry Form 189

Refining the Data Entry Form's Functionality 201

Using the DataGrid for Data Entry 212

Structuring Objects for Use by Data Entry Forms 216

Implementing the INotifyPropertyChanged Interface 216

Implementing the lEditableObject Interface 220

Adding Calculated Properties to Your Classes 222

Data Validation 223

Displaying Validation Errors 223

Types of Data Validation 226

Defining Validation Rules 227

Exposing Validation Errors to the User Interface 228

Page 9: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Notifying the User Interface of Object-Level Validation Errors 234

The Validator Class 235

Customizing Validation Attribute Error Messages 238

Submitting Changes to the Server 238

Change-Tracking 238

Submitting Changes via the DomainDataSource Control 239

Submitting Changes via a Domain Context 239

Handling Errors 240

Handling Concurrency Violations 242

Summary 243

Chapter 8: Securing Your Application 245

Implementing Server-Side Security Using RIA Services 245

The ASP.NET Membership API 246

Configuring Your Database for Forms Authentication 247

Authentication 249

Requiring Authentication 250

Role-Based Operation Access Restrictions 251

Returning a Subset of Data Based on the User's ID or Role 253

Permitting Update/Delete Operations According to Data 253

Exposing Custom User Profile Data 253

User Registration 254

Avoiding SQL Injection Attacks 255

Sanitizing Errors Sent to the Client 256

Implementing Client-Side Security 256

Authenticating and Registering Users 256

Accessing and Updating User Information 259

Implementing Client-Side Restrictions 260

Storing Data Locally 262

Encrypting Data Passed Between the Server and the Client 262

xii

Page 10: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Cross-Domain Access Policies 263

Implementing Cross-Domain Policies for HTTP-Based Communication 264

Implementing Cross-Domain Policies for Socket-Based Communication 265

Implementing Cross-Scheme Access Policies 265

Restricting Access to Your Application 266

Summary 268

Chapter 9: Styling Your Application 269

The Designer/Developer Workflow 269

Defining Style Resources 271

Defining a Style Resource 272

Defining Style Resources at Various Locations 273

Naming Style Resources 275

Inheriting Style Resources 276

Styling Constraints 277

Control Templates 277

Default Control Templates 278

Templating a Control 278

Control Template Structure 279

Them'mg 282

Creating a Custom Theme 282

Silverlight Toolkit Themes 2834.

Silverlight Navigation/Business Application Themed Project Templates 283

Icons and Images ,283

Animation 284

Pixel Shaders / Effects 284

Miscellaneous Styling Tips 285

Defining Constants in XAML 285

Restoring a Control's Default Style 286

xiii

Page 11: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

a CONTENTS

Summary. 286

Chapter 10: Advanced XAML and Data Binding 287

Advanced XAML 287

Comments in XAML 287

Defining Constants 288

Using OR to Combine Enumerated Values in XAML 289

Triggers, Actions, and Behaviors 289

Blendability 299

Creating Consolidated Namespaces 301

MoXAML Power Toys 303

Advanced Data Binding 303

Assigning the Source of a Binding 304

Binding to a Resource 308

Defining Resources in the Code-Behind (For Binding To) 311

Binding to Nested Properties 311

Binding to Indexed Properties 312

Binding to Collection Views 312

Enhancing Data Binding 317

Data Binding in Code 323

Getting and Setting Attached Property Values in Code 324

Additional Tips 324

Summary 329

Chapter 11: Creating Custom Controls 331

Adding Functionality to an Existing Control 331

Creating User Controls 332

Creating a Simple User Control 332

Exposing Properties 333

Exposing Methods 345

xiv

Page 12: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

is CONTENTS

Exposing Events 346

Determining If in Design-Time or Runtime Mode 347

Constraining the User Control's Size 347

Creating Custom Controls 347

Creating the Custom Control 348

The Control Structure 349

Defining the Control's Default Template 350

Defining the Control's Behavior 359

Content Controls 365

Containing a Single Control as Content 365

Containing Multiple Controls as Content 367

Attached Properties 369

Summary 370

Chapter 12: The Model-View-ViewModel (MWM) Design Pattern , 373

The Purpose of Implementing MWM 374

Why You Should Implement MWM 375

MWM Theory 376

The Layers 376

View / View Model Configurations 379

MWM in Practice 380

Creating the Layers 380

Connecting the Layers 383

View and ViewModel Interactions 385

Layer Interaction Summary 392

Hotly Debated Implementation Details 393

MWM and RIA Services 395

Implementing the Model 395

Implementing the View Model 396

XV

Page 13: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

a CONTENTS

Implementing the View 399

Frameworks 401

Summary 401

Chapter 13: Printing and Reporting... 403

The Importance of Implementing Reporting 403

Potential Solutions for Implementing Reporting 404

Generating a PDF 404

Generating HTML 405

Generating Office Documents 405

Using Silverligrrt 4 Printing Functionality 405

Third-Party ReportViewers 405

Office Integration Using COM 407

Choosing a Reporting Strategy 407

Printing Functionality in Silverlight ..407

Generating and Displaying a PDF Report 409

Generating a Report on the Server 409

Displaying the Report on the Client 418

Summary 426

a Chapter 14: Out of Browser Mode, and Interacting with the

Operating System 427

Out of Browser 427

Configuring 00B Mode 428

Installing the Application to Run Out of Browser 429

Determining Installation Status 431

Determining Whether Running Inside or Outside the Browser 432

Where/How is the Application's .xap File Stored and Run? 432

Interacting with the OOB Window 433

Checking for Updates 434

xvi

Page 14: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Debugging Out of Browser, 436

Uninstalling the Application 436

Toast Notifications 437

Caching Data Locally 439

Caching to Isolated Storage 439

Caching to a Client-Side Database 449

Detecting Network Connection Availability 451

File System Open/Save Dialogs 451

The Open File Dialog , 452

The Save File Dialog 456

Drag Drop Target 459

Clipboard 460

Full Screen 462

Initiating Full Screen Mode 462

Detecting the Switch to and from Full Screen Mode 463

Retaining Full Screen Mode When Unfocused 463

Keyboard Access 464

Elevated Trust 464

Enabling Elevated Trust 465

File System Access 466

COM Automation 471

Custom Chrome 478

Other Restrictions Lifted By Elevated Trust 480

Restrictions Imposed by Elevated Trust 481

Disabling Elevated Trust Applications with a Windows Group Policy 481

Summary 482

i Chapter 15: Application Deployment 483

Deploying the Application to the Server,

483

Page 15: Business Applications Silverlight - gbv.de · Contentsat a Glance iv Contents v AbouttheAuthor xix Acknowledgments xx aChapter 1: Introduction 1 WhoThis BookIs For 1 AboutThis Book

CONTENTS

Server Requirements 484

Using Xcopy 485

Publishing 486

Creating a Web Package 487

Creating a Setup Program 488

Deploying the Application to the Client 488

The Default Silverlight Installation Process 489

Customizing the Silverlight Installation Experience 490

Pushing the Silverlight Runtime to Users in the Enterprise 493

Building a Client Desktop Installer 494

Improving on the Default Application Loading Screen 495

Creating the Application Pre-loader Files 496

Designing the Application Pre-loader 497

Updating the Application Download Progress 499

Configuring the Application Pre-loader in the HTML File 500

Testing the Application Pre-loader 500

Partitioning Your Application 502

Initial Steps 502

Assembly Caching 502

Downloading Modules on Demand 505

Digitally Signing Your Application 506

Summary 511

H Index , 513

xviii