|
| |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
Visual Studio .NET supports a number of built in project types and wizards that generate skeleton projects and solutions depending on the type of application the developer intend to build. These project templates are an excellent way to automate the basic plumbing required for very common project types, such as Windows Applications, Class Libraries and Web Services. However, Visual Studio .NET supports only those basic project types that are extremely common across the developer community. Given the wide diversity and disparity of developer needs, Visual Studio .NET (and for that matter, previous versions of Visual Studio as well) expose an extensible architecture, where developers can write their own custom project types and wizards. This is an excellent feature that allows developers to write canned skeleton code that can be generated automatically, without having to resort to the infamous "copy-paste" anti-pattern. One of the strongest motivations for writing custom wizards is to write skeleton code once, and have it automatically customized, generated, and added to further projects. In this article, we will explore Visual Studio .NET\\\'s support for building custom wizards, and discuss how developers can create their own wizards and project templates. Further, we will build a sample wizard that generates a class that maintains a thread pool holding a specified number of threads. Each of these threads will call a user specified method via the use of delegates. All the thread management will be handled by the generated code. Note: this article is based on Visual Studio .NET Beta 2 and Windows 2000. In this article, we use the words Wizards and Templates interchangeably. Existing Projects Types and Wizards The following screen shot shows some of the project templates and wizards that are supported by Visual Studio .NET by default. When a project type and template is chosen, Visual Studio .NET creates a solution and project based on the user\\\'s selection, and generates some files that contain the skeleton code. Some project templates, like the Class Library template, generate very simple code that contains one class and a stub for its constructor, while templates such as Visual Studio .NET Add-ins or ATL Server generate quite a few files, each containing substantial code. This generated code is usually stored in a file with placeholders for user input values, such as the class and project name. When a specific project type is chosen, Visual Studio .NET invokes a component that reads these canned (also known as templated) files, and substitutes the placeholders with the user input values. Next, this component creates the required projects and solutions using the Visual Studio .NET Extensibility model. Visual Studio .NET looks for specific files in a fixed location that contain information about the available project templates, and the components that will be responsible for creating new projects based on that template. We will cover this architecture in the next section. Visual Studio .NET Project Template Architecture Visual Studio .NET uses a component based model to support the project templates. Each project template has a corresponding component that handles requests for the given template. A single component can handle requests for multiple templates as well. Visual Studio .NET uses two types of files that are used to identify the project templates, and the components that handle requests for these project templates. The first file (identified by a .vsz extension) contains information about the component, and any custom parameters that the component handling the project template might need. The second file (identified by a .vsdir extension) contains information about the description of the project type, and display properties in the New Project dialog box. Let us now explain these two files in detail.
A sample .vsz file that handles C# Console Projects is shown below. The first line is used for versioning, the Wizard tag is used to point to the ProgId of the component that will handle requests for C# console projects, and the multiple Param tags contain information that will be passed to the VsWizard.VsWizardEngine component. Note that this component handles multiple templates, so the parameters specify the specific template (console project) and the selected language (C#). VSWIZARD 7.0 Wizard=VsWizard.VsWizardEngine Param="WIZARD_NAME = CSharpConsoleWiz" Param="WIZARD_UI = FALSE" Param="PROJECT_TYPE = CSPROJ" All components that act as handlers for project templates need to support the IDTWizard interface from the EnvDTE namespace. This wizard contains a single method, Execute, which is called when the template is invoked. Visual Studio .NET passes the base DTE object (used to access the entire Visual Studio .NET object model, please refer to the links section for articles that cover this), a parameter list containing information such as the name and path of the project as specified by the user, and another parameter list that contains the custom parameters from the .vsz files as shown above. This information is sufficient for the component to process the template request, and since the method has access to the root DTE object, the component can create solution and projects by invoking methods on this object.
A sample .vsdir file containing two entries is shown below. CSharpEXE.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|#2318|10|#2319| {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4554| |WindowsApplication CSharpWebApp.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|#2326|40|#2327| {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4551|1|WebApplication1|web Some of the main fields in this file are described below. For a complete description of these fields and their relative orders, please refer to the links section at the end of this article.
When the New Project dialog box is invoked, Visual Studio .NET reads all the .vsdir and the .vsz files to display the available project templates. For the C# project type, this is read from the Microsoft Visual Studio.NET/VC#/CSharpProjects folder as shown below. This folder contains all the files that represent the C# templates that are available to the New Project dialog box. Later in the article, when we create our custom wizard, we will have to copy the .vsz and .vsdir files into this folder. 返回类别: 教程 上一教程: .NET断想 下一教程: 微软对.net的定义 您可以阅读与"给开心的:Visual Studio .NET Custom Wizards"相关的教程: · 使用 Visual Studio .NET 重新分发 .NET 框架 · Visual Studio.NET版本比较(中文版) · 关于Microsoft.NET Beta1与Visual Studio.NET Alpha不兼容 · Visual Studio.NET企业级结构设计版评测 · Visual Studio.net Beta2安装 |
| 快精灵印艺坊 版权所有 |
首页 |
||