Tải bản đầy đủ (.pdf) (2,266 trang)

Microsoft visual studio 2015 unleashed

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (42.58 MB, 2,266 trang )


About This eBook

ePUB is an open, industry-standard format for eBooks. However, support of
ePUB and its many features varies across reading devices and applications.
Use your device or app settings to customize the presentation to your liking.
Settings that you can customize often include font, font size, single or double
column, landscape or portrait mode, and figures that you can click or tap to
enlarge. For additional information about the settings and features on your
reading device or app, visit the device manufacturer ’s Web site.
Many titles include programming code or configuration examples. To
optimize the presentation of these elements, view the eBook in single-column,
landscape mode and adjust the font size to the smallest setting. In addition to
presenting code and configurations in the reflowable text format, we have
included images of the code that mimic the presentation found in the print book;
therefore, where the reflowable format may compromise the presentation of the
code listing, you will see a “Click here to view code image” link. Click the
link to view the print-fidelity code image. To return to the previous page
viewed, click the Back button on your device or app.


Microsoft® Visual Studio® 2015

Unleashed


Lars Powers

Mike Snell



800 East 96th Street, Indianapolis, Indiana 46240 USA


Microsoftđ Visual Studiođ 2015 Unleashed
Copyright â 2016 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and authors assume no responsibility for
errors or omissions. Nor is any liability assumed for damages resulting from
the use of the information contained herein.
ISBN-13: 978-0-672-33736-9
ISBN-10: 0-672-33736-3
Library of Congress Control Number: 2015907636
Printed in the United States of America
First Printing August 2015
Editor-in-Chie f
Greg Weigand
Acquisitions Editor
Joan Murray
De ve lopme nt Editor
Mark Renfrow
Managing Editor
Kristy Hart
Proje ct Editor
Elaine Wiley
Copy Editor
Gill Editorial Services

Inde xe r
Heather McNeill
Proofre ade r
Jess DeGabriele
Te chnical Editor
Christophe Nasarre-Soulier
Editorial Assistant
Cindy Teeters
Cove r De signe r
Mark Shirar
Compositor
Nonie Ratcliff
Trade marks


All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
Warning and Disclaime r
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on
an “as is” basis. The authors and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages
arising from the information contained in this book or from the use of the CD or
programs accompanying it.
Spe cial Sale s
For information about buying this title in bulk quantities, or for special sales
opportunities (which may include electronic versions; custom cover designs;
and content particular to your business, training goals, marketing focus, or

branding interests), please contact our corporate sales department at
or (800) 382-3419.
For government sales inquiries, please contact

For questions about sales outside the U.S., please contact



Contents at a Glance

Introduction
Part I Introducing Visual Studio 2015
1 A Quick Tour of Visual Studio 2015

2 The Visual Studio IDE

3 The .NET Languages

Part II An In-De pth Look at the IDE
4 Solutions and Projects

5 Browsers and Explorers

6 Introducing the Editors and Designers

Part III Working with the Visual Studio Tools
7 Working with Visual Studio’s Productivity Aids

8 Testing Code


9 Refactoring Code

10 Debugging Code
11 Deploying Code
12 Developing Applications in the Cloud with Windows Azure
13 Working with Databases
Part IV Exte nding Visual Studio
14 Introducing the Automation Object Model

15 Extending the IDE

16 Extending the Code Editor

Part V Building We b Applications
17 Building Modern Websites with ASP.NET 5
18 Using JavaScript and Client-Side Frameworks
19 Building and Consuming Services with Web API and WCF
Part VI Building Windows Clie nt Apps
20 Building Windows Forms Applications

21 Building WPF Applications

22 Developing Office Business Applications



Part VII Cre ating Mobile Apps
23 Developing Windows Store Applications
24 Creating Windows Phone Applications
25 Writing Cross-Platform Mobile Applications with Apache Cordova

Index


Table of Contents
Introduction
Part I Introducing Visual Studio 2015
1 A Quick Tour of Visual Studio 2015
The Visual Studio Product Line

Community Edition

Professional Edition

Enterprise

MSDN

TFS and Related Tools

Languages and Frameworks

Programming Language Choices

The .NET Framework

