pro business applications with silverlight 5 : [learn how to create ... · summary 21...

16
Pro Business Applications with Silverlight 5 Chris Anderson Apress*

Upload: others

Post on 20-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Pro Business Applicationswith Silverlight 5

Chris Anderson

Apress*

Page 2: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Contents

Contents at a Glance v

About the Author xxii

About the Technical Reviewers xxiii

Acknowledgments xxiv

Introduction xxv

Chapter 1: Getting Started with Silverlight 1

Collecting the Required Tools 1

Visual Studio 1

Expression Blend 5 and SketchFlow 2

Silverlight 5 Tools 2

WCF RIA Services 3

Silverlight Toolkit 3

SQL Server 2008 R2 Express Edition 3

Silverlight Spy (and .NET Reflector) 3

Working with the Silverlight Project Templates 4

Silverlight Application 4

Silverlight Navigation Application 5

Silverlight Business Application 5

Silverlight Class Library 5

WCF RIA Services Class Library .....6

Silverlight Unit Test Application 6

Workshop: Creating a Silverlight Project 6

Exploring the Base Silverlight Business Application 8

Exploring the Initial Silverlight Project Structure 10

Project Links 12

The Web Application Project Structure 14

The Silverlight Application Project Structure 16

Recommended Project Template Modifications 19

Understanding XAP Files 20

Page 3: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Summary 21

Chapter 2: An Introduction to XAML 23

Overcoming XAML's Steep Learning Curve 23

Why Learn XAML? 24

Understanding XAML Syntax, Document Structure, and Features 25

Core XAML Syntax 25

Creating an Object Hierarchy 26

Defining Namespaces 27

Assigning Property Values to Controls 29

Attached Properties 32

XAML Namespace Properties 33

Design-Time Properties 34

Markup Extensions 35

Namescopes 36

Controls 38

Base Control Classes 38

Layout Controls 40

XAML vs. Windows Forms Controls' Property Names 48

Assigning Event Handlers 49

Workshop: Creating a Simple User Interface 51

Working with Resources and Resource Dictionaries 54

Defining and Referencing Resources 54

Resource Dictionaries 55

Applying Styles 56

Defining a Style Resource 57

Applying the Style to a Control 57

Introducing Templates 58

Data Binding 58

Setting the Source of a Binding 59

Workshop: Binding to an Object 61

Binding to a Collection 64

Designing User Experiences 65

Enhancing the Designer/Developer Workflow 66

Designing XAML User Interfaces from a Different Perspective 66

Summary 67

viii

Page 4: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Chapter 3: The Navigation Framework 69

Getting Started with the Navigation Framework 69

Exploring the Components of the Navigation Framework 71

The Frame Control 71

The Page Class 71

The NavigationService Object 72

The NavigationContext Object 72

Navigating Between Views 72

Using View URIs 72

Navigating using the Frame Control 73

Navigating using the NavigationService Object 74

Using a HyperlinkButton Control 74

Using the Source Property of the Frame Control 75

User-Initiated Navigation 75

Passing Data Between Views 75

Passing Data Using Query String Parameters 75

Reading Query String Parameters 76

Passing Complex Data Types Between Views 77

Working with Deep Links 77

Enabling Friendly URIs with URI Mapping 78

Integrating with the Browser History 80

Handling Navigation Events 81

Frame Events 82

View Events 83

Caching Views 83

Workshop: Practicing with Views and Navigation 84

Part 1: Creating a New View 84

Part 2: Passing Data Between Views 87

Part 3: Mapping a URI 88

Applying Visual Transition Effects 89

Creating Custom Content Loaders 91

Working with Alternative User Interface Frameworks 92

Summary 92

Page 5: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Chapter 4: Exposing Data from the Server Using WCF RIA Services 93

Why Expose Data from the Server via Services 93

Creating a Data Access Layer Using the Entity Framework 94

Configuring the Database 94

About the Entity Framework 94

Workshop: Creating an Entity Model 95

WCF Services 96

A Short Overview of WCF 96

The Silverlight-Enabled WCF Service Item Template 96

Supported Bindings 98

Sharing Code and Business Objects Between the Server and Client 99

Workshop: Creating a WCF Service 100

WCF RIA Services 102

What Is WCF RIA Services? 102

An Overview of How to Use WCF RIA Services 103

How the WCF RIA Services Code Generator Works 104

Creating the RIA Services Link 106

Domain Services 108

Domain Operations 111

Decorating Entities 116

Presentation Model Types 126

Sharing Code and Logic Across Tiers 135

Encapsulating Logic in a Separate WCF RIA Services Class Library 136

Handling Errors 141

Handling Data Concurrency Violations 141

Transactions 146

WCF RIA Services Toolkit 147

The WCF RIA Services Contrib Project 147

WCF Data Services 148

Duplex Communication (Pull + Push) 149

Summary 150

Chapter 5: Implementing Summary Lists 151

Communicating Asynchronously 151

Consuming Data from the Server Using WCF Services 152

Workshop: Querying Data Exposed by a WCF Service 152

Common Pitfalls 156

Page 6: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Consuming Data from the Server Using RIA Services 157

Inspecting the Generated Code in the Silveriight Project 157

Consuming Data Declaratively in XAML via the DomainDataSource Control 160

Consuming Data in Code via the Domain Context Class 163

Choosing the Right Approach 166

Specifying Query Criteria 166

Explicitly Specifying a Domain Service Host 169

Handling Load Errors 170

Submitting Changes to the Server Using RIA Services 172

Change Tracking 172

Workshop: Submitting Changes via the DomainDataSource Control 173

Workshop: Submitting Changes via a Domain Context 174

Handling Submit Errors 175

Handling Concurrency Violations 177

HTTP Requests 178

Making an HTTP Request 179

Choosing a Networking Stack 179

Cross-Domain Access Policies 180

Implementing Cross-Domain Policies for HTTP-Based Communication 181

Implementing Cross-Domain Policies for Socket-Based Communication 182

Implementing Cross-Scheme Access Policies 182

Summary 182

Chapter 6: Building Data Entry Forms 185

Creating the Summary List 186

Using the DataGrid Control 186

Using the ListBox Control 193

Choosing the Right Control 200

Populating a Summary List with Data 201

The ObservableCollection<T> Type 202

Collection Views 202

Binding a Summary List to a DomainDataSource Control 206

Binding a Summary List to a View Model Class 207

Manipulating Summary List's Contents 213

Workshop: Filtering the Summary List 213

Workshop: Sorting the Summary List 219

Workshop: Grouping the Summary List 222

Workshop: Paging the Summary List 225

Page 7: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Using the Busylndicator Control 231

Displaying the Busylndicator Control 231

Customizing the Busylndicator Control's Content 232

Modal Behavior 232

The DisplayAfter Property 233

Drilling Down on a Record 233

Navigating to the Details View 233

Opening Details in a Pop-Up Window 235

Displaying Details Using the DataGrid's Row Details 237

Implementing a Master/Details View 238

Summary 239

Chapter 7: Securing Your Application 241

Creating the Data Entry User Interface 241

Laying Out the Data Entry Form 241

Refining the Data Entry Form's Functionality 254

Using the DataGrid for Data Entry 268

Structuring Objects for Use by Data Entry Forms 274

Implementing the INotifyPropertyChanged Interface 274

Implementing the lEditableObject Interface 278

Adding Calculated Properties to Your Classes 281

Data Validation 281

Displaying Validation Errors 282

Types of Data Validation 284

Defining Validation Rules 285

Reporting Validation Errors from Objects 286

Exposing Object-Level Validation Errors 292

Handling Validation Errors in Your View's Code-Behind 292

Executing Validation Rules Implemented as Attributes 293

Customizing Validation Attribute Error Messages 297

Complex Validation Rules,

297

Workshop: Creating a Master/Details Screen 298

Summary 300

Chapter 8: Styling Your Application 301

Implementing Server-Side Security Using RIA Services 301

Using the ASP.NET Membership API 302

xii

Page 8: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Configuring Your Database for Forms Authentication 303

Setting Up Authentication 304

Requiring Authentication 305

Restricting Access to Operations by Role 306

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

Permitting Update/Delete Operations According to Data 308

Exposing Custom User Profile Data 308

Registering Users 309

Avoiding SQL Injection Attacks 310

Sanitizing Errors Sent to the Client 310

Workshop: Practicing with Server-Side Security 311

Implementing Client-Side Security 314

Authenticating and Registering Users 315

Accessing and Updating User Information 317

Implementing Client-Side Restrictions 318

Storing Data Locally 320

Workshop: Practicing with Client-Side Security 320

Encrypting Data Passed Between the Server and the Client 322

Restricting Access to Your Application 323

Summary 325

Chapter 9: Advanced XAML 327

Considering the Designer/Developer Workflow 327

Defining Style Resources 329

Defining a Style Resource 329

Defining Style Resources in the Object Hierarchy 331

Storing Complex Property Values in Styles 333

Naming Style Resources 333

Inheriting Style Resources 334

Styling Constraints 335

Applying Control Templates 335

Using Default Control Templates 335

Templating a Control 336

Structuring Control Templates 337

Theming 340

Creating a Custom Theme 340

Using Silverlight Toolkit Themes 341

xiii

Page 9: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Installing Silverlight Navigation and Business Application ThemedProject Templates 341

Effectively Using Icons and Images 341

Applying Animation 342

Applying Pixel Shaders and Effects 342

Miscellaneous Styling Tips 343

Defining Constants in XAML 343

Finding a Control's Style Definition 344

Applying a Style to a Control Using the XAML Designer 345

Restoring a Control's Default Style 345

Binding in Style Setters 345

Summary 346

Chapter 10: Advanced Data Binding 347

Ignoring Elements and Attributes 347

Comments in XAML 348

Commenting Out Elements 348

Commenting Out Attributes 348

Nesting Comments 349

Defining "Constants" 349

Using OR to Combine Enumerated Values in XAML 350

Triggers, Actions, and Behaviors 351

Using Predefined Triggers and Actions 351

Workshop: Writing Custom Triggers 355

Workshop: Creating and Using Custom Actions 358

Behaviors 360

Which Should I Use? 364

Custom Markup Extensions 364

Creating a Custom Markup Extension 364

Extending the Binding Markup Extension 366

Services 366

Advanced Markup Extension Behavior 368

Workshop: Implementing the Custom Markup Extension 369

Blendability 372

The Design-Time Data Mechanisms 373

Defining the Shape of the Data 373

xiv

Page 10: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Using Sample Data 374

Creating Consolidated Namespaces 375

MoXAML Power Toys 376

XAML Extensions for Visual Studio 2010 377

XAML IntelliSense Presenter 377

XAML Regions 377

XAMLStyler 377

Xaml Formatter 377

Summary 377

Chapter 11: Creating Custom Controls 379

Assigning the Source of a Binding 380

Using the Binding's Source Property 380

ElementName Binding 380

RelativeSource Binding 381

Binding a Control Property Directly to Its Data Context 383

Detecting When the DataContext's Value Has Changed 384

Binding to a Property in the View's Code Behind 385

Instantiating a Class in XAML 386

Creating a Class to Bind To 386

Instantiating the Class as a Resource in XAML and Binding to It 387

Instantiating the Class as the Value of a Control's Property 387

Defining Resources in the Code Behind 388

Binding to Nested Properties 388

Binding to Indexed Properties 389

Binding to Dynamic Properties 389

Enhancing Data Binding 390

String Formatting 390

Alternative Value When a Source Property Value Is Null 392

Fallback Values 392

UpdateSourceTrigger 392

Value Converters 393

Binding Using Property Element Syntax 397

MultiBinding 397

Data Binding in Code 398

Getting and Setting Attached Property Values in Code 398

Page 11: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Additional Tips 399

Visual Studio Data Binding Expression Builder 399

Using the Data Sources Tool Window to Create a Resource and ControlsThat Bind to It 402

Debugging Data Binding Issues 403

Commands 407

Creating a Command 407

Binding to a Command 408

Passing a Parameter to a Command 409

The DelegateCommand 410

Summary 412

Chapter 12: The Model-View-View Model (MWM) Design Pattern 413

Adding Functionality to an Existing Control 413

Creating User Controls 415

Workshop: Creating a Simple User Control 415

Exposing Properties Overview 416

Exposing Methods 433

Exposing Events 433

Determining Whether a Control Is Executing in Design-Time or Runtime Mode 434

Constraining the User Control's Size 434

Creating Custom Controls 434

Workshop: Creating the Base of a Custom Control 435

The Control Structure 436

Defining the Control's Default Template 438

Defining the Control's Behavior 448

Content Controls 454

Containing a Single Control as Content 455

Containing Multiple Controls as Content 457

Attached Properties 458

Summary 460

Chapter 13: An Introduction to Prism and MEF. 461

Benefits of Implementing the MWM Design Pattern 462

MWM Theory 463

The Layers 463

xvi

Page 12: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Connecting the Layers 466

Layer Interaction Summary 469

MWM in Practice 470

Creating the Layers 470

Workshop: Connecting the ViewModel to the View 474

Communicating Between the View and the ViewModel 475

MWM Frameworks 479

Popular MWM Frameworks 479

Should I Use a MWM Framework? 479

Creating a Simple MWM Framework 479

Implementing Common Scenarios with MWM 481

How Do I Assign a Value to a Control's Property? 481

How Do I Enable/Disable Controls? 481

How Do I Show/Hide Controls? 482

How Do I Display a Validation Error in the View? 482

How Do I Add/Remove Items from a List? 482

How Do I Select an Item in a List or Get the Currently Selected Item? 483

How Do I Control the Filtering, Sorting, Grouping, and Paging of Items

Displayed in a List? 485

How Can I Handle Control Events? 485

How Do I Revert a ViewModel to a Previous State? 485

How Do I Prevent the User from Doing Anything While an AsynchronousOperation is in Progress? 485

How Do I Notify the View When an Asynchronous Operation Is Complete? 486

How Do I Set Focus to a Control? 486

How Do I Display a Message Box to the User? 487

How Do I Start a State Transition or Control an Animation? 488

How Can I Pass Complex Objects Between Views? 488

How Do I Navigate to a Different View? 488

How Do I Show a ChildWindow/Dialog? 490

How Do I Close a ChildWindow? 491

How Can My ViewModels Communicate with One Another? 492

How Can I Use My ViewModels at Design Time? 493

How Can I Use RIA Services in a MWM-Friendly Manner? 494

Common MWM Questions 495

Should I implement Dependency Properties or Standard Properties on MyViewModel Class? 495

Should Model Objects Really Be Exposed Directly to the View? 495

Page 13: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Should ViewModels Copy Data to and from the Models? 496

What Should Come First, the View or the ViewModel? 496

Alternative View / ViewModel Configurations 497

One ViewModel Serving Multiple Views 497

One View Interacting with Multiple ViewModels 497

Summary 498

Chapter 14: An Introduction to LightSwitch 501

Terminology 502

Using MEF 503

Key Steps in the Composition Process 503

Managing the Lifetime of Parts 507

Lazy Imports 508

Obtaining New Export Instances on Demand 508

Catalogs 509

Workshop: Getting Started with MEF 511

Workshop: Specifying a Contract 513

Workshop: Importing Multiple Parts 514

Workshop: Configuring Catalogs and Downloading Modules 516

Summary 519

Chapter 15: Printing and Reporting 521

The Importance of Implementing Reporting 521

Potential Solutions for Implementing Reporting 522

Using Silverlight's Printing Functionality 522

Generating a PDF 523

Generating HTML 523

Generating Office Documents 523

Third-Party Report Viewers 524

Perpetuum Software's Report Sharp-Shooter for Silverlight 524

Office Integration Using COM 526

Choosing a Reporting Strategy 526

Using Silverlight's Native Printing Functionality 526

Starting a Print Job 527

Providing the Content for a Printed Page 527

Advanced Print Settings 528

xviii

Page 14: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Workshop: Printing a Screen 529

Workshop: Printing a Report Created in Code 530

Generating and Displaying a PDF Report 533

Generating a Report on the Server 533

Displaying the Report on the Client 543

The PivotViewer Control 552

Key Features of the PivotViewer Control 552

Comparing the Silverlight 4 and Silverlight 5 Versions of the Control 555

Configuring the PivotViewer Control for Use 555

Saving and Restoring the PivotViewer Control's State 558

Uses for the PivotViewer Control 559

Finding More Information on the PivotViewer Control 559

Workshop: Implementing the PivotViewer Control 559

Workshop: Applying an Item Adorner to Trading Cards 561

Summary 562

Chapter 16: Out of Browser Mode and Interacting with the Operating System.... 565

Running Silverlight Applications Out of the Browser 565

Configuring OOB Mode 566

Installing the Application to Run Out of the Browser 568

Determining Installation Status 569

Determining Whether Running Inside or Outside the Browser 570

Where/How Is the Application's .xap File Stored and Run? 570

Interacting with the OOB Window 571

Checking for Updates 572

Debugging in OOB Mode 573

Uninstalling the Application 574

Toast Notifications 574

Caching Data Locally 576

Caching to Isolated Storage 577

Caching to a Client-Side Database 586

Detecting Network Connection Availability 588

File System Open and Save Dialogs 589

The Open File Dialog 589

The Save File Dialog 593

Drag-and-Drop Target 596

Clipboard 598

xix

Page 15: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Running in Full-Screen Mode 599

Initiating Full-Screen Mode 599

Detecting the Switch to and from Full-Screen Mode 600

Retaining Full-Screen Mode When Unfocused 600

Keyboard Access 601

Configuring Elevated Trust 601

Enabling Elevated Trust in OOB Mode 602

Enabling Elevated Trust Inside the Browser 603

Accessing the File System 605

COM Automation 609

P/lnvoke 615

Creating Additional OOB Windows 616

Custom Chrome 617

Other Restrictions Lifted by Elevated Trust 619

Restrictions Imposed by Elevated Trust 619

Enabling/Disabling Elevated Trust Applications with a Windows Group Policy 620

Summary 620

Chapter 17: Application Deployment 621

Deploying the Application to the Server 621

Server Requirements 621

Using Xcopy 623

Workshop: Publishing from Visual Studio 624

Workshop: Creating a Web Package 625

Workshop: Creating a Setup Program 625

Deploying the Application to the Client 626

The Default Silverlight Installation Process 626

Customizing the Silverlight Installation Experience 628

Pushing the Silverlight Runtime to Users in the Enterprise 631

Building a Client Desktop Installer 631

Forcing an Update of the Cached XAP File for New Versions 633

Improving on the Default Application Loading Screen 634

Workshop: Creating the Application Preloader Files 635

Workshop: Designing the Application Preloader's Appearance 636

Workshop: Updating the Application Download Progress 637

Workshop: Configuring the Preloader in the HTML File 638

Testing the Application Preloader 639

XX

Page 16: Pro business applications with Silverlight 5 : [learn how to create ... · Summary 21 Chapter2:AnIntroductiontoXAML 23 OvercomingXAML'sSteepLearning Curve 23 WhyLearnXAML? 24 UnderstandingXAMLSyntax,

Partitioning Your Application 640

Initial Steps 640

Assembly Caching 641

Downloading Modules on Demand 644

Digitally Signing Your Application 650

Why Sign Your Application 650

Obtaining a Code-Signing Certificate 651

Workshop: Signing Your XAP File 652

Summary 655

Index 657

xxi