microsoft visual studio 2012 unleashed - gbv · 2013-10-15 · microsoft visual studio 2012...

15
Mike Snell Lars Powers Microsoft" Visual Studio" 2012 UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA

Upload: others

Post on 06-Jul-2020

9 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Mike Snell

Lars Powers

Microsoft"

Visual Studio"2012

UNLEASHED

800 East 96th Street, Indianapolis, Indiana 46240 USA

Page 2: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Table of Contents

Introduction 1

Who Should Read This Book? 2

How Is This Book Organized? 3

Part I: An Introduction to Visual Studio 2012 3

Part II: An In-Depth Look at the IDE 3

Part III: Writing and Working with Code 3

Part IV: Extending Visual Studio 3

Part V: Creating Enterprise Applications 3

Conventions Used in This Book 4

Part I An Introduction to Visual Studio 2012

1 A Quick Tour of Visual Studio 2012 5

The Visual Studio Product Line 6

Express Editions 7

Professional Edition 8

Premium 9

Ultimate 9

MSDN 9

Related Tools 10

Languages, Frameworks, and Application Templates 14

Programming Language Choices 14

The .NET Framework(s) 16

The Many Faces of a .NET Application 16

Developing Rich(er) Clients 19

Windows (WinForms) 19

Windows Presentation Foundation 20

Office-Based Solutions 22

Creating Web Clients 26

BuildingWebsiteswithASP.NET 26

Adding Richer Features with AJAX 30

Developing for Silverlight 31

Making the Client Choice 34

Coding for the Cloud 35

Creating a Cloud Application 36

Running a Cloud Application 36

Publishing to the Cloud 38

Page 3: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Microsoft Visual Studio 2012 Unleashed

Working with Data 38

Design Your Data 39

Separate Data Design from Storage Schema 40

Build Occasionally Connected Applications 41

Write Connected, Service-Oriented Solutions 42

Develop an Application/Business Process 43

Create and Consume Services 45

Summary 47

2 The Visual Studio IDE 49

Installing Visual Studio 49

Choosing a Language 50

Configuring Your Development Environment Settings 51

Getting Started 54

Startup Options 55

Creating Your First Project 56

Targeting Your Environment 56

Navigating the IDE 58

The Menus 59

The Many Toolbars 65

Customizing Toolbars 66

The Solution Explorer 67

The Text Editors 68

The Visual Designers 72

The Toolbox 73

The Properties Window 75

Managing the Many Windows of the IDE 75

Pinning 76

Docking 76

Navigating IDE Windows 78

Customize Your Font 80

Summary 81

3 The .NET Languages 83

Language Primer 83

Programming Objects 84

Types, Variables, and Constants 99

Understanding Operators 103

Making Decisions and Branching Code 106

Looping 109

Page 4: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents vii

Working with Groups of Items Ill

Programming with Attributes 115

Creating and Raising Events 117

Language Features 121

Infer a Variable's Data Type Based on Assignment 121

Create an Object and Set Its Properties with a Single Line of Code 124

Define a Collection and Initialize Its Values 125

Creating an Instance of a Nonexistent Class 126

Add Methods to Existing Classes 127

Add Business Logic to Generated Code 129

Access and Query Data Using the .NET Languages 131

Write Simple Unnamed Functions Within Your Code 132

Splitting an Assembly Across Multiple Files 134

Working with XML Directly Within Your Code (VB Only) 135

Removing Unused Arguments from Event Handlers (VB Only) 136

Creating an Automatically Implemented Property 136

Dropping the Underscore in VB for Line Continuation (New) 137

Working with Dynamic Languages/Objects (New) 137

Covariance and Contravariance (New) 143

Asynchronous Programming 144

The .NET Framework 147

A Map to the .NET Framework 147

Summary 149

Part II An In-Depth Look at the IDE

4 Solutions and Projects 151

Understanding Solutions 151

Creating a Solution 152

Working with Solutions 157

Getting Comfortable with Projects 164

Creating a Project 164

Working with Project Definition Files 167

Working with Projects 174

Summary 179

5 Browsers and Explorers 181

Leveraging the Solution Explorer 181

Visual Cues and Item Types 182

Interacting with Items 185

Inspecting Objects 189

Page 5: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Microsoft Visual Studio 2012 Unleashed

1 Q1Class View 1 y 1

Toolbar 191

Search Bar 193

Objects Pane 193

Members Pane l9^

Server Explorer l9^

Data Connections 197

Server Components 1"

Object Browser 202

Changing the Scope 203

Browsing Objects 203

Document Outline 205

Editing Elements 207

