饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 14 页

作者:Nick Randolph/等 当前章节:15370 字 更新时间:2026-6-18 14:51

Two additional switches can be used with the alias command. The /delete switch, along with

an alias name, removes a previously defined alias. If you want to remove all aliases you may have

defined and revert any changes to a predefined alias, you can use the /reset switch.

the iMMediate window

Quite often when you are writing code or

debugging your application, you will want to

evaluate a simple expression either to test a bit

of functionality or to remind yourself of how

something works. This is where the Immediate

window comes in handy. This window enables you

to run expressions as you type them. Figure 4-12

shows a number of statements — from basic

assignment and print operations to more

advanced object creation and manipulation.

fiGure 4-12

The Immediate window supports a limited form of IntelliSense, and you can use the arrow keys to

track back through the history of previous commands executed. Variable values can be displayed

by means of the Debug.Print statement. Alternatively, you can use its ? alias. Neither of these

are necessary in C#; simply type the variable ’ s name into the window and press Enter to print

its value.

When you execute a command in the Immediate window while in design mode, Visual Studio will

build the solution before executing the command. If your solution doesn ’ t compile, the expression

cannot be evaluated until the compilation errors are resolved. If the command execute code has

an active breakpoint, the command will break there. This can be useful if you are working on a

particular method that you want to test without running the entire application.

You can access the Immediate window via the Debug . Windows . Immediate menu or the

Ctrl + Alt + I keyboard chord, but if you are working between the Command and Immediate windows

you may want to use the predefi ned aliases cmd and immed , respectively.

In Visual Basic you can ’ t do explicit variable declaration in the Immediate

window (for example, Dim x as Integer), but instead you do this implicitly

via the assignment operator. The example shown in Figure 4 - 12 shows a new

customer being created, assigned to a variable c, and then used in a series of

operations. When using C#, new variables in the Immediate window must be

declared explicitly before they can be assigned a value.

Note that in order to execute commands in the Immediate window you need to

add > as a prefi x (for example, > cmd to go to the Command window); otherwise

Visual Studio tries to evaluate the command.

Also, you should be aware that the language used in the Immediate window is

that of the active project. The examples shown in Figure 4 - 12 will work only if a

Visual Basic project is currently active.

the class View

Although the Solution Explorer is probably the most useful tool window for navigating your

solution, it can sometimes be diffi cult to locate particular classes and methods. The Class View

tool window provides you with an alternative view of your solution that lists namespaces, classes,

and methods so that you can easily navigate to them. Figure 4 - 13 shows a simple Windows

application that contains a single form (MainForm), which is selected in the class hierarchy. Note

that there are two Chapter04Sample nodes. The fi rst is the name of the project (not the assembly

The Class View . 63

64 .

chaPter 4 The ViSuAl STudio WorkSpAce

as you might expect), and the second is the namespace that

MainForm belongs to. If you were to expand the References

node, you would see a list of assemblies that this project

references. Drilling further into each of these would yield a

list of namespaces, followed by the classes contained in the

assembly.

In the lower portion of Figure 4-13 you can see the list of

members that are available for the class Form1. Using the

right-click shortcut menu, you can either filter this list based

on accessibility, sort and group the list, or use it to navigate to

the selected member. For example, clicking Go To Definition

on InitializeComponent() would take you to the Form1.

Designer.vb file.

The Class View is useful for navigating to generated members,

which are usually in a file hidden in the default Solution Explorer

view (such as the designer file in the previous example). It can

also be a useful way to navigate to classes that have been added

to an existing file — this would result in multiple classes in the

same file, which is not a recommended practice. Because the file

does not have a name that matches the class name, it becomes

hard to navigate to that class using the Solution Explorer; hence

the Class View is a good alternative.

the error list

fiGure 4-13

The Error List window displays compile errors,

warnings, and messages for your solution, as

shown in Figure 4-14. You can open the Error

List window by selecting View . Error List, or

by using the keyboard shortcut Ctrl+\, Ctrl+E.

fiGure 4-14

Errors will appear in the list as you edit code

and when you compile the project. Double-clicking an error in the list opens the file and takes you

to the line of code that is in error.

You can filter the entries in the list by toggling the buttons above the list to select the types of errors

(Errors, Warnings, and/or Messages) you want to display.

the obJect browser

Another way of viewing the classes that make up your application is via the Object Browser.

Unlike most other tool windows, which appear docked to a side of Visual Studio 2010 by default,

the Object Browser appears in the editor space. To view the Object Browser window, select

The object Browser .

65

View . Object Browser, or by using the keyboard shortcut Ctrl+Alt+J (or F2, depending on your

keyboard settings). As you can see in Figure 4-15, at the top of the Object Browser window is a

drop-down box that defines the object browsing scope. This includes a set of predefined values, such

as All Components, .NET Framework 4.0, and My Solution, as well as a Custom Component Set.

Here, My Solution is selected and a search string of sample has been entered. The contents of the

main window are then all the namespaces, classes, and members that match this search string.

fiGure 4-15

In the top right-hand portion of Figure 4-15

you can see the list of members for the selected

class (MainForm), and in the lower window

the full class definition, which includes its

base class and namespace information. One

of the options in the Browse drop-down of

Figure 4-15 is a Custom Component Set. To

define what assemblies are included in this set

you can either click the ellipsis next to the

drop-down or select Edit Custom Component

Set from the drop-down itself. This presents

you with an edit dialog similar to the one

shown in Figure 4-16.

Selecting items in the top section and clicking

Add inserts that assembly into the component

