When you open Visual Studio 2012 for the first time you're going to notice a few changes. We're going to start out by looking at some of the standard activities you will perform with Visual Studio in your normal development activities and in doing so discover a number of new and changed features in this powerful development tool.
These are tasks which are common to all developers regardless of the language they program in or the platform they are targeting.
It might look the same as it did before, but there are a few changes when creating a new project. Let's create a new project and see what has changed.
Start Visual Studio 2012.
Choose the File | New Project menu option.
Examine the list of project types that are available and choose one that is of interest to you. If you're not sure what to choose, select Visual C# | Class Library.
Enter a name of your choice for the project. If you feel lacking in creativity, take the default name and then click on OK.
The project is now created and you are ready to start writing code.
On its own the project creation process in Visual Studio works exactly as it did in previous versions of Visual Studio, with the only difference being that you can now target .NET Framework 4.5.
If that's all there was to it, it would hardly be worth talking about, however there are larger differences to be seen in the project creation area. Let's talk about them.
It's here that you will notice the first major change from Visual Studio 2010 and where you will see differences between Visual Studio 2012 running on Windows 8 versus a prior version of Windows.
The operating system you are using dictates whether you have access to the new Windows Runtim e (WinRT) or not and thus whether you can write Windows Store Applications or not. On Windows 8 you will see a range of options for creating Windows Store Applications, whereas on Windows 7 and prior you will only see options for creating applications that do not use WinRT.
The Portable Class Library project template allows developers to create class library assemblies that can be referenced from not only standard .NET Framework applications, but also from Silverlight, Xbox 360 (XNA), and Windows Phone 7 projects.
This is at its most valuable when sharing service and data contracts or common domain classes between backend web services and frontend clients built using different technologies. For example, if previously you had a Silverlight application that used a set of web services running under ASP.NET then you would have to share code for those services by having separate projects for each runtime that looked exactly the same and used linked files to share the source.
Now all you have to do is move your common code into a single portable class library and add a reference to that portable library project from both your Silverlight project and your ASP.NET Web Application project.
Note that Portable Class Libraries are also available for Visual Studio 2010 using the Portable Library Tools extension from the Visual Studio Gallery (http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981).
Visual Studio 2012 only provides project templates for Office 2010 projects. For Office 2007 projects you will need to continue using Visual Studio 2010.
Visual Studio 2012 no longer has the Crystal Reports project template, nor does it feature Visual Studio Installer projects. If you are using Visual Studio Installer projects at the moment you will need to look at some different approaches. We cover some of the choices for creating installers in the Appendix, Visual Studio Pot Pourri
The Creating a Windows Store app recipe in Chapter 2, Getting Started with Windows Store Applications
It's always nice to start a new project and if you've been working with legacy code for a while there's no better feeling! Unfortunately that feeling is often all too rare and we spend most of our time dealing with existing code with a long history.
All that legacy code is probably what's keeping you employed, so what you really want is to be able to open up that existing code in your shiny new copy of Visual Studio 2012 and bring the power of Visual Studio 2012 to bear on it, making life just that little bit easier for yourself.
One of the big changes in Visual Studio 2012 is that projects opened in Visual Studio 2012 are also backwards compatible with Visual Studio 2010 Service Pack 1 and we will see how that works. This process is called round tripping.
The good news for teams is that round tripping means they can gradually move from Visual Studio 2010 to Visual Studio 2012 as and when they are ready. They won't have the problems of the past where one team member checks in an upgraded solution file to source control, thus forcing the rest of the team to upgrade simply to continue working.
If you don't have any existing code you want to use you can use the sample Visual Studio 2010 solution we've prepared for you.
The solution we will be using throughout this chapter is called
VS2010_Web and can be found at
If you are going to use some of your existing code, ensure that the current code is in source control or that you have backed up the code.
In order to see the backwards compatibility in action you will need Visual Studio 2010 with Service Pack 1 installed on your machine.
Using either the Open Project link on the Visual Studio start page or the File | Open | Project/Solution menu select the
VS2010_Websolution to start the conversion process.
Visual Studio will automatically migrate the solution and all the projects within it. When the process is complete you will be shown a migration report.
Close the report when you have finished looking at it.
From Solution Explorer open the
Default.aspxfile in the
VS2010_Webproject and change the Welcome to ASP.NET! text on the page to Welcome to Visual Studio 2012!.
Build and run the project to see that the application is working as expected.
Close Visual Studio 2012 and open Visual Studio 2010 with Service Pack 1.
VS2010_Websolution and run the application.
You should see the web application appear, showing the updated message from Visual Studio 2012.
The Visual Studio team worked with the various language and development product teams to ensure that project file formats would work consistently between Visual Studio 2010 and Visual Studio 2012. The first stage of these changes rolled out publicly with the release of Service Pack 1 for Visual Studio 2010, allowing Visual Studio 2010 to understand the new project formats.
When Visual Studio 2012 opens a Visual Studio 2010 project it will automatically upgrade the project format unless those changes will affect the ability to open the project in Visual Studio 2010. Any compatibility breaking changes will cause a dialog to be shown describing the changes and you can decide what action to take.
Not all project types will work with round tripping.
Visual Studio database projects aren't supported for round tripping. Visual Studio 2012 obsoleted Visual Studio database projects and replaced them with the new SQL Server Data Tools (SSDT) projects. When you open an old database project in Visual Studio 2012 you can upgrade it to an SSDT project. This project format also supports round tripping. However, if you wish to open an SSDT project in Visual Studio 2010 you will need to install the SQL Server Data Tools separately. You can download the tools from http://msdn.microsoft.com/en-us/data/hh297027.
Visual Studio 2012 ships with support for both ASP.NET MVC 3 and ASP.NET MVC 4 projects. ASP.NET MVC 2 projects are not supported for round tripping in Visual Studio 2012 and therefore you will need to upgrade your MVC 2 projects to MVC 3 projects before opening them in Visual Studio 2012.
To help with the upgrade process you can use the ASP.NET MVC 3 Application Upgrader available from the ASP.NET Codeplex site at http://aspnet.codeplex.com/releases/view/59008.
In a similar manner to ASP.NET MVC 2 projects, Silverlight 3 projects and prior will not be supported for round tripping with Visual Studio 2012. You will need to upgrade these projects to a later version of Silverlight first.
Visual Studio 2012 supports both Silverlight 4 and Silverlight 5 projects and you will be prompted for the version of Silverlight to use when you create a new project.
The Using the new SQL Server Data Tools recipe in Appendix,Visual Studio Pot Pourri
As you would expect, with a new Visual Studio version there comes a number of changes to how windows are managed. The changes that have been made have been done with the intention of reducing the clutter in your editing workspace and making the development experience one that is more focused on what you are doing.
Open either the
VS2010_Web solution we have been using or use a solution of your choice.
Ensure that the Solution Explorer is open.
In Solution Explorer locate the
Default.aspx.csfile in the
VS2010_Webproject and double-click it. The source file will open in the main window area as with previous versions of Visual Studio; however you will now notice that the document tab features a pin icon next to the tab name as you can see in the following screenshot. You'll use that pin in just a few steps.
Click on the Default.aspx.cs tab to select it and then click on the pin. The pin will change to point downwards indicating that the document is now pinned. Visual Studio 2012 will keep pinned tabs visible in the tab well even when you have so many open that Visual Studio starts hiding tabs. The pinned document tab will be moved to the left next to any other pinned documents you may have open.
Right-click the Global.asax.cs document tab and click on Close All But This to close all open documents except for the one currently selected. This will include closing any pinned documents.
Reopen both the
About.aspx.csfiles that you closed by double-clicking on them in Solution Explorer.
One of the usability problems with document tabs in Visual Studio 2010 was that you could accidentally float documents by double-clicking a document tab. In Visual Studio 2012 this behavior has changed. Double-click on a document tab of your choice and notice how Visual Studio sets the focus to that tab instead of floating it. Much better!
Press Ctrl+Shift+F to open the Find in Files dialog. Enter Class in the Find what field and ensure Look in is set to Solution, then click on Find All.
The preview tab shows the contents of the currently selected document if it is not already open. In the Find Results 1 window select a result from
Login.aspx. It will now be opened automatically in the preview tab and the
ChangePassword.aspxdocument will be closed.
Assume you now want to keep
Login.aspxopen for a while. Either click the Keep Open icon in the tab or change the contents of the file. Any document in the preview tab that is changed is automatically promoted to a normal tab.
Visual Studio will move the document from the preview tab area into the main tab area. The color of the tab will also be changed from purple to blue indicating that the tab is now a normal document tab.
Pinning documents works much like pinning does in any other part of Visual Studio and makes it very handy for keeping those documents you are working on regularly within easy reach, especially when you have many documents open at once.
The preview document tab is a great way to prevent tab clutter and becomes very useful when debugging deeply nested code. For example, say you have a section of code where a result you are expecting from a high-level method call is incorrect, and the source of the error actually is from a much lower-level method called at the end of a chain of four other intermediate methods, each from a different class. The odds are that you aren't really interested in those intermediate classes. Sure, you're happy to see those classes as you step through them on the way to the source of the problem, but you don't want to keep the documents open once you've moved past them. The preview tab means that during debugging, these class files are only opened temporarily, not permanently, helping you focus on just the code you are genuinely interested in and preventing the tab well filling up with documents you just aren't interested in.
As always there are ways to customize the behavior of the document tabs in Visual Studio.
The preview tab isn't restricted to just the find results window. It can also be used from within Solution Explorer. If you activate the Preview Selected Items button in the Solution Explorer toolbar then every item you click on will be opened in the preview tab automatically.
Navigating to the Tools | Options menu in Visual Studio will show the following dialog box:
There are a number of options in here that let you control how the tabs behave. With Visual Studio 2010 Productivity Power Tools many developers found different ways to configure their tab well to get the experience they wanted, and while not everything from the power tools came across to the final Visual Studio 2012 product, a number of features most certainly did. Feel free to experiment with the Tabs and Windows settings to get Visual Studio working the way you like it most.
Visual Studio has a number of new commands in this release, combined with all the previous Visual Studio commands. Well, that makes for a lot of commands to try and keep in your head at once. Why take those brain cells where you stored all your sci-fi movie quotes and overwrite them with the intricate details of Visual Studio commands, when Visual Studio 2012 gives you an easy way to find the commands you're looking for with nothing more than a quick keystroke and a keyword or two? That's a win-win situation right there!
Let's see how this works.
Start Visual Studio 2012 and either open a project of your choice or the
VS2010_Web project we've been using throughout this chapter.
Have a look at the top-right hand side of the Visual Studio editor window and you will see the new Quick Launch search box.
For maximum speed the fastest developers try to minimize mouse usage and you're going to do the same for this recipe, so instead of clicking in the textbox to activate, press the shortcut key (Ctrl+Q). You will see that the Quick Launch box now activates and is waiting for your input.
Assume you want to open a file but you can't remember what the command is or where it might be hiding amongst all those menus. Yes, there's a really obvious File menu and it is the obvious place to look, but for the sake of the exercise pretend that you had almost no sleep last night, that you've just arrived at work today, that the coffee hasn't taken effect, your brain hasn't yet kicked into motion, and that you've forgotten where the open command lives.
With the Quick Launch box active, enter the word
openwithout pressing Enter and wait a moment for the search results to appear:
Take a look at the results. You'll see that not only are there the commands you might reasonably expect to see from the File | Open submenu, but also other commands you might not have realized existed, such as Open Developer Account (only if you are working in Windows 8). You can also see that the search results include Visual Studio options, not just the commands that are available.
Remembering that you are going through this recipe using only the keyboard, use the up and down cursor keys to navigate to a result of your choice and press Enter to execute that command.
You now need to open the
Default.aspxpage using just the Quick Launch tool and only with the keyboard. To do this you need to navigate to the
Default.aspxfile. Hit the Quick Launch shortcut (Ctrl+Q) and type in
navto see what commands are available:
Press cursor down until the Edit | Navigate To entry is highlighted. Press Enter to activate the command.
In the resulting dialog box type
defaultand wait momentarily for the search results to appear. Press cursor down until Default.aspx is highlighted and then hit Enter.
Now that you have the file open press the Quick Launch shortcut (Ctrl+Q) and type in default. In the search results you will not only see matching Visual Studio options, but also commands that perform different operations on the open
Default.aspxfile. That's pretty cool!
When there are a large number of results Visual Studio will show you a subset of those results based on what it deems to be the best matches for your search.
Use the Quick Launch control and enter the value
debug. You will see a subset of all available results.
To see the full set of results simply press the Quick Launch shortcut again (Ctrl+Q). The expanded results are then shown.
The Visual Studio team realized that the growing number of commands in Visual Studio was overwhelming for many people. Those people needed an easy way to quickly locate a command they were after without dredging through their memory cells for where that command might be hiding, or to waste time browsing through the various Visual Studio menus until they found it.
The Quick Launch search box is the recommended interface to find the commands you need, but don't use often enough to remember where they are.
If you're a developer looking to improve your skills, be more productive, and maximize your use of the IDE, then knowing how to navigate your way around Visual Studio 2012 with nothing but the keyboard is a great goal to aim for. It minimizes the movement of your hand between the keyboard and the mouse which saves you time as well as gives you a greater sense of mastery over your tools. As a bonus, you'll also be able to impress your colleagues and dazzle them with your awesome Visual Studio skills!
If you find you use a command regularly, take the time to learn its specific shortcut key so you don't have to search for it every time you want to use it. For everything else, as long as you remember the name of the command or something close to it, you can use the Quick Launch control to find and execute that command without wasting time hunting through menus, and you won't have had to move your hands off the keyboard to do it.
As a code base grows it's important to be able to understand and find things quickly in your solution. The Solution Explorer we were used to in Visual Studio 2010 was good for being able to understand how files were organized into the various projects of a solution, but it didn't do much more than that.
With Visual Studio 2012, Microsoft has revisited Solution Explorer and given it an overhaul. It still contains all the functionality you know from the old Solution Explorer and adds to that a range of new features intended to make navigating and searching within your solution a more powerful, yet simpler experience.
We'll begin with navigating through our solution. Locate the
Default.aspxpage in the
VS2010_Webproject and click the arrow next to it so that its contents are displayed. As you would expect there is a code behind file and a designer file.
Look at the
Default.aspx.csfile. You can see that there is a small arrow next to it just as there was for the
Default.aspxpage. Click on the arrow.
Visual Studio 2012 expands the file to show its contents and in the case of a code behind file those contents are the class definitions it contains. Classes have methods and properties in them, so click the arrow next to the
_Defaultclass to see the methods inside it. Since the
VS2010_Webproject is just a shell there is only an empty
Page_Load()method as shown in the following screenshot:
Now select the
IService1.csfile from the
VS2010_Web.Servicesproject and expand it to see its contents. You will see that there is both an interface definition in this file (
IService1) and a class definition (
CompositeType) as shown in the following screenshot:
Right-click on the
IService1interface and click Derived Types to see what classes implement this interface.
Solution Explorer will change views to show you the types that either implement this interface or inherit from it, as shown in the following screenshot. Click on the back button (showing the blue background) to return to the standard Solution Explorer view.
Right-click on the
IService1interface and choose the Is Used By option to see where the interface is currently being used. As with the Derived Types option you will see Solution Explorer change context to only show the interface and where that interface is used in the solution, including line and column numbers.
Return to the regular Solution Explorer view by clicking on the home button.
At this point you know how to navigate using Solution Explorer and you have already used the existing Navigate To feature from Visual Studio 2010 in the previous recipe (Finding Visual Studio commands) when opening a file. With the enhancements to Solution Explorer you can locate files in much the same way as with the Navigate To command, albeit with a slightly different user experience.
Click in the Search Solution Explorer textbox at the top of Solution Explorer or use the default shortcut key of Ctrl+; (Ctrl+Semicolon).
servinto the textbox and wait a moment for the search results to display. The results should look similar to the following screenshot. You can see not only the file names that match the search term, but also any matching references, classes, and methods.
The Navigate To command from Visual Studio 2010 was a fantastic addition to Visual Studio. It had problems in large projects with many search matches since the location of a match was embedded in the result itself making it hard to locate the specific match you were after.
The new Solution Explorer search tool provides similar results to the Navigate To command, but having the location of a match represented in the tree view makes it very easy to quickly identify the specific match you are interested in.
It's worth mentioning a few other things about searching within your solution.
Assuming you have the Preview tab enabled for Solution Explorer then as you navigate using Solution Explorer to various classes and methods you may have noticed that the document preview tab was updating and showing exactly where the selected class, method, or property is declared.
This makes it easy to see what the code is doing without the need to specifically open the file or scroll through a source file to see what code is actually inside a method, class, or property.
You now know how to search within your solution for classes and methods and how to drill down into various items, but what if you are looking for a specific variable or piece of text in your code?
Once again, open the same solution we have been using in the other recipes; either the
VS2010_Web solution or a solution of your choosing.
IService1.csfile from the
Ensure that focus is set to the code window, not Solution Explorer, and press Ctrl+F. This will trigger the new search tool as shown in the following screenshot. Alternatively you can access this from the menu using Edit | Find and Replace | Quick Find.
Enter the text
serviceinto the textbox. As you type you will notice that Visual Studio matches the search term against the code on the fly and all the matches are highlighted as shown in the following screenshot. This makes it very quick and easy to see if you have located the code you are looking for.
Since the search term has been matched multiple times it makes sense that you might want to quickly move between those matches. Press F3 to move to the next match and press Shift+F3 to move to the previous match.
Change the search term to
serviceHost. Because there is no match in the current file the color of the border of the search box changes to red.
Given that you don't have a match in the current file it's worth expanding the scope of the search. Click on the expand icon on the left side of the search box and change the scope from Current Document to Current Project. When you do, you will notice that the red outline disappears indicating a match has been located.
Press F3 to find the next match. Note that the next matching file is opened as a normal document tab, not in the preview tab.
By making the search dialog appear within the window that currently has focus, the user experience for both find and replace is much improved. It takes up only a small amount of space, it doesn't use a floating window, and the search box goes away when focus moves to a different window or document within Visual Studio, keeping the visual clutter low.
When you clicked the button to expand the Quick Search tool window you may have noticed the option to replace text. This works as you would expect. Simply enter the text you want the search term replaced with and instead of pressing Find Next use the Replace Next button.
It's still there! Well, kind of. The changes in making search more context-specific mean that the Quick Find dialog is gone. If you have the focus somewhere other than a document tab and you press Ctrl+F or use the Quick Find command then the Find and Replace dialog will be shown with the Find in Files option selected. This is the same dialog box you will be familiar with from previous Visual Studio versions.
The graphics tools in previous versions of Visual Studio might best be described as mediocre. However, in Visual Studio 2012 they have been revisited to provide some much needed updates.
Visual Studio 2012 in no way replaces a full featured graphics package, however if you just need to tweak an image or make some simple changes then Visual Studio can be very useful.
In this recipe, you'll create an image that you could use in the website project we've been using throughout this chapter.
Right-click on the
VS2010_Webproject and select Add | New Folder. Call the new folder
Imagesfolder selected, from the File menu choose New File or press Ctrl+N. From the New File dialog select Graphics | PNG Image (.png) and click on Open.
The new Visual Studio Graphics Designer will appear.
Save the image to the
Imagesfolder of your project using File | Save As with the default name of
In Solution Explorer turn on the Show all files option and locate the image you just saved to the
Imagesfolder. Include it in the project by right-clicking it and choosing the Include In Project option.
Select the Brush tool so you can draw on your wonderfully blank picture.
To set the color of the brush, ensure the Properties panel is open. If it isn't it can be accessed by pressing F4 or choosing View | Properties Window.
To select the brush thickness change the Width property of Appearance to a value of your choice, such as
Now draw on the canvas and start creating your next masterpiece! Experiment with the other options in the toolbar to get a feel for what the graphics tool provides. Just make sure you produce something better than this horrible effort!!
The new graphics designer is a DirectX accelerated design surface. You can alter the DirectX rendering output method to use software acceleration if, for example, you are using older hardware and are seeing graphics glitches. To switch, use the side toolbar and select Advanced | Graphics Engines | Render with D3D11WARP.
While the graphics editor is a much better editor that the previous resource editor, and even though DirectX acceleration means that the image editor can now work with very large, and complex images and a multitude of formats, it still isn't a match for a full featured graphics editing program. For advanced graphics needs, use a specialist tool.
Visual Studio 2012 doesn't simply provide the same 2D image editing options of the past with a new interface; it now provides for some more advanced techniques specifically designed for those who need to produce visually rich applications such as games or information visualization tools.
MIP mapping is a technique used in video games for texture mapping 3D models. A single image file is structured to contain a high resolution texture as well as multiple versions of the same texture at lower levels of resolution. When the game is running, a texture of the appropriate resolution is extracted from the image file and applied to the 3D model based on the distance the model is from the camera. The further away the object, the lower the resolution chosen.
You may have noticed when you were creating the PNG file that you also had the option to create a 3D scene.
Visual Studio 2012 supports the viewing, editing, and creating of AutoDesk FBX files, and also supports the viewing and editing (but not creating) of OBJ and Collada DAE files.