Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 13 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
13
Dung lượng
584,2 KB
Nội dung
341 Chapter 12 Customizing the Development Environment 342 Microsoft Visual Studio 2010: A Beginner’s Guide Key Skills & Concepts ● Implement Custom Templates ● Create Custom Snippets ● Write Macros I n addition to all the great features you’ve seen in preceding chapters, VS gives you the capabilities to customize your own environment. The customizations I’ll discuss are custom templates, custom snippets, and macros. Throughout the book, you’ve started new projects and added items to those projects, using what is available with VS. On some occasions, you might desire to have a special type of project or customize an existing project for your own needs. The same need might apply to project items (such as a special type of class file), where you might change the contents of an item or create a new item. Chapter 2 showed you how to use snippets, and Chapters 3 and 4 showed how to use several more snippets to quickly code common statements. In addition to using existing snippets, you can create your own. VS also has a management window that allows you to organize snippets, adding, deleting, and rearranging as you see fit. Whenever you run into repetitive scenarios, it would be nice to capture the actions you perform so that you can quickly complete a task. For example, if you found yourself using the same set of keystrokes, it would be nice to collapse that action down into one command. Macros allow you to collapse repetitive tasks into a single task, saving you time. Let’s start the journey of customizing VS by looking at customizing templates. Implementing Custom Templates As you’ve seen in previous chapters, VS helps you get started with new projects and project items by providing default project items and skeleton code. In most cases, this is a very quick way to get started with a project. As you become more proficient in VS, you might prefer to have projects with different items than those that ship with VS or item templates with different code. This section will show you how to create your own project and item templates. Chapter 12: Customizing the Development Environment 343 Creating New Project Templates If you’re working on a single project that lasts a long time, you might be satisfied with using a default template and doing a one-time modification. However, if you are starting new projects on a regular basis, customizing a project template can be very useful. There are various reasons you might want to create a custom project template, such as adding new items that aren’t included in the default project template, removing items from the default template that you don’t ever use, or changing an existing item for version upgrades. In addition to customizing existing templates, you might want to create a brand new template for a new project type that doesn’t yet exist. The example in this section will show you how to customize the ASP.NET MVC project template. The specific changes made to the template will be to remove much of the default code provided by the template. The assumption is that once you’ve written a few ASP.NET MVC applications, you won’t need the default files as examples and would prefer to start with a more bare-bones template and build the application from the ground up yourself. Modifying a Project The easiest way to get started with creating a new project template is to start a new project that is most similar to the project type you want to create. If you wanted a blank project, you might start with a Console project because there aren’t many items and it’s quicker to delete them all. In the scenario for this section, we want to create a specialized ASP .NET MVC project, so it makes sense to create a new ASP.NET MVC project. The following steps show you how: 1. Press CTRL-SHIFT-N to create a new project and select ASP.NET MVC 2 Web Application. Name the project and solution Custom ASP.NET MVC Web Application and set the location for anywhere you like. Click OK to create the project. Next, the Create Unit Test Project window will appear and you should click “No, do not create a unit test project” and click OK. VS will create a new solution with a project. Chapter 9 explains what each of the projects and items are. 2. Open the Controllers folder and delete its contents. 3. Open the Models folder and delete its contents. 4. Open the Views folder but only delete the Account and Home folders. 5. Open the Shared folder, under the Views folder, and delete its contents. 6. Double-click Global.asax and comment out the call to routes.MapRoute in the editor. 344 Microsoft Visual Studio 2010: A Beginner’s Guide 7. To make sure your changes are okay, build and run the project. Select Build | Rebuild Solution and ensure you don’t receive compiler errors. Then press F5 to run and allow VS to modify the Web.config file. Since you’ve commented out the route in Global .asax and there aren’t any files to locate, you’ll receive the message “The resource can’t be found” in the browser. This is okay because it’s assumed that you want to build your own controllers, models, and views and apply custom routing too. You now have customized ASP.NET MVC project that allows you to build your application without any preinstalled items. VS is likely to ship with an empty template, but for additional customization, you might replace the CSS file in the Content folder or add your own JavaScript libraries to the Scripts folder. Make any changes you feel are most helpful for starting a new ASP.NET MVC project. Next, you’ll learn how to transform this project into a reusable project template. Exporting the Project Template After you have a project configured the way you want, you can save it as a project template. The first step is to select File | Export Template, which will display the Choose Template Type window, shown in Figure 12-1. Choose Project Template and click Next. Figure 12-1 The Choose Template Type window Chapter 12: Customizing the Development Environment 345 The next window is Select Template Options, shown in Figure 12-2. The Template Name defaults to the name of the project, which you can change. You can see how the filled-in Template description tells what the template is used for. Additionally, if you want to associate an icon or preview, you can click the respective Browse button and select the image you want to be associated with the project. As you may recall, the New Project window has an icon for the project and will display a preview whenever you select the project. The “Automatically import the template into Visual Studio” option will make the project template available via the New Project window. “Display an explorer window on the output files folder” will allow you to access the new file, shown in the Output location. Click Finish to create the template. After you click Finish, VS does two things: it saves to the output location and makes the template available in VS. The output location is just a place to store the project template, Custom ASP.NET Web Application.zip, which contains all of the information VS needs to display the template and create the project if you select it in the New Projects window. Figure 12-2 The Select Template Options window 346 Microsoft Visual Studio 2010: A Beginner’s Guide You can share the project template with other developers too. The next section shows what to do to get the project template to appear in VS. Using the New Project Template The instructions for exporting the project template in the preceding section chose “Automatically import the template into Visual Studio.” The use of the word “import” might make you think there is some magic process going on in the background, which there is to some extent. However, all the Export Template Wizard did was copy the Custom ASP.NET MVC Web Application.zip file from the Output location to <My Documents>\Visual Studio 2010\Templates\ProjectTemplates, which I’ll call local project templates. The <My Documents> folder location can differ, depending on the version of Windows you’re running. Once the file appears in the local project templates folder, you can verify that it’s been imported into VS by pressing CTRL-SHIFT-N and observing that Custom ASP.NET MVC Web Application appears in the list. If you had not checked “Automatically import the template into Visual Studio” (Figure 12-2), then you could have copied the Custom ASP.NET MVC Web Application .zip file to the local project templates folder yourself and the project template would appear in VS. If you share the Custom ASP.NET MVC Web Application.zip file with another developer, she can copy to the local project templates folder also. If you delete the file from the local project templates folder, it will no longer appear in the VS New Projects window. Another option for adding project templates is to copy the project templates file to a folder under \Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates, which I’ll call global project templates. There are various folders under global project templates, including CSharp VisualBasic, Web, and more; each folder corresponding to folders in the VS New Project window. Under each folder is a locale code—for instance, English is 1033—and you would copy the file into the locale folder for the category you wanted the project template to appear in. For example, if you wanted the project template to appear in Visual C# | Web in the New Projects window, copy the project template *.zip file to \Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Web. Unlike templates in the local project templates folder, where all you need to do is copy the file, project templates in the global project templates folder don’t automatically show up. To test the global project templates scenario, you should remove the project template from your local project templates folder. You must close down VS and execute the following in a command window, which you should open by selecting Start | All Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | right-click on Visual Studio Command Chapter 12: Customizing the Development Environment 347 Prompt (2010) and select Run As Administrator. This will take a few minutes to run, but afterward you’ll see the project appear in the VS New Project window. This command imports all of the project templates from the global project templates folder into VS: devenv /installvstemplates If later you decide you don’t want a given template to appear in the VS New Project window, remove the project template from the global project templates folder(s) and run the preceding command again. Now you’re able to create and use custom project templates. While you might create projects occasionally, it’s a common task to create project items, covered next. Creating New Item Templates Sometimes, you use certain item templates frequently but often modify the contents of the item for your own purposes. In these cases, it’s useful to be able to create a custom item template instead. The example in this section will be to create something that isn’t currently an item in VS: a new item template for enums. To create a new item template, we’ll create the file for holding the item, save the new item, and then use the new item in a project. Creating an Item Template The easiest way to get started with creating a new item template is to start a new project that has an existing item template that is most similar to the one you want to create. For a new enum template, all we need is a class file, so any project that allows you to add a class file template will work. The example in this section will use a Console project, but the project type doesn’t matter because we’ll only be interested in extracting one file for the item template. The following steps show you how: 1. Press CTRL-SHIFT-N to create a new project and select Console Application. Name the project anything you want and set the location for anywhere you like; name and location don’t matter because we are only interested in the item template file and not the project. Click OK to create the project. VS will create a new solution with a project. By now, you’ve seen plenty of new Console applications in previous chapters, and this will be the same. 2. Right-click the project in Solution Explorer, select Add | New Item, select Code File, name the file Enum.cs (Enum.vb for VB), and click Add. This will add a new blank file to your project. 348 Microsoft Visual Studio 2010: A Beginner’s Guide 3. Add the following code to the file: C#: /// <summary> /// Enum description /// </summary> public enum MyEnum { /// <summary> /// Item 1 description /// </summary> Item1, /// <summary> /// Item 2 description /// </summary> Item2 } VB: ''' <summary> ''' Enum description ''' </summary> Public Enum MyEnum ''' <summary> ''' Item 1 description ''' </summary> Item1 ''' <summary> ''' Item 2 description ''' </summary> Item2 End Enum 4. Save the file. You now have a file that can be used as a skeleton for new enums. The next section shows you how to export this file so that it can be used as an item template. Exporting the Item Template After you have a file written the way you want, you can save it as an item template. The first step is to select File | Export Template, which will display the Choose Template Type window, shown in Figure 12-3. Choose Item Template and click Next. Chapter 12: Customizing the Development Environment 349 The next window is Select Item To Export, shown in Figure 12-4. The list shows all of the files eligible for creating an item. Check Enum.cs, which is the only file we’re interested in for this example. Click Next to continue. Next, you’ll see the Select Item References window, shown in Figure 12-5. These are the assemblies that are part of the project that you’re extracting the item template from. Check the assemblies that this item will require. In this case, I want to ensure the System assembly is included. Ignore the warning message, as it is assumed that you will always have the .NET Framework installed and the System.dll assembly will always be available. Click Next to continue. Figure 12-3 The Choose Template Type window 350 Microsoft Visual Studio 2010: A Beginner’s Guide Figure 12-6 shows Select Template Options, where you specify how the item template will appear in the New Items window that appears when selecting Add | New Item on a project. The Template name defaults to the name of the project, which you should change to the item name, by changing the template name to Enum. The description lets the user know the purpose of the item template. If you want to associate an icon or preview, you can click the respective Browse button and select the image you want to be associated with the item. As you may recall, the New Item window has an icon for the item and will display a preview whenever you select the project. The “Automatically import the template into Visual Studio” option will make the item template available via the New Item window. “Display an explorer window on the output files folder” will allow you to access the new file, shown in the Output location. Click Finish to create the item template. Figure 12-4 The Select Item To Export window [...]... “Automatically import the template into Visual Studio, ” copying the Enum.zip file from the Output location to \Visual Studio 2010\ Templates\ItemTemplates, which I’ll call local item templates The folder location can differ, depending on the version of Windows you’re running Once the file appears in the local 351 352 Microsoft Visual Studio 2010: A Beginner’s Guide Figure 12-6... item templates folder You must close down VS and execute the following in a command window, which you should open by selecting Start | All Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | right-click Visual Studio Command Prompt (2010) and select Run As Administrator This will take a few minutes to run, but afterward you’ll see the project appear in the VS New Item window This command... item templates is to copy the project templates file to a folder under \Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates, which I’ll call global item templates There are various folders under global item templates, Chapter 12: Customizing the Development Environment including CSharp VisualBasic, Web, and more, each folder corresponding to folders in the VS New Item window Under... new or existing project if one is not opened), pressing CTRL-SHIFT-A, and observing that Enum appears in the list in the New Item window If you had not checked “Automatically import the template into Visual Studio (Figure 12-6), then you could have copied the Enum.zip file to the local project templates folder yourself and the project template would appear in VS If you share Enum.zip file with another... but the process is similar for a VB snippet To create a new snippet, you can either work from an existing snippet file or start from scratch I’ll show you how to find and open existing snippets first 353 . Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | right-click on Visual Studio Command Chapter 12: Customizing the Development Environment 347 Prompt (2010) and select Run As. open by selecting Start | All Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | right-click Visual Studio Command Prompt (2010) and select Run As Administrator. This will take. chose “Automatically import the template into Visual Studio, ” copying the Enum.zip file from the Output location to <My Documents> Visual Studio 2010 TemplatesItemTemplates, which I’ll call