set. Similarly, selecting an item in the lower

section and clicking Remove deletes that

assembly from the component set. Once you

have finished customizing the component set, it

will be saved between Visual Studio sessions.

fiGure 4-16

66 .

chaPter 4 The ViSuAl STudio WorkSpAce

the code definition window

When navigating around your code you might come across a method call that you’d like to view

the code for without leaving your current position in the code editor. This is where the Code

Definition window can come in handy, to show the source of the method when the cursor has been

placed within a reference to it (as shown in Figure 4-17). Access it via View . Other Windows .

Code Definition window (Ctrl+\ , Ctrl+D). It’s just like another code editor window with many of

the same commands available (such as inserting a breakpoint, view call hierarchy, and so on), but

is read-only. To edit the code for that method, right-click anywhere within the Code Definition

window and select Edit Definition. The source code file for this method will be opened in a code

editor window and the method definition will be navigated to.

fiGure 4-17

You can also use the Code Definition window with the Class View and the

Object Browser windows to view the code for the selected member of a class.

the call hierarchy window

A new feature in Visual Studio 2010, the Call Hierarchy window displays all the calls to and from a

method (or property or constructor, but each henceforth referred to as methods), enabling you to see

where a method is being used and additionally what calls it makes to other methods. This enables

you to easily follow the execution path and the flow of the code.

To view the call hierarchy for a method, select a method definition in the code editor window and

select View Call Hierarchy from the right-click context menu. This adds the method to the tree

in the Call Hierarchy window with two subnodes — Calls To (MethodName) and Calls From

(MethodName), as shown in Figure 4 - 18 .

fiGure 4 - 18

Expanding the Calls To (MethodName) lists all the methods that call the specifi ed method. Expanding

the Calls From (MethodName) lists all the other methods that are called by the specifi ed method.

The Call Hierarchy window allows you to drill down through the results to build a hierarchy of the

program execution fl ow — seeing which methods call the specifi ed method, which methods call them,

and so on.

Double - clicking a method navigates to that method defi nition in the code editor window.

You can view the call hierarchy for methods in the Class View window or the

Object Browser window also, by right - clicking the method and selecting View

Call Hierarchy from the drop - down menu.

Despite the fact that the Call Hierarchy window can be left fl oating or be docked, it doesn ’ t work in

the same way as the Code Defi nition window. Moving around the code editor window to different

methods will not display the call hierarchy automatically for the method under the cursor — instead

you will need to explicitly request to view the call hierarchy for that method, at which point it will

be added to the Call Hierarchy window. The Call Hierarchy window can display the call hierarchy

for more than just one method and each time you view the call hierarchy for a method it is added

to the window rather than replacing the call hierarchy currently being viewed. When you no longer

need to view the call hierarchy for a method, select it in the window and press Delete (or the red

cross in the toolbar) to remove it.

This window can come in very handy when working on an unfamiliar project or

refactoring a project.

The Call Hierarchy Window . 67

68 .

chaPter 4 The ViSuAl STudio WorkSpAce

the docuMent outline tool window

Editing HTML files, using either the visual designer or code view, is never as easy as it could be,

particularly when you have a large number of nested elements. When Visual Studio .NET first arrived

on the scene, a feature known as document outlining came to at least partially save the day. In fact,

this feature was so successful for working with HTML files that it was repurposed for working with

non-web forms and controls. This section introduces you to the Document Outline window and

demonstrates how effective it can be at manipulating HTML documents, and forms and controls.

htMl outlining

The primary purpose of the Document Outline window is to present a navigable view of HTML

pages so that you could easily locate the different HTML elements and the containers they were in.

Because it is difficult to get HTML layouts correct, especially with the many .NET components that

could be included on an ASP.NET page, the Document Outline view provides a handy way to find

the correct position for a specific component.

Figure 4-19 shows a typical HTML page. Without the Document Outline window, selecting an

element in the designer can be rather tricky if it’s small or not visible in the designer. The Document

Outline pane (View . Other Windows . Document Outline), on the left of Figure 4-19, enables

you to easily select elements in the hierarchy to determine where in the page they are located, and to

enable you to set their properties.

fiGure 4-19

The Document outline Tool Window .

69

Visual Studio analyzes the content of the currently active file and populates it with a tree view

containing every element in the page hierarchy. The Name or ID value of each element will be displayed

in the tree (if they are assigned one), while unnamed elements are simply listed with their HTML tags.

As you select each entry in the Document Outline window, the Design view is updated to select the

component and its children. In Figure 4-19, the SelectCategoryList RadioButtonList control’s tag is

selected in the Document Outline window, highlighting the control in the Design view, and enabling you

to see where it is located on the page. Correspondingly, selecting a control or element in the Design view

will select the corresponding tag in the page hierarchy in the Document Outline window (although

you will need to set the focus back to the Document Outline window for it to update accordingly).

control outlining

The Document Outline window has been available in Visual Studio since the first .NET version for

HTML files but has been of little use for other file views. When Visual Studio 2003 was released, an

add-in called the Control view was developed that allowed a similar kind of access to Windows Forms.

The tool was so popular that Microsoft incorporated its functionality into the Document Outline

tool window, so now you can browse Windows Forms in the same way.

Figure 4-20 shows a typical complex form, with many panels to provide structure and controls to

provide the visual elements. Each component is represented in the Document Outline by its name

and component type. As each item is selected in the Document Outline window, the corresponding

visual element is selected and displayed in the Design view.

This means that when the item is in a menu (as is the case in Figure 4-20) Visual Studio

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页