The Many Faces of a .NET Application
Windows
Web
Mobile
Developing Windows 8/10 Clients

Windows (WinForms)
Windows Presentation Foundation (WPF)
Office/SharePoint Solutions
Creating Web Applications with ASP.NET 5
Building Websites with Web Forms
Developing with MVC/Razor
Creating a Single Page Application (SPA)
Coding Web Services with Web API
Coding for Azure

Creating a Cloud Application

Publishing to Azure

Working with Data

Model as Code (Code First)

Writing Mobile Apps

Create an Apache Cordova App

Summary

2 The Visual Studio IDE
Installing Visual Studio


Installing Optional Features


Signing In to Visual Studio

Managing Your IDE Settings
Specify Stored and Synchronized Settings
Change Color Theme
Manually Import/Export and Change Default IDE Settings
Switch IDE User
Getting Started

Startup Options

Creating Your First Project

Targeting Your Environment

Navigating the IDE

The Menus

The Many Toolbars

Customizing Toolbars

The Solution Explorer

The Text Editors

The Visual Designers

The Toolbox


The Properties Window

Managing the Many Windows of the IDE

Pinning

Docking

Custom Window Layouts

Navigating IDE Windows

Touch Support

Customize Your IDE Font

Providing Feedback on Visual Studio

The Customer Experience Program

Summary

3 The .NET Language s
What’s New in C# 6.0 and VB 14

Null-Conditional Operators

ReadOnly Auto Properties


NameOf Expression

Using (Imports) Statics

String Interpolation