Summary 208

6 Introducing the Editors and Designers 209

Getting Started with the Basics 209

The Text Editor 210

Visual Studio Designers 212

Coding with the Code Editor 213

Opening an Editor 213

Writing Code in the Code Editor 214

Anatomy of the Code Editor Window 215

Code Navigation Tools 218

Searching Documents 221

Debugging in the Text Editor 227

Printing Code 231

Using the Code Definition Window 231

Creating and Editing XML Documents and Schema 233

Inferring Schema 234

Designing XML Schemas 234

Editing XSLT Style Sheets 237

Working with Cascading Style Sheets 238

Adding Style Rules238

Defining Style Sheet Attributes 239

Developing Windows Client Applications 239

Creating a Windows Forms Project 239

Creating a Windows Presentation Foundation Project 248

Developing Web Forms251

Designing a Web Form Application 251

Page 6: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents ix

Authoring WinForms Components and Controls 257

Creating a New Component or Control 257

Further Notes on Writing Component Code 259

Creating Classes with the Class Designer 260

Creating a Class Diagram 260

Adding Items to the Diagram 262

Defining Relationships Between Classes 263

Defining Methods, Properties, Fields, and Events 266

Summary 268

7 The .NET Community: Interacting Online 269

The Community Features of Visual Studio 269

The Visual Studio Start Page 270

Accessing Help 277

Manage Your Help Settings 278

Using the Visual Studio Documentation 279

MSDN Forums 280

Reporting a Bug and Other Customer Feedback Options 287

Samples 289

Discovering and Consuming Shared Content 291

Examining Shared Content Types 291

Finding the Right Content 292

Installing and Storing Shared Content 293

Participating in the Community 294

Starter Kits Versus Templates 294

Creating a Project Template 295

Creating Item Templates 301

Packaging Your Creation for Wider Distribution 302

Summary 314

Part III Writing and Working with Code

8 Working with Visual Studio's Productivity Aids 315

Basic Aids in the Text Editor 317

Change Tracking 317

Coding Problem Indicators 318

Active Hyperlinking 319

Syntax Coloring 319

Outlining and Navigation 320

Code Outlining 320

Tag Navigation 323

Page 7: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Microsoft Visual Studio 2012 Unleashed

Smart Tags and Smart Tasks 325

II I MI. Designer 325

Windows Forms Designer 325

Code Editor 326

IntelliSense 327

Complete Word 328

Quick Info 330

List Members 331

Parameter Info 331

Organize Usings 332

Code Snippets and Template Code 332

Brace Matching 342

Customizing IntelliSense 343

The Task List 345

User Tasks 345

Shortcut Tasks 345

Comment Tasks 346

Summary 348

9 Testing Code 349

Unit Testing Basics 350

Creating a Test Project 350

Writing a Unit Test 353

Running Your Tests 356

Viewing Test Results 356

The Unit Testing Framework 359

The Testcontext Class 359The Test Attribute Classes 360

Unit Test Setup and Teardown 365

The Assert Classes366

Testing Your Exceptions 368

Creating Data-Driven Unit Tests 368Writing ASP.NET Hosted Unit Tests 374

Defining ASP.NET Attributes 374

Configuring Your Test Project Host 375

Creating Ordered Tests376

Summary 377

Page 8: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents xi

10 Refactoring Code 379

Visual Studio Refactoring Basics 380

Invoking the Refactoring Tools 381

Previewing Changes 384

Renaming Code 385

Accessing the Rename Operation 386

Working with the Rename Dialog Box 387

Extract Method 389

Accessing the Extract Method Refactor 389

Extracting Methods 390

Generate Method Stub 396

Extract Interface 396

Accessing the Extract Interface Refactor 397

Extracting Interfaces 398

Refactor Parameters 400

Remove Parameters 400

Reorder Parameters 402

Encapsulate Field 403

Accessing Encapsulate Field 403

The Encapsulate Field Dialog Box 403

Summary 404

11 Debugging Code 407

Debugging Basics 408

The Scenario 408

The Many Phases of Debugging 408

Debugging the Application (Self-Checking) 409

Debugging Basics Summary 418

The Visual Studio Debugger 419

The Debug Menu and Toolbar 419

Debug Options 426

Stepping In, Out, and Over Code 427

Indicating When to Break into Code 432

Working with Tracepoints (When Hit Option) 441

Viewing Data in the Debugger 442

Using the Edit and Continue Feature 448

Advanced Debugging Scenarios 449

Remote Debugging 449

Debugging WCF Services 450

Debugging Multithreaded Applications 451