Lambda Expressions as Methods (C# Only)



Index Initializers (C# Only)
Language Primer

Programming Objects

Types, Variables, and Constants

Understanding Operators

Making Decisions and Branching Code

Looping

Working with Groups of Items

Programming with Attributes

Creating and Raising Events
Language Features
Infer a Variable’s Data Type Based on Assignment

Create an Object and Initialize Its Values (Object Initializers)
Define a Collection and Initialize Its Values
Creating an Instance of a Nonexistent Class
Add Methods to Existing Classes (Extension Methods)
Add Business Logic to Generated Code (Partial Methods)
Access and Query Data Using the .NET Languages
Write Simple Unnamed Functions Within Your Code (Lambda

Expressions)

Splitting an Assembly Across Multiple Files

Working with XML Directly Within Your Code (VB Only)

Removing Unused Arguments from Event Handlers (VB Only)
Creating an Automatically Implemented Property
Dropping the Underscore in VB for Line Continuation
Working with Dynamic Languages/Objects

Covariance and Contravariance

Asynchronous Programming

The .NET Framework

A Map to the .NET Framework

Summary
Part II An In-De pth Look at the IDE
4 Solutions and Proje cts

Understanding Solutions

Creating a Solution

Working with Solutions

Getting Comfortable with Projects

Creating a Project


Working with Project Definition Files

Working with Projects

Summary

5 Browse rs and Explore rs
Leveraging the Solution Explorer

Visual Cues and Item Types

Interacting with Items

Inspecting Objects

Class View

Toolbar


Search Bar

Objects Pane

Members Pane

Server Explorer

Data Connections

Server Components

Azure

Object Browser

Changing the Scope

Browsing Objects

Document Outline

Editing Elements

Summary

6 Introducing the Editors and De signe rs
Getting Started with the Basics

The Text Editor


Visual Studio Designers

Coding with the Code Editor

Opening an Editor

Writing Code in the Code Editor

Anatomy of the Code Editor Window

Code Navigation Tools

Searching Documents

Debugging in the Text Editor

Printing Code

Using the Code Definition Window

Creating and Editing XML Documents and Schema
Inferring Schema


Designing XML Schemas

Editing XSLT Style Sheets

Working with Cascading Style Sheets


Adding Style Rules

Defining Style Sheet Attributes

Developing Windows Client Applications
Creating a Windows Forms Project
Creating a Windows Presentation Foundation Project
Developing Web Forms

Designing a Web Form Application

Authoring WinForms Components and Controls
Creating a New Component or Control
Further Notes on Writing Component Code
Creating Classes with the Class Designer
Creating a Class Diagram
Adding Items to the Diagram
Defining Relationships Between Classes
Defining Methods, Properties, Fields, and Events
Summary
Part III Working with the Visual Studio Tools
7 Working with Visual Studio’s Productivity Aids
Basic Aids in the Text Editor

Change Tracking

Coding Problem Indicators

Active Hyperlinking


Syntax Coloring

Outlining and Navigation

Code Outlining

Tag Navigation

Smart Tasks and Light Bulbs

HTML Designer

Windows Forms Designer

Code Editor

IntelliSense

Complete Word

Quick Info

List Members

Parameter Info



Organize Usings

Code Snippets and Template Code
Brace Matching
Customizing IntelliSense
The Task List

Shortcut Tasks

Comment Tasks

Summary
8 Te sting Code
Unit Testing Basics

Creating a Test Project

Writing a Unit Test

Running Your Tests

Controlling Test Settings

The Unit Testing Framework

The TestContext Class

The Test Attribute Classes

Unit Test Setup and Teardown

The Assert Classes


Testing Your Exceptions

Creating Data-Driven Unit Tests

Testing Web Applications
Unit Testing MVC and Web API Projects
Unit Testing ASP.NET Pages
Creating Ordered Tests

Summary

9 Re factoring Code
Visual Studio Refactoring Basics
Invoking the Refactoring Tools
Making (and Previewing) Changes
Using the Class Designer to Refactor
Renaming Code
Accessing the Rename Operation
Working with the Rename Dialog Box
Refactoring Variable Assignments

Introduce Constant

Introduce Local

Inline Temporary Variable




Extract Method

Accessing the Extract Method Refactor

Extracting Methods

Extracting a Single Line of Code

Generate Method Stub

Extract Interface

Accessing the Extract Interface Refactor

Extracting Interfaces

Change Signature

Removing a Parameter

Reorder Parameters

Encapsulate Field

Accessing Encapsulate Field

Summary

10 De bugging Code
Debugging Basics

The Scenario
The Many Phases of Debugging
Debugging the Application (Self-Checking)
Debugging Basics Summary
The Visual Studio Debugger
The Debug Menu and Toolbar
Debug Options
Stepping In, Out, and Over Code
Indicating When to Break into Code
Working with Tracepoints (When Hit Option)
Viewing Data in the Debugger
Using the Edit and Continue Feature
Advanced Debugging Scenarios
Remote Debugging
Debugging WCF Services
Debugging Multithreaded Applications
Debugging Parallel Applications
Debugging a Client-Side Script
Debugging Crash Information (Dump Files)
Debugging Windows Store Apps
Summary
11 De ploying Code


An Overview of Client Deployment Options
Introducing ClickOnce Deployments
Introducing Windows Installer and InstallShield Deployments
Publishing a Project with ClickOnce

Publishing a Project with InstallShield Limited Edition


Publishing an ASP.NET Web Application

Selecting a Target

Configuring a Connection

Configuring Deployment Settings

Previewing the Publication

Summary
12 De ve loping Applications in the Cloud with Windows Az ure
Create Your Azure Account

Azure Account Sign-Up

Link Your Account to Visual Studio

Manage Azure Subscriptions

Create and Deploy an Azure Web Apps in Visual Studio
The Azure Hosting Platform
Create the ASP.NET Application and Azure Hosting
Deploy/Publish an Application to Azure
Set Up an Existing Application to Publish to an Azure web app
Website Management with Azure Server Explorer
Debug an Azure web app
Create Your Web App from the Azure Portal


Create the Application Hosting Environment

Configuring Your New Azure web app

The Website Toolbar

Creating a Database

Deploying to the New Environment from Visual Studio

Monitor and Manage Applications in Azure

Monitor and Manage a Website

Monitor and Manage a SQL Database

The Azure SDK for Visual Studio 2015

Download, Install, and Sign In

QuickStart Templates

Azure Resource Group Deployment Projects

Azure Cloud Services (PaaS)

Creating a Cloud Service Project




Running Your Cloud Service Project Locally
Deploy the Cloud Service Project
Summary
13 Working with Database s
Creating Tables and Relationships

Creating a New SQL Server Database

Defining Tables

Working with SQL Statements

Writing a Query

Creating Views

Developing Stored Procedures

Creating Triggers

Creating User-Defined Functions

Using Database Projects

Creating a Database Project

Changing the Database

Building and Deploying


Creating Database Objects in Managed Code
Creating a Stored Procedure in C#
Binding Controls to Data
An Introduction to Data Binding
Autogenerating Bound Windows Forms Controls
Editing Typed Data Sets
Manually Binding Windows Forms Controls
Data Binding in WPF Applications
Data Binding with Web Controls
Object Relational Mapping
An Overview of LINQ
Mapping Using the O/R Designer
LINQ Code
Working with the Entity Framework
Querying Against the Entity Data Model
Summary
Part IV Exte nding Visual Studio
14 Introducing the Automation Obje ct Mode l
An Overview of the Automation Object Model
Object Model Versions
Automation Categories


The DTE/DTE2 Root Object
Solution and Project Objects
Controlling Projects in a Solution
Accessing Code Within a Project
Working with Windows
Referencing Windows
Interacting with Windows

Text Windows and Window Panes
The Tool Window Types
Linked Windows
Command Bars

Documents

Text Documents

Command Objects

Executing a Command

Mapping Key Bindings

Debugger Objects

Summary

15 Exte nding the IDE
Creating Your First Extension

Setting Package Parameters

Adding Project Items

The Structure of an Extension
Defining and Reacting to Commands
A Sample Extension: Color Selector
Getting Started

Creating the User Control
Finishing the Package
Summary
16 Exte nding the Code Editor
The Extensibility Problem

Creating Dynamic Applications

MEF Architecture

MEF Principles

Working with MEF

The Visual Studio Editor and MEF
Editor Extension Points
Using the Visual Studio SDK


Managing Extensions and Updates

Creating Your Own MEF-Based Editor Extension

Summary

Part V Building We b Applications
17 Building Mode rn We bsite s with ASP.NET 5
ASP.NET Website Fundamentals
Introducing ASP.NET 5
The .NET Core Framework and Execution Environment

Choosing an ASP.NET Project Template
Understanding the ASP.NET 5 Project Template and Related Files
ASP.NET 5 Dependencies and Package Managers
Creating a Web Application with ASP.NET 5/MVC 6

Understanding the MVC Pattern

Creating a New ASP.NET 5 MVC 6 Project

Writing ASP.NET Server Code (Models and Controllers)

Defining a Model (Using Entity Framework 7)

Developing Controllers

Coding for the UI (Views and Related Web UI Elements)

The HTML Tags

The Razor Syntax

HTML Helpers

Page Layout with Razor

Strongly Typed Views

User Input Validation

Creating the Customer Example Pages


View Components, View Models, and Partial Views

Using Scaffolding to Generate a Controller and Views

Summary
18 Using JavaScript and Clie nt-Side Frame works
JavaScript Fundamentals

Storing and Using Scripts

Writing JavaScript

Functions

Objects

Built-In Objects

Working with the Browser Object Model (BOM)

Document Object Model (DOM)

Events



Developing with jQuery
jQuery in Your Visual Studio Project


Selecting Elements

Acting on Your Selection

Traversing Your Selections

Accessing Selection Content

Changing Elements/Attributes

Handling Events

Animations and Effects

jQuery and AJAX
Building Single-Page Applications (SPAs) with Client-Side JavaScript
Frameworks
Selecting a Client Framework
Responsive Web Layout with Bootstrap 3

Minify Your JavaScript with Gulp

Using Knockout

Creating a Site with AngularJS

Summary
19 Building and Consuming Se rvice s with We b API and WCF
Service Fundamentals
Why ASP.NET Web API and WCF


Key Web Service Terms

Use ASP.NET Web API to Build HTTP Services

Creating an ASP.NET Web API Project

Defining a Model

Creating the Services (Controller)

Understanding Service Routing

Consuming an ASP.NET Web API Service

WCF Service Applications

The WCF Project Template

Creating a WCF Service

Running and Testing Your WCF Service

Consuming a WCF Service

Creating/Calling REST-Based WCF Services

Hosting and Deploying a WCF Service

Summary

Part VI Building Windows Clie nt Apps
20 Building Windows Forms Applications


The Basics of Form Design

Considering the End User

Understanding the Role of UI Standards

Planning the User Interface

Creating a Form
The Windows Forms Application Project Type
Form Properties and Events
Adding Controls and Components

Control Layout and Positioning

Using Containers

Control Appearance and Behavior

Working with ToolStrip Controls

Displaying Data

Creating Your Own Controls

Subclassing an Existing Control


Designing a User Control

Creating a Custom Control

Summary
21 Building WPF Applications
The Windows Presentation Foundation Platform
Programming Model
Introducing the WPF Designer
XAML and Design Panes
Programming with WPF

Layout

Styles and Templates

Data Binding

Routed Events

Building a Simple Image Viewer Application
Starting the Layout
Storing the Images
Binding to the Images
Button Event Handlers and Image Effects
Path Selection with a Common Dialog Box
Summary
22 De ve loping Office Busine ss Applications
An Overview of Office Extension Features


Office Features



Visual Studio Office Project Types
Creating an Office Add-In

Customizing the Ribbon

Customizing the Task Pane

Creating Outlook Form Regions

Creating an Office Document Extension

Hosting Controls

Creating an Actions Pane

Storing Data in the Data Cache

Extending Office with Webpages

Starting with the App for Office Project Template

Summary

Part VII Cre ating Mobile Apps
23 De ve loping Windows Store Applications

Introducing the Modern UI

Modern UI Attributes

The Windows Runtime Library

Language Choices

The Application Model

Building a Windows Store Application

Selecting the Project Type

Designing the Layout

Reacting to Lifecycle Events

Publishing to the Windows Store

Summary
24 Cre ating Windows Phone Applications
Windows Phone Fundamentals

The UI Basics

The Programming Model

Moving from Silverlight to WinRT


Porting a Simple Silverlight Phone App to WinRT

Building a Universal App

The Universal Project Types

Creating the Data Model and View Model

Creating the Windows Phone UI

Creating the Windows UI

Summary
25 Writing Cross-Platform Mobile Applications with Apache Cordova


Fundamentals of Cordova Development
How Cordova Works
Cordova Dependencies
The Cordova Project Template
Creating a Basic Cordova App
Running and Debugging Your App
Using Cordova Frameworks and Plug-Ins
Choosing Cordova Client Frameworks
Cordova Plug-Ins (for Accessing Native Device Capabilities)
Developing a Cordova App with Ionic and Angular
Set Up Your Project
Anatomy of the Ionic-Angular-Cordova App
Rebuild the Sample App
Support Storage

Running on Windows Phone
Additional Items to Consider
Summary
Inde x


About the Authors

Mike Sne ll spends his work life helping teams build great software that
exceeds the expectations of end users. He runs the Solutions division at CEI
(www.ceiamerica.com). Mike and his team deliver architecture, consulting,
and mentoring to clients looking for help with enterprise projects, commercial
software, mobile applications, or cloud-based solutions. He is also a
Microsoft Regional director.
Lars Powe rs is currently the director of application development at
Newgistics, Inc. Prior to Newgistics, he held various technical management
positions at 3M and spent many years with Microsoft as a platform evangelist
focused on emerging technologies.


Dedication
To my wife, Carrie Snell. Thank you.
Mike Snell
To Cheryl, once again.
Lars Powers


Acknowledgments
Mike Sne ll:
I would like to thank all the fine people involved with the making of this book.

This includes the team at Pearson: Joan Murray, Mark Renfrow, Elaine Wiley,
and Christophe Nasarre-Soulier. I would also like to thank the team of
developer-architects at CEI for acting as a sounding board for so many topics.
Of course, I’m also grateful to my co-author, Lars Powers, for the many years
of collaboration.
Lars Powe rs:
Nothing of consequence is delivered without team work. This book is no
exception. I’d like to acknowledge the Pearson team who did all the hard work
to get this book into your hands. Joan Murray alternately cajoled, supported,
and pushed us along. Mark Renfro and Elaine Wiley stitched all the pieces
together for us. And Christophe Nasarre-Soulier ensured we stayed on the
mark for technical accuracy.
And to my co-author, Mike: as always, projects are easier with you on board.


×