Page 9: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Microsoft Visual Studio 2012 Unleashed

Debugging Parallel Applications 456

Debugging a Client-Side Script 461

Debugging Crash Information (Dump Files) 463

Summary4(>6

12 Deploying Code 467

An Overview of Client Deployment Options 467

Introducing ClickOnce Deployments 468

Introducing Windows Installer and InstallShield

Deployments468

Publishing a Project with ClickOnce 469

Publishing a Project with InstallShield Limited Edition 472

Application Information 474

Installation Requirements 475

Application Files 476

Application Shortcuts 478

Application Registry 479

Installation Interview 480

Publishing an ASP.NET Web Application 481

Creating a Profile 482

Configuring a Connection 483

Configuring Deployment Settings 484

Previewing the Publication 486

Summary 487

Part IV Extending Visual Studio

13 Introducing the Automation Object Model 489

An Overview of the Automation Object Model 490

Object Model Versions 490

Automation Categories 492

The DTE/DTE2 Root Object 493

Solution and Project Objects 494

Controlling Projects in a Solution 497

Accessing Code Within a Project 498

Working with Windows 501

Referencing Windows 501

Interacting with Windows 502

Text Windows and Window Panes 506

The Tool Window Types 508

Linked Windows 517

Page 10: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents xiii

Command Bars 519

Documents 523

Text Documents 525

Command Objects 538

Executing a Command 540

Mapping Key Bindings 541

Debugger Objects 542

Automation Events 542

14 Writing Add-lns and Wizards 545

Creating Your First Add-In Project 546

Setting Add-In Parameters 547

The Structure of an Add-In 554

The Lifecycle of an Add-In 554

Reacting to Commands 560

Managing Add-lns 562

A Sample Add-In: Color Palette 563

Getting Started 564

Creating the User Control 564

Finishing the Connect Class 569

Exposing Add-In Settings 571

Creating a Visual Studio Wizard 588

Examining the Wizard Structure 589

Creating an Add New Item Wizard 592

Summary 597

15 Extending the Code Editor Using Managed Extensibility Framework 599

The Extensibility Problem 599

Creating Dynamic Applications 600

MEF Architecture 600

MEF Principles 601

Working with MEF 601

The Visual Studio Editor and MEF 603

Editor Extension Points 603

Using the Visual Studio SDK 604

Managing Extensions and Updates 611

Creating Your Own MEF-Based Editor Extension 612

Summary 622

Page 11: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

xiv Microsoft Visual Studio 2012 Unleashed

Part V Creating Enterprise Applications

16 Creating ASP.NET Form-Based Applications 623

The Basics of an ASP.NET Website 624

Creating a New Website or Web Application 624

Controlling Project Properties and Options 634

Creating Web Pages 641

Designing Your User Interface 650

Determining Page Layout and Control Positioning 652

Creating a Common Look and Feel 656

Creating a User-Configurable UI 675

Working with the ASP.NET Controls 687

An Overview of the ASP.NET Controls 688

The ASP.NET Standard Controls 689

Validation Controls 692

The Login Controls 694

Site Navigation Controls 697

Data Controls 698

User Controls 700

Building a Richer UI with ASP.NET AJAX 703

The ASP.NET AJAX Extensions 704

Creating a Page That Supports Partial Update 706

The ASP.NET AJAX Library/Control Toolkit 713

Summary 722

17 Building Websites with Razor and ASP.NET MVC 725

Building ASP.NET Web Pages with Razor 725

Creating a Razor Website 726

A Look at the Razor Syntax 729

Building a Razor Page 730

Creating ASP.NET MVC Websites 739

Understanding the MVC Pattern 739

Creating an MVC Project 742

Adding Features to an ASP.NET MVC Application 748

Creating the Model 748

Creating a Controller 750

Returning a List of Customers 751

Returning a Single Customer 751

Page 12: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents xv

Editing and Creating Customers 752

The ActionResuit Objects 753

Creating Views 754

Summary 756

18 Building Windows Forms Applications 757

The Basics of Form Design 757

Considering the End User 758

Understanding the Role of UI Standards 759

Planning the User Interface 760

Creating a Form 761

The Windows Forms Application Project Type 761

Form Properties and Events 762

Adding Controls and Components 764

Control Layout and Positioning 766

Using Containers 770

Control Appearance and Behavior 774

Working with ToolStrip Controls 775

Displaying Data 783

Creating Your Own Controls 786

Subclassing an Existing Control 787

Designing a User Control 788

Creating a Custom Control 789

Summary 791

19 Creating Richer, Smarter User Interfaces 793

The Windows Presentation Foundation Platform 793

Programming Model 795

Introducing the WPF Designer 799

XAML and Design Panes 799

Programming with WPF 804

Layout 804

Styles and Templates 812

Data Binding 815

Routed Events 817

Building a Simple Image Viewer Application 818

Starting the Layout 819

Storing the Images 825

Binding to the Images 827

Button Event Handlers and Image Effects 828

Path Selection with a Common Dialog Box 830

Summary 837

Page 13: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Microsoft Visual Studio 2012 Unleashed

20 Working with Databases 8,i3

Creating Tables and Relationships 840

Creating a New SQL Server Database 841

Defining Tables 843

Using the Database Diagram Designer 846

Working with SQL Statements 851

Writing a Query 851

Creating Views 855

Developing Stored Procedures 855

Creating Triggers 859

Creating User-Defined Functions 859

Using Database Projects 861

Creating a Database Project 862

Changing the Database 865

Building and Deploying 867

A Note on Data Tier Applications 867

Creating Database Objects in Managed Code 868

Creating a Stored Procedure in C# 868

Binding Controls to Data 872

An Introduction to Data Binding 872

Autogenerating Bound Windows Forms Controls 874

Editing Typed Data Sets 879

Manually Binding Windows Forms Controls 881

Data Binding in WPF Applications 885

Object Relational Mapping 893

An Overview of LINQ 894

Mapping Using the O/R Designer 895

LINQ Code 898

Working with the Entity Framework 901

Querying Against the Entity Data Model 906

Summary 908

21 Service-Oriented Applications 911

Service Fundamentals 912

Why ASP.NET Web API and WCF 913

Key Web Service Terms 915

Building HTTP Services Using the ASP.NET Web API 916

Creating an ASP.NET Web API Project 916

Understanding Service URIs and Routing 923

Consuming an ASP.NET Web API Service 924

Page 14: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

Contents xvii

WCF Service Applications 929

The WCF Project Template 930

Creating a WCF Service 933

Consuming a WCF Service 938

Hosting and Deploying a WCF Service 941

Summary 943

22 Embedding Workflow in Your Applications 945

Windows Workflow Fundamentals 946

The Components of a Workflow 946

The Workflow Project Templates 948

The Workflow Designer 949

The Workflow Item Templates 951

The Basics of Creating a Workflow 952

Working with the Built-in Workflow Activities 966

Controlling Flow 966

Runtime and Primitives 968

Error Handling 969

Collection Activities 970

Supporting Transactions 971

Managing Messages 973

Creating Flowchart Workflows 979

Creating a Workflow Application 980

The Scenario: Travel Request 980

The Application Project Templates 981

Creating the Database and Data Access Library 983

Creating the Vendor Reservation Service 989

Creating the Custom Activity Library 990

Designing the Workflow (Travel Request) 992

Creating the Client Application (Request and Approval Forms) 1003

Running the Travel Request Workflow Application 1009

Summary 1012

23 Developing Office Business Applications 1013

An Overview of Office Extension Features 1014

Office Features 1014

Visual Studio Office Project Types 1017

Creating an Office Add-In 1019

Customizing the Ribbon 1020

Customizing the Task Pane 1024

Creating Outlook Form Regions 1026

Page 15: Microsoft Visual Studio 2012 unleashed - GBV · 2013-10-15 · Microsoft Visual Studio 2012 Unleashed Class View 1Q1 1 y 1 Toolbar 191 Search Bar 193 Objects Pane 193 Members Pane

xviii Microsoft Visual Studio 2012 Unleashed

Creating an Office Document Extension 1031

Hosting Controls 1031

Creating an Actions Pane 1033

Storing Data in the Data Cache 1035

Summary 1037

24 Developing Applications in the Cloud with Windows Azure 1039

The Azure Management Portal 1040

Creating a New Azure Service 1040

Managing Your Azure Services 1044

Deploying a Website to the Cloud 1053

Creating a Cloud Service in Visual Studio 1058

Installing the Azure SDK for .NET 1059

Creating a Cloud Service Project 1060

Deploying the Cloud Service Project 1064

Summary 1069

25 Writing Windows Store Applications Using the Windows

Runtime Library 1071

Introducing Windows 8 and the Modern UI 1072

Modern UI Attributes 1074

The Windows Runtime Library 1076

Language Choices 1078

The Application Model 1081

Building a Windows Store Application 1082

Selecting the Project Type 1083

Designing the Layout 1085

Reacting to Lifecycle Events 1101

Publishing to the Windows Store 1105

Summary 1114

Index 1115