IntroductionWelcome| 00:03 | Hi! I'm Walt Ritscher, and I want to
welcome you to my Visual Studio 2010
| | 00:07 | Essential Training series.
| | 00:09 | If you write code for a Microsoft
platform, like Silverlight, Windows Phone, or
| | 00:14 | ASP.net, you should be using Visual Studio.
| | 00:17 | In this course, I'll show you
every corner of this powerful tool.
| | 00:20 | I'll start by looking at the code
editors and how to customize them to make
| | 00:24 | you more productive.
| | 00:25 | You will see how to break point your
code for debugging purposes and explore the
| | 00:29 | Watch, Threads, and other status windows.
| | 00:32 | You'll see the Data Source window
and discover the in-depth database and
| | 00:36 | server tools available.
| | 00:37 | There is a rich testing environment
included in Visual Studio, and I will cover
| | 00:41 | both unit and performance tests in this title.
| | 00:43 | There is also a section on working
with source control and another on
| | 00:46 | compiling and deploying
your finished application.
| | 00:49 | As a professional programmer, I rely
on remarkable tools available in Visual
| | 00:53 | Studio to build my applications, and
I'm eager to show you how they work.
| | 00:57 | So, let's get started
exploring Visual Studio 2010.
| | Collapse this transcript |
| Using the exercise files| 00:00 | If you are premium member of lynda.com,
or if you are watching this tutorial on
| | 00:04 | a DVD-ROM, you have access to the
exercise files used throughout this title.
| | 00:09 | I have copied the files to my
Desktop. Let's take a look.
| | 00:12 | Inside this Exercise Files folder, you
will see there is a folder for most chapters.
| | 00:17 | Inside the chapter, there is a
folder for most of the movies.
| | 00:20 | Let's take a look at Chapter 03, Movie 02.
| | 00:24 | Inside this folder, are two folders:
| | 00:27 | InfoReaders is the starter project,
and InfoReader-Done is the finished
| | 00:32 | version of the project.
| | 00:33 | I am going to double-click on InfoReader.
| | 00:36 | Inside here is an SNL file.
| | 00:38 | That's the typical way you
start up a project in Visual Studio.
| | 00:42 | Occasionally, the SLN file will be
in one of the subfolders, next to the
| | 00:47 | CSPROJ or VBPROJ file.
| | 00:49 | If you are a monthly subscriber or
annual subscriber to lynda.com, you don't
| | 00:54 | have access to the exercise files,
but you can follow along with your own
| | 00:58 | projects and assets.
| | 00:59 | Let's get started.
| | Collapse this transcript |
|
|
1. Getting StartedUnderstanding the Visual Studio versions| 00:00 | Microsoft produces a number of versions
of their popular Visual Studio product.
| | 00:04 | In this movie, I want to spend a couple
of minutes talking about the different
| | 00:07 | versions that are available.
Visual Studio also comes in a wide price range.
| | 00:11 | You might be surprised to learn that there
are free versions available for you to use.
| | 00:15 | On the other hand, you can get out
your corporate credit card and spend some
| | 00:18 | serious cash for the Ultimate
edition, which costs up to $12,000.
| | 00:23 | There are a number of price
points in between these two extremes.
| | 00:26 | Microsoft caters to every niche.
| | 00:28 | For this title, I will cover the
features that are available in Visual
| | 00:31 | Studio Premium Edition.
| | 00:34 | And I will sneak in a preview, a really
cool debugging tool in the Ultimate edition.
| | 00:39 | To start your journey with Visual Studio,
go to their download sites listed here.
| | 00:43 | There is also a link to a
product comparison page.
| | 00:46 | Can you imagine the fun you can have reading
about the differences between the versions?
| | 00:50 | The Professional and Premium
versions offer all the core features.
| | 00:54 | Either one will keep you happy while coding.
| | 00:56 | Premium also offers automated UI
testing tools, special database projects, and
| | 01:01 | code analysis tools.
| | 01:03 | If you have got the money, then
the Ultimate edition is your baby.
| | 01:07 | It contains every conceivable tool you
will ever want to utilize as a developer.
| | 01:12 | It also has the price tag to match.
| | 01:14 | The Test Professional version
is targeted at your QA team.
| | 01:19 | It contains test planning and
test tracking workflow tools.
| | 01:22 | It also includes manual testing tools,
and works with the new IntelliTrace
| | 01:26 | features, which I will
cover in the debugging chapter.
| | 01:28 | Team Foundation Server is
the collaboration backend.
| | 01:31 | This server enables your team to
integrate all of the individually deployed
| | 01:35 | Visual Studio tools.
| | 01:36 | TFS contains centralized databases for
storing project requirements, work items,
| | 01:42 | it can track bugs, and has
a source code repository.
| | 01:46 | It has automated build tools
available, and can be configured to have web
| | 01:50 | portals for each project.
| | 01:52 | A new tool in the Visual Studio
Toolbox is the Team Explorer Everywhere.
| | 01:56 | This tool exists to
collaborate with Java developers.
| | 02:00 | I copied a portion of the comparison
matrix on the Microsoft web site to this slide.
| | 02:05 | Take a look at the Debugging
and Diagnostics section here.
| | 02:08 | This shows that if you buy the
Professional edition, you have the basic debugger.
| | 02:12 | But if you fork out the money for the
Premium or the Ultimate edition, you also
| | 02:16 | get these additional tools.
| | 02:18 | When you purchase a version of
Visual Studio, you also get an MSDN
| | 02:22 | subscription level.
| | 02:23 | Having MSDN is cool, because it gives access to
the software download library. The more you pay,
| | 02:29 | the more software licenses you get.
| | 02:31 | For example, in the Ultimate
edition, you get business software.
| | 02:35 | You can download Office 2010 or Project 2010.
| | 02:40 | A couple of months ago, I was working
on a project that required a legacy copy
| | 02:44 | of Access 2003, and there it was in the library.
| | 02:47 | If you need Design tools, look for
Expression Blend or Expression Design.
| | 02:51 | In the Developer section, there are
copies of Visual Studio going back ten
| | 02:55 | years, plus other tools like FoxPro
Classic and even 1990s era versions of
| | 03:00 | Visual Basic, and to sweeten the deal,
you get access to all the standard Windows
| | 03:04 | operating system and Server products too.
| | 03:07 | Microsoft is also interested in
bringing the power of .NET to the student and
| | 03:11 | hobbyist programmer, so they created
the Express editions of Visual Studio.
| | 03:15 | Go to the Express web site to learn
more about these free, yet powerful tools.
| | 03:20 | Yes, low cost all means that they are
lacking features that you would find
| | 03:23 | in the paid versions.
| | 03:24 | Here is a listing of the free editions.
| | 03:27 | There is one for each of the major languages,
| | 03:29 | for example, C#, Visual Basic, and C++.
| | 03:33 | There are also versions for
specific types of development,
| | 03:36 | for example, the XNA Game edition.
| | 03:40 | There are so many choices.
| | 03:41 | I am certain you will find
one that fits your needs.
| | 03:43 | My personal favorite is Visual Studio
Ultimate, but that's also an easy choice
| | 03:47 | for me because it's
included in my MSDN subscription.
| | Collapse this transcript |
| Setting up your developer computer| 00:00 | This movie discusses what it takes to get
your computer ready to use Visual Studio.
| | 00:05 | There are a number of versions of
Visual Studio available for you to download.
| | 00:08 | The first movie in this chapter
provides more details about these editions.
| | 00:12 | Regardless of which one you choose,
you need to download the installer to
| | 00:15 | your local computer.
| | 00:17 | Like most .NET applications, you can
install Visual Studio 2010 on the same
| | 00:21 | computer as older versions--for example,
Visual Studio 2008--and they will work side by side.
| | 00:27 | It's not really necessary for you to
have Visual Studio 2008 on your computer
| | 00:31 | however, because Visual Studio
2010 supports the same project types.
| | 00:36 | For certain types of development, there
are additional tools that you must install.
| | 00:39 | They are not part of the core install,
because they shipped at a later time.
| | 00:43 | Here is a couple of additional
tools you might consider installing.
| | 00:47 | The first one, Silverlight 4 Tools,
configures your version of Visual Studio to
| | 00:51 | create the newest version of Silverlight.
| | 00:53 | The second one is called
Silverlight toolkit for Silverlight 4.
| | 00:56 | This contains additional controls
and tools for Silverlight development.
| | 01:00 | An exciting twist in the mobile phone
wars is Microsoft's new Windows Phone 7.
| | 01:05 | To write phone apps, you will have
to install the phone developer tools.
| | 01:08 | This supplement to Visual Studio
provides the tools you need to create
| | 01:11 | Silverlight and XNA phone applications.
| | 01:13 | It even includes a software emulator of
the phone operating system, so that you
| | 01:17 | can test the software
without having the physical phone.
| | 01:20 | Since it takes about an hour to install
Visual Studio, I won't bore you with the
| | 01:23 | actual installing of this
software on my computer;
| | 01:26 | instead, let me walk you
through some of the screenshots of the
| | 01:29 | installation process.
| | 01:30 | This is the initial screen you
will see when you start the installer.
| | 01:33 | Click the Install Microsoft
Visual Studio 2010 link at the top.
| | 01:37 | Next, it will show you the Setup screen
asking you if you want to participate in
| | 01:41 | the Improve Setup experience.
| | 01:43 | I have chosen to click the Yes check box here.
| | 01:46 | On this screen, I have to
accept the licensing terms.
| | 01:50 | It also tells me which items I
will be installing on the left column.
| | 01:54 | Next, like most installers, it
tells you the disk space necessary.
| | 01:59 | There are two basic installs: Full and Custom.
| | 02:01 | I am choosing the Full install. And as
you can see that the bottom, it says I am
| | 02:05 | going to need 5.9 GB of space on my hard drive.
| | 02:09 | You can pick Custom, and then you can
disable installing certain components or
| | 02:14 | adding non-standard components.
| | 02:16 | For instance, some of the C++
tools may not be installed by default.
| | 02:21 | Now the installation starts.
| | 02:22 | You can see a list of all the steps
that it is going to perform, and at the
| | 02:25 | bottom of the screen is a progress bar
showing you where you are in this current step.
| | 02:30 | And finally, I get the success
screen, which says I have installed the
| | 02:33 | application successfully.
| | 02:35 | At this point, I would suggest clicking
on the Install Documentation button, so
| | 02:39 | that you get the appropriate
help files installed as well.
| | 02:42 | Then this screen asks me
where I want to install my help.
| | 02:44 | I think this is pretty self-explanatory.
| | 02:47 | Here, I can pick which help files I want
to install by clicking on the add links.
| | 02:51 | Once you have finished the
install, you can run Visual Studio.
| | 02:54 | The first time you run, you
will be greeted with this screen.
| | 02:57 | Your decision in this dialog affects
the positions of the tool windows and the
| | 03:01 | keyboard shortcuts that are available to you.
| | 03:03 | For this course, I am using
the General Development Settings.
| | 03:07 | Don't worry if you have
picked one of the other choices.
| | 03:09 | I will show you how to
change these settings later.
| | 03:11 | You have got some work ahead of you
to get your Visual Studio installed.
| | 03:15 | I would say that you will need an
hour or so to download and install the
| | 03:18 | required files before you are ready to go.
| | 03:20 | But once you are over that hurdle,
you will be able to create some
| | 03:23 | remarkable applications.
| | 03:24 | I have already got the tools installed,
so let's move on to the next chapter.
| | Collapse this transcript |
|
|
2. Exploring the Visual Studio Workspace (IDE)Creating a Visual Studio project| 00:00 | In this movie, I will spend a few
minutes looking at how to create a basic
| | 00:03 | Visual Studio project.
| | 00:05 | Elsewhere in this series, I have
details on creating the different kinds of
| | 00:08 | project templates, and provide movies
showing how to create an application
| | 00:12 | from start to finish.
| | 00:14 | My goal in this movie is to show what
happens the first time you run Visual
| | 00:18 | Studio, and how to create a basic project.
| | 00:21 | How are you going to start Visual Studio?
| | 00:24 | Okay, that was rhetorical question.
| | 00:26 | You know what to do.
| | 00:28 | In Windows 7, I am going to go to the
Start menu, and then I am going to look in
| | 00:32 | All Programs, and I am going to
find my copy of Visual Studio.
| | 00:37 | That's relatively easy to do because I
have just installed it on this computer
| | 00:41 | and Windows 7 is
highlighting that folder in yellow.
| | 00:43 | I will open this up, and I will find my
Microsoft Visual Studio 2010 shortcut.
| | 00:51 | Before I click on this link though, I
would like to add a shortcut to my Start menu.
| | 00:54 | That's really easy to do in Windows 7, by
right-clicking and choosing Pin to Start menu.
| | 01:01 | The next time I go to the Start menu,
it's available up here at the top.
| | 01:09 | Let's run the application.
| | 01:10 | I will click on the link, and after
about 3 or 4 seconds, Visual Studio starts
| | 01:16 | and shows me the Start screen.
| | 01:19 | There are times when Visual Studio needs
administration privileges to complete a task.
| | 01:24 | Here in Windows 7, that is easy to accomplish.
| | 01:27 | I am going to go back to the Start menu,
go up to my shortcut, and I am going to
| | 01:32 | right-click and choose Run as administrator.
| | 01:40 | And now I want you to look on the title
bar of these two versions of Visual Studio.
| | 01:45 | The one in front says
Administrator in parentheses.
| | 01:48 | That's how I know that this is
running with elevated privileges.
| | 01:53 | For today though, I only need to one copy of
Visual Studio, so let me close this top window.
| | 01:55 | There is one more thing I would like to
show you before we create our first project.
| | 02:03 | Visual Studio and .NET ship
with dozens of additional tools.
| | 02:07 | Let's go back to the Start menu, and go
to All Programs and then back into the
| | 02:13 | Microsoft Visual Studio 2010 folder.
| | 02:16 | You can see, in here
there are lots of sub-folders.
| | 02:20 | There is one for Microsoft Windows SDK tools.
| | 02:23 | There is one for Team Foundation Server tools.
| | 02:26 | Down here is the Visual Studio tools folder.
| | 02:29 | As you can see, there are
lots of tools to work with.
| | 02:31 | The one I want to look at is this
one here: Visual Studio Command Prompt.
| | 02:35 | For our convenience, there is this special
command prompt hiding here in the Start menu.
| | 02:39 | I am going to run it and see what it does.
| | 02:43 | Look at the top of this window.
| | 02:46 | Up here, it says Setting environment for
using Microsoft Visual Studio 2010 x86 tools.
| | 02:52 | What this does is it brings into scope
all of the different programming tools
| | 02:57 | that are part of the Visual Studio family.
| | 02:59 | Now I can go out to the prompt
and run those from a command line.
| | 03:03 | So I am going to go up to the
top folder by typing in cd\.
| | 03:08 | And then I am going to type in
the name of one of these tools.
| | 03:10 | There is a tool called MSBuild for instance.
| | 03:11 | When I press Enter, it shows that it
found MSBuild, and now it's asking me for
| | 03:20 | additional information.
| | 03:23 | For another example, here is a
tool called Visual Basic Compiler.
| | 03:27 | Again, I will press Enter.
| | 03:28 | It finds the tool, and since I didn't
provide the rest of the settings, it just
| | 03:33 | shows me the help file.
| | 03:34 | We are going to be using this tool
occasionally throughout this course.
| | 03:39 | For now though, let me close the command window.
| | 03:41 | Finally, we are ready to
create our first project.
| | 03:46 | There are a couple of ways
to do that from this screen.
| | 03:48 | One, I can click on this New Project
hyperlink. Or if I would rather, I can go
| | 03:53 | up to File > New, and then
choose this Project menu.
| | 03:58 | Today, I am going to use the hyperlink.
| | 04:00 | So I am going to go back to the
Start screen and click New Project.
| | 04:03 | Visual Studio shows me a variety
of different project templates.
| | 04:09 | I have more details about
these later in the course.
| | 04:12 | There are Visual Basic templates
up here, C# templates, and many more.
| | 04:19 | Today I am going to look in the Visual
C# and then the Windows section, and I am
| | 04:25 | going to create a basic Console Application.
| | 04:27 | I will leave the settings as the
default, and then I am going to click on OK.
| | 04:35 | After a few seconds, Visual Studio loads the
project template and shows me my Code window.
| | 04:43 | I would write the code, I would compile
the code, I would run the application,
| | 04:46 | but those are topics for another movie.
| | 04:49 | That was easy, wasn't it?
| | 04:50 | Coming up in the next movie is the
thrilling story on how to use the Solution
| | 04:54 | Explorer over here on the
right edge of the screen.
| | Collapse this transcript |
| Working with Solution Explorer| 00:00 | A complex business application might
consist of dozens of projects, thousands
| | 00:04 | of code files, and hundreds of ancillary items.
| | 00:08 | With so many files loaded into Visual
Studio, you need a way to understand the
| | 00:12 | chaos and navigate through your assets.
| | 00:14 | The Solution Explorer is the
tool that tames this potential mess.
| | 00:19 | I'm inside Visual Studio, and I am going
to create a brand-new project, and then
| | 00:25 | we are going to look at it over here in
the Solution Explorer, which is usually
| | 00:29 | docked to the right edge of the screen.
| | 00:31 | Before I talk about Solution Explorer,
let me talk about the difference between
| | 00:35 | a solution and a project.
| | 00:37 | A solution, in .NET terms, is an item
that can contain zero or more projects.
| | 00:45 | A project, in .NET terms, is a section
that contains many files that are used to
| | 00:52 | construct a .NET assembly.
| | 00:55 | Let me show you how that works.
| | 00:57 | I'm going to go to the File
menu and choose New > Project.
| | 01:01 | I am going to create a C# console application.
| | 01:07 | Down here at the bottom of the
screen, I can give my project--which is a
| | 01:11 | container for files and
compilation instructions--a name.
| | 01:15 | I am going to call this one
FirstConsole, and I am going to save it out in my
| | 01:21 | Exercise File folder.
| | 01:22 | So I am going to choose Browse, and
then I will go to my desktop and find
| | 01:29 | Exercise Files, chapter 2.
| | 01:34 | And I am going to create a new folder
in here, 02, since we are in the second
| | 01:39 | movie in this chapter.
| | 01:40 | And then I will select the folder. Okay.
| | 01:47 | Now here is the key part. Down at
the bottom is the name of the Solution.
| | 01:51 | The Solution contains zero or more projects.
| | 01:54 | In this case it's going to create a
project called FirstConsole, and the name of
| | 01:56 | the solution will be exactly the same.
| | 01:59 | Over here you can see that I have
a choice to create a directory for
| | 02:03 | the solution files.
| | 02:04 | There are typically two files affiliated
with a solution: an FLN file and an SUO file.
| | 02:10 | Those will go in a separate directory.
| | 02:12 | I am going to click OK,
and there is our project.
| | 02:17 | It's a console application.
| | 02:19 | It shows the FirstConsole
project. In the Solution Explorer,
| | 02:23 | it's inside the FirstConsole Solution.
| | 02:25 | Another common pattern is to
create a stand-alone solution first.
| | 02:30 | Let me show you how to do that.
| | 02:32 | File > New > Project.
| | 02:36 | Then I will scroll down to the Other
Projects Types node and then I am going
| | 02:44 | to choose Visual Studio Solutions.
| | 02:46 | I will pick Blank Solution, and
then I'll choose the name of the
| | 02:51 | solution: FirstSolution.
| | 02:52 | If you are following along, click OK.
| | 03:01 | What's the difference?
| | 03:02 | Look inside the Solution Explorer.
| | 03:03 | There is only a solution.
| | 03:05 | There are no other files.
| | 03:06 | I'm going to add two projects to this solution.
| | 03:09 | My favorite way to do that is to right-
click here in the Solution Explorer and
| | 03:14 | choose Add, but there
are other ways to do this.
| | 03:16 | I am going to choose Add > New Project,
which takes us back to the same dialog
| | 03:21 | we saw a minute ago.
| | 03:23 | This time I'm going to create a Visual
Basic application. Again, I am going to
| | 03:27 | create a Console Application, so I
will call this project ConsoleVB.
| | 03:32 | I'll click OK, and over here in the
Solution Explorer, you will now see one project.
| | 03:44 | Note that the file ends with .vb, which
signifies that it contains Visual Basic
| | 03:48 | code. And now I am going to go up to
my Solution one more time, right-click,
| | 03:53 | choose Add > New Project.
| | 03:55 | I will pick the C# language, and then
Windows, and then I think I'll choose
| | 04:04 | the WPF Application.
| | 04:05 | I will call this one WpfAppCS and click OK.
| | 04:21 | So let's review what I've done.
| | 04:23 | I created a blank solution and
called it FirstSolution.
| | 04:26 | I added a Visual Basic Console
application to it, and I added a WPF
| | 04:30 | C# application to it.
| | 04:33 | Now I am going to save this file, and
then I am going to go out to my hard drive
| | 04:37 | and show you how the Solution Explorer
mimics the same settings that you have
| | 04:41 | inside your hard drive.
| | 04:42 | I am going to right-click on my
solution, and I'm going to go down and choose
| | 04:48 | Open Folder in Windows Explorer.
| | 04:50 | Here is what I want you to see.
| | 04:53 | There is an SLN file that you
typically use to load the solution, and that
| | 04:59 | contains instructions to tell it open
this folder, which contains the vbproj file.
| | 05:05 | The vbproj file contains all the
information about what to load for this project.
| | 05:11 | Also in the C# folder here, there is
a csproj file, which contains all the
| | 05:18 | instruction about what to load for that project.
| | 05:19 | So Visual Studio uses the SLN and
csproj and the vbproj and other proj
| | 05:25 | files to figure out what to actually
load into Visual Studio and to show in
| | 05:29 | the Solution Explorer.
| | 05:30 | Now, you can see there are some hidden
files, like there's this bin folder; that
| | 05:36 | doesn't show up over here
in the Solution Explorer.
| | 05:39 | That's because Microsoft has decided
they don't want to show you that by default.
| | 05:45 | I can enable that by coming over here
and clicking on the Show All Files, and
| | 05:49 | then you can see the bin and
object folder show up there.
| | 05:52 | So it basically mimics the file system.
| | 05:54 | Not only can you add files to your
project, but one more thing you can do is you
| | 05:59 | can add references of
other precompiled assemblies.
| | 06:04 | That's why I have this Reference folder.
| | 06:06 | I can right-click here and choose Add
Reference, and now I can go out and add
| | 06:11 | other .NET libraries of this project.
| | 06:16 | I have more details about how to do
this in other movies in this series.
| | 06:21 | Creating a project is easy, but it's
not something that you do everyday.
| | 06:25 | I'd say that you are more likely to
open an existing project over and over
| | 06:27 | again, so that's the topic of
the next movie in this chapter.
| | Collapse this transcript |
| Working with big projects| 00:00 | I don't know about you, but I get tired of
seeing trivial little projects and product demos,
| | 00:05 | so I decided to hunt down a
mammoth project to show you.
| | 00:08 | For this demo, I wanted big.
| | 00:11 | Not only do I want big,
I also want realistic.
| | 00:14 | So I turned to Microsoft Open Source.
| | 00:17 | Did you know that Microsoft maintains
an open source web site called CodePlex?
| | 00:21 | It's not as big as other open source
sites, like SourceForge, but it has a lot of
| | 00:26 | .NET projects available to download.
| | 00:28 | I picked this Silverlight
toolkit for today's demo.
| | 00:31 | This is production-level code
that is maintained by Microsoft.
| | 00:36 | To open a project in Visual Studio,
you go to File > Open and then choose the
| | 00:43 | Project/Solution menu item. Also, you can
use Ctrl+Shift+O. For this project, I'm
| | 00:51 | going to open the Exercise Files folder,
Chapter 2, movie 3, and then here is the
| | 01:00 | source for the Silverlight 4 toolkit.
| | 01:02 | I will drill down into Silverlight
toolkit and then the Source folder, and then
| | 01:08 | at the very bottom of the
screen, I am looking at the
| | 01:12 | Silverlight.Controls.toolkit.sln.
| | 01:16 | You might remember from another movie that
SLN is the file that loads the other projects.
| | 01:23 | It just so happens that this project
contains 23 projects and nearly 4,000 files.
| | 01:29 | On my dual core laptop, it takes
about five seconds to load the project and
| | 01:33 | when I compile the code, it takes
about 28 seconds to compile, the very first
| | 01:37 | time I compiled the code.
| | 01:39 | Now you might remember that the
Solution Explorer is there to let you navigate
| | 01:44 | through your projects and your source code.
| | 01:46 | So if I go over to my Solution Explorer,
you will just see that I have a folder
| | 01:49 | up here called Source.
| | 01:50 | Now on your machine, you might need
to collapse and open certain nodes
| | 01:54 | in Solution Explorer.
| | 01:55 | I have got a Source folder, I have got a
Themes folder, and I have got a Testing folder.
| | 02:00 | Inside the Source folder, I have
multiple projects. There's a Controls.Data.toolkit
| | 02:07 | project and many more.
| | 02:09 | The one I am interested in is down here.
| | 02:11 | It's called Controls.toolkit.
| | 02:13 | When I open that project, I see
multiple folders. And here you can see, for
| | 02:18 | instance, there is the DragDrop folder,
the Expander folder, and inside the
| | 02:23 | Expander folder is the actual code
that creates the Expander control.
| | 02:28 | And it appears that there are four files
here: three C# files and one XAML file.
| | 02:35 | Now if you are not familiar with XAML
or X-A-M-L, that is the markup language that's
| | 02:39 | used by Windows Font 7, Silverlight, and by WPF.
| | 02:47 | For today, I want to look at the GlobalCalendar.
| | 02:50 | Now what does this look like when you run it?
| | 02:51 | If I switch over to a browser--I
have Internet Explorer open here--
| | 02:56 | I've loaded this site:
silverlight.net and then this URL.
| | 03:00 | This is a sample site for the tools
I'm showing you currently. And if I open
| | 03:04 | this up and scroll down to the
Calendar section, this is the calendar control
| | 03:09 | that we are looking at in Visual Studio.
| | 03:11 | Again, this is production-
level code we are looking at here.
| | 03:14 | So switching back to Visual Studio,
you will see that I have a class called
| | 03:19 | GlobalCalendar, and I then
want to go GlobalCalendar.xaml.
| | 03:23 | Down here, I have a
GlobalCalendarButton.cs, which contains the source code
| | 03:28 | for that button, and the GlobalCalendarButton
.xaml file, which contains the XML markup.
| | 03:42 | There are a lot of things that you
can learn by looking at other people's
| | 03:45 | code, both good and bad.
| | 03:47 | I encourage you to explore this project
and learn more about what Microsoft is
| | 03:51 | doing with Silverlight.
| | Collapse this transcript |
| Taking a tour of the Integrated Developer Environment (IDE)| 00:00 | I spend a good portion of my day inside
Visual Studio when I am working on a project.
| | 00:05 | I need to be able to control the
layout of my windows, and get the screens
| | 00:08 | arranged the way I prefer.
| | 00:10 | During this movie, I'll show you how
to work with the windows inside Visual
| | 00:12 | Studio, and I'll share some layout
tips I've learned over the years.
| | 00:16 | I am inside Visual Studio, and I
have opened this TourIDE solution.
| | 00:22 | That solution contains one
project called ConsoleApplication1.
| | 00:26 | The first thing I want to show you
is how to open a Code Editor window.
| | 00:30 | I have two code files inside this project:
| | 00:33 | Book.cs and Program.cs.
| | 00:37 | If I double-click on Book.cs in
Solution Explorer, you'll see that it loads
| | 00:43 | into the center portion of the screen, and
it places a tab across the top of the window.
| | 00:48 | If I double-click on the Program.cs file,
you'll see that another tab is added
| | 00:54 | in the same location.
| | 00:55 | It's very easy to close these tabs.
| | 00:58 | There is an X button on each of the tabs.
| | 01:00 | Click on the X button to close it.
| | 01:03 | To reopen it, I need to go back to
Solution Explorer and double-click on Program.cs.
| | 01:09 | Occasionally, you'll end up
with many Code Editor files open.
| | 01:13 | In that case, they may not all
fit across the top of this area.
| | 01:18 | Microsoft provides a dropdown window
here that shows all of the available tabs.
| | 01:23 | If you can't see one of your tabs, be sure
and explore this area, to see of it's there.
| | 01:28 | Of course, in this application, I've only got two
tabs open, so I don't need to worry about that.
| | 01:32 | Visual Studio supports a very
flexible layout engine for their Windows.
| | 01:38 | I can take this Program tab, and I can pull
it out into the center of my viewing area.
| | 01:44 | Now I've got an independent window
floating there. And of course, I can size
| | 01:49 | this window if necessary.
| | 01:52 | I am on a Windows 7 machine, so if I
take this window and I slide it over to the
| | 01:57 | edge of the monitor, it will
automatically dock that to the edge of the window.
| | 02:03 | Now, keep in mind this is a Windows 7
feature, not a Visual Studio feature.
| | 02:07 | So if I let go, you see that window is
now docked to the side of my monitor.
| | 02:12 | If you're fortunate enough to have a
multi-monitor machine, you can take
| | 02:16 | this and slide it off onto the other
monitor, and then you can dock it over
| | 02:21 | on the other monitor.
| | 02:22 | It's hard to show docking on a
second monitor though in a movie, so take my
| | 02:26 | word for it, that will work.
| | 02:29 | Now I want to get this tab
back where it was before.
| | 02:33 | There is a couple of ways that I can get there.
| | 02:37 | One thing I can do is I can
double-click on this header.
| | 02:40 | When I double-click on the header, it
maximizes the view of this code window.
| | 02:46 | Watch, double-click, and now I have the
entire screen taken up with that window.
| | 02:51 | That's not what I wanted.
| | 02:52 | I wanted to put it back where it was originally.
| | 02:55 | Here is the tip I learned a while back:
| | 02:58 | you hold down the Ctrl key, and then
you double-click on the header. And there
| | 03:03 | it is, back in its original position.
| | 03:07 | The other way to get it back is to use
this visualizer that's sitting in the
| | 03:11 | center of the screen.
| | 03:12 | Do you see it there?
| | 03:14 | There is this floating icon that
looks like it has five work areas.
| | 03:19 | That represents the five different
places that I can put this window as I am
| | 03:22 | dragging it around the screen.
| | 03:25 | Look at the tip of my mouse.
| | 03:26 | When my tip of the mouse enters this
area, you'll see a blue highlight show
| | 03:32 | up in the background
| | 03:34 | that says now if I let go of this
mouse at this point, it's going to put that
| | 03:39 | tab on that section of the screen,
which is not exactly what I wanted.
| | 03:43 | I want to have it go back
where it was originally.
| | 03:47 | So, I'll come down here and
take a look at my other choices.
| | 03:50 | Split the screen vertically, and put it
either on the left or the right, put it
| | 03:55 | on the bottom half of the screen, or
move here to this center item, and that
| | 03:59 | places it back in the original position.
| | 04:01 | Again, I am going to let go when
my mouse is over that center point.
| | 04:04 | Now I can do the same thing with other windows.
| | 04:07 | Look over here in the Solution Explorer.
| | 04:09 | You see in the bottom of the
window here--there are three tabs.
| | 04:12 | Now your machine may look
slightly different than this.
| | 04:14 | On my machine, I have Solution Explorer,
a Team Explorer, and Class View down here.
| | 04:20 | I can take one of these tabs and pull it out,
just like I showed you for the code windows.
| | 04:26 | I can maximize it and dock
it to the left and the right.
| | 04:29 | If I want to put it back in this
location or let's say I want to put it in
| | 04:33 | another location like down here in this
Properties section, I just drag, keep my
| | 04:38 | eye on those visualizers, and I am
going to go down here and use the center
| | 04:43 | icon, and now I am going to add it as
a second tab to the Properties area.
| | 04:46 | Okay, Class View and Properties.
| | 04:49 | Pull it out, move out here,
and I can put it back.
| | 04:53 | Well, let me show you one more tip.
| | 04:56 | You'd likely to make a messier screen
at some point and can't figure how to get
| | 05:01 | something back where it belongs.
| | 05:03 | Luckily for you and for me,
there is a Reset switch for these
| | 05:06 | embarrassing moments.
| | 05:07 | The simplest way to handle this is to
just go up to the Window menu and choose
| | 05:14 | Reset Window Layout.
| | 05:17 | This sets it back to the default settings.
| | 05:20 | So now you can see the
Class view is back over here.
| | 05:25 | It also added another tab
that we didn't see originally.
| | 05:29 | The default views have this thing
called the Start Page open right there.
| | 05:32 | I'll close the Start Page tab.
| | 05:34 | There is another feature on the
Layout windows I'd like to show you now.
| | 05:39 | It's called the Auto Hide feature.
| | 05:41 | If you look on the left side of my
screen, you'll see that it says Toolbox
| | 05:45 | and Server Explorer.
| | 05:47 | Those are windows that are
automatically hid, or shrunk down to a very small
| | 05:50 | size, and placed on the left
or right side of the window.
| | 05:53 | If I move my mouse over one of them,
say the Server Explorer, I can see the
| | 05:59 | window temporarily. And if I move my
mouse away from the edge, it will go
| | 06:04 | back to where it was.
| | 06:05 | It's showing me that there is a window
I can look at, but it's not keeping it
| | 06:08 | permanently in my work area.
| | 06:10 | If I want to dock it to the side of the
window, I need to click on this Auto Hide button.
| | 06:15 | You'll also note that over here in the
Class view and in the Properties view,
| | 06:22 | there is an Auto Hide button.
| | 06:23 | These ones are showing, but if I where
to click on those Auto Hide buttons, it
| | 06:26 | would collapse those down to
the right edge of the screen.
| | 06:29 | Next, I want to show you,
what's called the Full Screen mode.
| | 06:33 | Sometimes, I just want to remove all
of the windows and just concentrate on
| | 06:36 | one window at a time.
| | 06:38 | To accomplish that, I go to View > Full Screen.
| | 06:44 | I don't know if you saw it, but all my
toolbars across the top also disappeared.
| | 06:50 | All I have now is the menu.
| | 06:52 | I work in my code for a while, and when
I am done with the Full Screen view, I
| | 06:56 | click here on this icon to
return back to the original view.
| | 06:59 | Currently, I have about six or seven
windows available for me to work with.
| | 07:05 | There are other windows that
are not shown by default.
| | 07:09 | You can find more about
these windows in the View menu.
| | 07:12 | For instance, there is the
task list and the Output window.
| | 07:17 | There is also the section called Other
Windows, which contains 12 or 15 other windows.
| | 07:23 | Also, when you're debugging an application,
you'll find some extra windows in the Debug menu.
| | 07:28 | I'll talk more about this
in the debugging chapter.
| | 07:30 | Right now, you can see I have
the Breakpoints, the Output, and the
| | 07:33 | Immediate window available.
| | 07:36 | For my last tip in this movie,
let's look at what happened when you do
| | 07:39 | a Ctrl+Tab keystroke.
| | 07:41 | I am going to hold down the Ctrl key,
and then I am going to tap the Tab key
| | 07:47 | without letting go of the Ctrl key,
like this--tapping the Tab key.
| | 07:52 | So basically, all of the files that I
currently have opened in the editor are
| | 07:56 | listed in the Active Files section,
and all of the windows I have currently
| | 08:00 | opened, are available
over here--the Tool windows.
| | 08:04 | So you can see that if I go to Server
Explorer by clicking here, my focus will
| | 08:08 | move over to the Server Explorer.
| | 08:10 | I have been using Visual Studio for a long time.
| | 08:14 | Each version gives me some new
IDE feature I can't live without.
| | 08:18 | I think my favorite new feature
in this release is probably the
| | 08:21 | multi-monitor support.
| | 08:24 | I have three monitors on my Desktop,
and this feature is invaluable, but there
| | 08:29 | are a lot of other good
features for you to find.
| | 08:32 | Have fun exploring, and let me know
which one you think is your favorite.
| | Collapse this transcript |
| Introducing drag-and-drop UI design| 00:00 | One of the major advantages of
designing UI in Visual Studio is the drag-
| | 00:04 | and-drop placement of elements during the
design of both rich client and web applications.
| | 00:09 | These elements are all available
in what is known as the ToolBox.
| | 00:12 | I am inside Visual Studio, and I've
opened a solution, appropriately called ToolBox.
| | 00:18 | Inside this solution, I
have three separate projects:
| | 00:21 | an ASP.NET project, a Windows
Form project, and a WPF project.
| | 00:26 | The reason I picked these three
project types is all three of them support a
| | 00:29 | drag and drop designer.
| | 00:30 | I am going to start by
looking at the Windows Form App.
| | 00:34 | I'm going to open this up, and I am
going to find this file called Form1.cs.
| | 00:38 | I'm going to double-click on it.
| | 00:40 | That will load what's called the
designer into this central area of my screen.
| | 00:45 | Now I can go to the Toolbox, which
currently on my machine is down here on this tab.
| | 00:50 | If you don't see it, you may need to
go to the View menu as a View toolbox.
| | 00:55 | It also may be docked to the edge.
| | 00:57 | In that case, you need to go over here and
click the Auto Hide button to make it visible.
| | 01:01 | I'm going to open the Toolbox, and
now I can do a drag-and-drop operation.
| | 01:07 | For instance, I can take this
button and drag it from the Toolbox onto
| | 01:11 | my designer surface.
| | 01:13 | The Windows Form designer has this little
rectangle that shows where I am going to drop it.
| | 01:19 | Now when I let go, it will add the button.
| | 01:22 | Now behind the scenes, Visual
Studio is writing some code.
| | 01:26 | It's not very obvious where
it's writing this code, however.
| | 01:30 | It's over here in the Form1.Designer.cs.
| | 01:34 | Other designer surfaces are better
at showing you where this code lives.
| | 01:37 | I'll show you that in a minute.
| | 01:39 | First, let me show you what happens when
you want to work with multiple controls.
| | 01:41 | I am going to go add another control.
| | 01:46 | This time I am going to
choose the TextBox control.
| | 01:48 | I will drag that over.
| | 01:50 | You can also double-click
on the Toolbox to add it.
| | 01:52 | You see that I get this blue line.
| | 01:55 | The Windows Form designer is using this
blue line to show me that I have aligned
| | 01:59 | the left edges of these controls.
| | 02:01 | So when I let go over the mouse
now, it will snap to that location.
| | 02:05 | Once I have an item added, I can click on it.
| | 02:09 | I get sizing handles--which
means I can change its size--
| | 02:13 | I can move it to a new location by dragging it
with my mouse, and of course, I can delete it.
| | 02:21 | Let me show you the WPF designer now.
| | 02:23 | I'll go to the Wpf project, find this
file called MainWindow.xaml, and double-click.
| | 02:34 | Immediately, you can see that the
designers are different from each other.
| | 02:37 | This designer has a white design area,
and at the bottom of the split screen is
| | 02:42 | a set of XAML, which is Markup Language.
| | 02:45 | Now watch what happens when I drag
a button from this Toolbox over to
| | 02:49 | the designer surface.
| | 02:52 | I get a plus symbol.
| | 02:53 | I get a blue outline.
| | 02:54 | When I let go, the button shows up,
but also in the markup section
| | 02:59 | you can see that it says
<Button, the Height = "23".
| | 03:04 | So what do you think what happen if I come
over here, and I typed in 60 for the height?
| | 03:08 | Right, the button changes size.
| | 03:10 | So it's a lot easier to see the
relationship between the text here and the
| | 03:15 | button than you get in
the Windows Form designer.
| | 03:18 | Something similar happens
in HTML and ASP.NET editing.
| | 03:23 | If I go over to my AspNet application
and open up a file called Default.aspx by
| | 03:31 | double-clicking, you see that
I have a HTML document here.
| | 03:37 | If I want to see the drag and drop
designer, I need to click on the Design tab
| | 03:40 | on the bottom of the screen.
| | 03:43 | Now I can go to my Toolbox, find a
control, like this button, and drag it over to
| | 03:50 | the designer surface.
| | 03:52 | Now if you look at the source code,
you'll see this line, <asp:Button. That shows
| | 04:00 | where it's instantiating, or
creating an instance of that control.
| | 04:03 | So you see how each designer has a
different way of working and representing
| | 04:08 | the underlying data.
| | 04:10 | I am going to return back
to my Form1.cs designer.
| | 04:15 | I'll click here in this tab.
| | 04:17 | Now, what I want to spend the next five minutes or
so looking at is what you can do with Toolbox.
| | 04:24 | For instance, I can rename the
elements and move them to new positions.
| | 04:29 | That's what I want to show you now.
| | 04:31 | The first thing I want to show you is
that there are sections of this Toolbox.
| | 04:35 | I currently have the Windows Form
designer selected, so the Toolbox
| | 04:39 | is context-sensitive.
| | 04:40 | It's just showing me only sections
of the Toolbox that have to do with the
| | 04:44 | Windows Form designer.
| | 04:46 | If I switch to the WPF designer, you
will see that the section is changed.
| | 04:51 | Now it has one for common WPF
controls and another for all WPF controls.
| | 04:56 | If I switch back to the Form1.cs, you
will see again that the Toolbox changes
| | 05:01 | because it's monitoring what I am
editing in the center of the screen.
| | 05:06 | I can go to my Toolbox, right-click, and
uncheck this List View, and what you will
| | 05:12 | see is I now have smaller icons.
| | 05:15 | It no longer has the Text view anymore,
so I have to hover over the icon to see
| | 05:18 | what control is, but I save a lot
of screen real estate by doing this.
| | 05:23 | To go back to seeing the Text view,
I right-click and choose List view.
| | 05:27 | You can create your own sections of the Toolbox.
| | 05:34 | You can go down to this General
section down here, and I can add other
| | 05:39 | third-party controls. If you happened to have
bought a third-party package of user
| | 05:43 | controls, you can go and pick the
Choose Items menu and then go out and
| | 05:50 | find those controls.
| | 05:51 | They might be listed here, or you might
have to go and click the Browse button.
| | 05:55 | But what you will do here is you will
find the control you want--say this one
| | 06:00 | right here--and then click OK, and
after a few seconds that control will now be
| | 06:05 | added to whatever section of the
Toolbox you currently have highlighted. In my
| | 06:09 | case, that would be the General section.
| | 06:10 | I don't want to do that for this demo, so
I am going to go ahead and click Cancel.
| | 06:13 | While you're working in the Toolbox,
you can rearrange the controls.
| | 06:21 | I can pick this control up
and drag it to a new section.
| | 06:26 | I can rename the control, by right-
clicking on it and choosing Rename.
| | 06:33 | If I make a mess of the Toolbox, I can
always click Reset Toolbox, which I am
| | 06:38 | going to do right now.
| | 06:39 | On my machine, this usually
takes 20 seconds or so to reset.
| | 06:48 | Some of the controls that you'll find in
the Toolbox are not visible at runtime.
| | 06:53 | Let me show you one of those.
| | 06:55 | It's called the Timer Control.
| | 06:56 | I am going to go to All Windows Forms,
scroll down to the T section and then
| | 07:04 | take this timer, and I am going to drag
it and drop it on my designer surface.
| | 07:07 | Now keep an eye on the
bottom half of the screen.
| | 07:09 | When I let go over the mouse button,
you'll see that Visual Studio adds a
| | 07:13 | special section called the component
tray, and because the timer is not visible
| | 07:18 | at run time, it puts a
timer on the component tray.
| | 07:21 | There is a companion window
that's often used when working with the
| | 07:23 | Toolbox and the designer.
| | 07:25 | I am talking about the Properties window, which
is over here on the right side of the screen.
| | 07:29 | It's usually underneath the Solution Explorer.
| | 07:32 | It's such an interesting tool that I
made an entire movie about it, and it's the
| | 07:36 | next one in this chapter.
| | Collapse this transcript |
| Working with the Properties window| 00:00 | A frequently used tool window in
Visual Studio 2010 is the Properties window.
| | 00:05 | This window is typically
docked to the right side of the IDE.
| | 00:08 | The hotkey for this window is F4.
| | 00:11 | I'm inside Visual Studio, and I have
this solution called PropertiesWindow open.
| | 00:15 | There is one project inside this
solution called WpfApp.
| | 00:19 | This is really a mixed application.
| | 00:21 | It has a Windows form and an HTML
page and also a XAML page in here.
| | 00:26 | I am going to start by
looking at the MainWindow.xaml page.
| | 00:29 | I'll double-click on this icon,
and that loads the designer.
| | 00:34 | Now I'm going to add a couple of
controls to this designer, by going to my
| | 00:37 | Toolbox and dragging the Button to the
designer surface, and then I am going to
| | 00:45 | drag the TextBox to the Designer surface.
| | 00:49 | Next, I am going to the show
you how the Property window works.
| | 00:51 | The Property window is over here in the
lower right-hand corner of the screen.
| | 00:55 | To make it easier to see, I am going to
detach it by dragging it out and moving
| | 01:00 | it into the main part of the window.
| | 01:04 | You may need to size it to fit on your screen.
| | 01:07 | Then I am going to sort alphabetically.
| | 01:10 | By default, when you first start Visual
Studio, the property grid is sorted by
| | 01:14 | Category view, which means it stores
the different property settings in these
| | 01:19 | categories, like Layout.
| | 01:21 | I don't like this view.
| | 01:22 | I prefer the alphabetical view, so I am
going to switch to the alphabetical view.
| | 01:27 | Then I am going to show you that when
you select an item in the designer, the
| | 01:33 | property grid automatically monitors
that control and loads the property grid
| | 01:38 | with the settings from that control.
| | 01:41 | So I'm seeing the properties of this button.
| | 01:44 | When I click on the text box, I'm
seeing the properties of the text box.
| | 01:48 | You can verify that by looking up
here and seeing that I'm working with a
| | 01:51 | text box, and the name of
the text box is textBox1.
| | 01:55 | There are a lot of
properties on the button class.
| | 01:58 | Let me select it again.
| | 02:01 | If I want, I can filter the property grid
to only show me a subset of the properties.
| | 02:06 | I do that by clicking in the Search
and typing in the first letters of the
| | 02:10 | item I am looking for.
| | 02:13 | For instance, I want to look for the
Width property, so I am going to type in "wid".
| | 02:17 | I see the Width property down here at
the bottom, but I also see that there was
| | 02:21 | a MinWidth and a MaxWidth property
that also contain those three letters.
| | 02:25 | When I'm done filtering, I can click on
this X button to go back to the entire view.
| | 02:32 | Each property is listed
here in alphabetical order.
| | 02:35 | One of the cool things about the
property grid is that when you select a
| | 02:40 | property, like this Background property,
| | 02:42 | it loads a custom editor
in this section over here.
| | 02:46 | Because the background uses a
brush, it's loading the brush editor.
| | 02:51 | So when I click on this dropdown, I
get a color editor for changing the color
| | 02:56 | value of the Button.
| | 03:00 | When I click on the Clickmode
property, I get a different editor.
| | 03:05 | Let's see what I have over here.
| | 03:07 | This is an enumerated value, so I
get in the list of the three possible
| | 03:10 | enumerated values for this property.
| | 03:13 | This is very handy.
| | 03:15 | If you like to edit your items
visually, this is super-easy to use.
| | 03:18 | There is an indicator down the center
of the property grid that shows whether
| | 03:23 | I'm using the default value or not.
| | 03:25 | This diamond here signifies I'm no
longer using the default value for the
| | 03:28 | background color of the button.
| | 03:30 | I can reset it at any point by
right-clicking on this icon and choosing Reset Value.
| | 03:36 | Now this only works in
Silverlight and WPF applications.
| | 03:40 | ASP.NET, the HTML editors, and other designers
use a different metaphor for this reset.
| | 03:46 | For instance, like the ASP.NET editor,
it shows if I'm using a non-default value
| | 03:52 | by bolding the text in this column.
| | 03:56 | Not only are there properties in this
button, but this button also contains events.
| | 04:02 | The property grid shows me the
events when I click on this tab here.
| | 04:07 | This lists every event that this
button is capable of listening for.
| | 04:12 | So if want to write some code for a
click event, I would go to this section,
| | 04:16 | and I would type in the name of the
function that I want here. Or let's say I
| | 04:19 | want to work with the mouse event, so
I'll go down to the Ms and find something
| | 04:23 | called MouseLeftButtonUp.
| | 04:26 | Now the way I can write code is I can
double-click on this event, and Visual
| | 04:31 | Studio will pick a name for me and
write the code. Let me show you.
| | 04:35 | I'll double-click.
| | 04:37 | Visual Studio switches me over to the
Code Behind window and shows that it wrote
| | 04:41 | a function called button1_MouseLeftButtonUp.
| | 04:45 | We'll learn more about
these in the Coding chapter.
| | 04:46 | I'm going to switch back to the designer.
| | 04:51 | Now I can see that this event has the
name of the event procedure listed here.
| | 04:56 | If I don't like that name, I can change
it here or I can suggest my own name.
| | 05:01 | Let me go to MouseEnter and type
in my own function name, like that.
| | 05:07 | I will press enter.
| | 05:10 | Visual Studio switches over to the Code
view and writes in this piece of code.
| | 05:15 | Notice how the name is the one that
I selected in the property grid.
| | 05:22 | When I switch back to the Designer view,
if I want to undo the event wiring, I
| | 05:27 | can come here and delete the word "myCode".
| | 05:31 | Now when that event fires, myCode will
no longer run, however, Visual Studio
| | 05:35 | does not remove the code
from the Code Behind window.
| | 05:38 | You see it's still here.
| | 05:40 | Let me put this property grid
back in its normal position.
| | 05:43 | To do that, you'd hold down the Ctrl
key and double-click on the header.
| | 05:48 | Now, it's back in its original position.
| | 05:50 | Then I'll switch back to the
designer, and let's review.
| | 05:54 | The property grid contains two sections:
| | 05:56 | Properties and the Events.
| | 06:00 | In the Events section, you can see the
names of the events, you can write code,
| | 06:04 | you can rename the code,
and you can remove the code.
| | 06:08 | If you're in the Property view, you can
monitor the properties of the selected
| | 06:13 | object and change them.
| | 06:15 | One thing I didn't mention earlier is
that you can select more than one item.
| | 06:19 | If I drag a second button over here,
and select the two of them, then the
| | 06:25 | property grid, when I make changes
over here, it's changing common properties
| | 06:29 | that are shared among both of these controls.
| | 06:32 | So if you are a visual person, you are
going to love using the Property window
| | 06:35 | to edit your UI items.
| | 06:37 | If you're not a visual person, you can
always hand-edit the HTML, the XAML, or
| | 06:42 | the C# code instead.
| | Collapse this transcript |
| Looking at Server Explorer| 00:00 | For this movie, I need to run
Visual Studio as an administrator.
| | 00:04 | Let me show you how to do that again.
| | 00:06 | I am going to go to my Start menu,
find my shortcut to Visual Studio,
| | 00:12 | right-click on it, and
choose Run as administrator.
| | 00:20 | Now that I'm inside Visual Studio, I
need to open a project, by going to File >
| | 00:25 | Open > Project/Solution.
| | 00:29 | Go to your Exercise File folder >
Chapter 2 > Movie 7 and then open the Server
| | 00:36 | Explorer folder and then load the
ServerExplorer.sln file and click Open.
| | 00:45 | The Server Explorer lives over
here on the left side of the screen.
| | 00:48 | It provides a way to explore hardware
resources on your local computer. And if
| | 00:52 | you are on a network, it also permits access
to computers and services on remote computers.
| | 00:57 | You can see, in my Server Explorer, one
computer, which is the local computer.
| | 01:02 | If I expand this node,
you'll see several sections.
| | 01:06 | There is the event logs, the management
classes, messaging queues, and many more.
| | 01:12 | I am going to show you today the Event
Logs and the Data Connection section.
| | 01:17 | There are lots more to explore in here, however.
| | 01:19 | Let's start with the event logs.
| | 01:22 | The event logs exist in Windows to allow
applications to store information about
| | 01:26 | what's happening as they are running.
| | 01:27 | If I expand his node, you will see
the different kinds of event logs.
| | 01:32 | I'm going to work with
the Application event logs.
| | 01:34 | When I open this up, you'll see each
application listed here, and if I open up
| | 01:41 | one of these--let's say this one done
here called Desktop Windows Manager--
| | 01:45 | you'll see that there are a number of
events that have been logged over the
| | 01:51 | last couple of days.
| | 01:52 | I'm going to write my own
event items into the event log.
| | 01:58 | I've already got a node from
earlier today called Essential Training.
| | 02:01 | I am going to write to that note.
| | 02:02 | Let me show you how easy that is to do.
| | 02:05 | First, I'm going to take this
application log, and I am going to drag it over
| | 02:09 | and drop it on the designer surface.
| | 02:14 | When I do that, Visual Studio adds an item to
this Component Tray down here at the bottom.
| | 02:18 | Let me close this Output window here.
| | 02:23 | Next, I'm going to write some
code on a button click event.
| | 02:28 | So I need to open my Toolbox, find the
Button control, drag it to the designer
| | 02:34 | surface, and then to write the code,
| | 02:37 | I am going to double-click on this button.
| | 02:42 | This is a Visual Basic application,
so I need to write a couple of lines
| | 02:45 | of Visual Basic code.
| | 02:46 | I'll start by naming my
source for the event log.
| | 02:50 | I'm going to call it
Essential Training, like that.
| | 03:06 | Then on the next line of code, I'm
going to write to the log file itself.
| | 03:18 | I'm going to say, "Hello log."
| | 03:26 | Then I'm going to type a comma, and
here I'm going to use EventLogEntryType,
| | 03:29 | and here I get to pick the warning level.
Is it an error, a warning, a piece of information?
| | 03:43 | I'm going to chose Error.
| | 03:45 | So let's review what I did again.
| | 03:49 | I wrote a piece of code for
the button click procedure.
| | 03:52 | I told the event log I am going to
be writing something into a log called
| | 03:56 | Essential Training and then the actual
data that I wrote was a string "Hello
| | 04:00 | log", and this is the type of log entry.
| | 04:03 | It's an error entry.
| | 04:05 | I'll run the application now by pressing F5.
| | 04:10 | Then I'll click on the Button.
| | 04:13 | After a few seconds, I'll close the
application. And then I'm going to go
| | 04:17 | look in the Event Log.
| | 04:19 | I can use the normal Windows Event Log
viewer, or I can go to Server Explorer,
| | 04:24 | find the Essential
Training section and open that up.
| | 04:29 | As you can see, I have three entries.
| | 04:31 | You're going to have more or less than I do,
depending on how often you've run your demos.
| | 04:36 | I've already run some demos earlier
today when I was setting up this computer,
| | 04:39 | so you can see that at 3:57 this
afternoon I wrote a test in there, and here is
| | 04:45 | the one we just wrote a
few minutes ago: Hello log.
| | 04:48 | Another favorite of mine in the Server
Explorer is the Data Connections section.
| | 04:53 | This allows me to add connections to databases.
| | 04:56 | Let me show you how easy that is.
| | 04:58 | I'm going to click here, Connect to Database.
| | 05:01 | I get to pick the kind of
database I am going to connect to.
| | 05:05 | For today's demo, I am going to
use a SQL Server database file.
| | 05:09 | I am going to click on Continue.
| | 05:12 | I am next asked what
information is required to find that file.
| | 05:19 | I need the file name, and
I need a login credential.
| | 05:22 | So I'm going to start by clicking on Browse.
| | 05:26 | In your Exercise File, on your
desktop, is an Assets folder.
| | 05:33 | Inside that Assets folder
is a northwnd database file.
| | 05:38 | It ends with an MDF extension.
| | 05:40 | You may or may not see this
extension, depending on how you've got your
| | 05:42 | computer configured.
| | 05:43 | I'm going to click on
northwnd.mdf and click Open.
| | 05:49 | Then I am going to click the Test
Connection to see if I have permissions to
| | 05:52 | talk to this database, and I do.
| | 05:58 | Next, I'm going to click on OK, and after
waiting a few seconds, a new connection
| | 06:06 | shows up in Server Explorer.
| | 06:09 | Now without having to leave Visual
Studio, I can open this node and look at the
| | 06:15 | structure of my database.
| | 06:16 | For instance, here are all
the tables inside the database.
| | 06:20 | Here are the stored procedures
that are inside the database.
| | 06:25 | When I have a table, I can click on
it and learn more about its structure.
| | 06:31 | This shows me the number of
fields that are part of the table.
| | 06:35 | I can right-click and choose Show
Table Data to actually talk to the database
| | 06:41 | and see the data that's stored inside that file.
| | 06:46 | On several of the movies in this title,
we'll be looking at this database.
| | 06:50 | The Server Explorer exists to let me
look at my local computer and other servers
| | 06:54 | on my network and discover information
about the computer itself and databases
| | 06:59 | that are out there available
for me to use. It's a great tool.
| | 07:01 | I suggest you check it out.
| | Collapse this transcript |
| Exploring the new Help engine| 00:00 | For better or worse, Microsoft
has created a new help engine.
| | 00:04 | The official name is Microsoft Help 3,
and it is the de facto help engine in
| | 00:09 | Visual Studio and MSDN.
| | 00:11 | The claim to fame for this latest
version is that it runs in a browser.
| | 00:16 | The goal--or so I've heard--is to make it
easier to keep the help files up to date.
| | 00:21 | During a typical install of Visual Studio, you
were offered the opportunity to install Help.
| | 00:26 | Whether you chose this option or not,
you can still configure and update Help
| | 00:31 | from inside Visual Studio.
| | 00:32 | Let me show you how that is done.
| | 00:35 | I'm inside Visual Studio, and
I have opened a project called
| | 00:38 | ExploringHelpEngine.
| | 00:40 | I'm going to click on the Help
menu and choose Manage Help Settings.
| | 00:46 | I've already run Help a number of
times on this computer, so I move right into
| | 00:50 | the Help Library Manager.
| | 00:52 | If you're running this for the first
time, you might see a dialog that looks
| | 00:57 | like this: Online Help Consent.
| | 01:01 | You also might see this dialog:
select local content location.
| | 01:06 | As I said, I have already installed
Help and used it several times, so I didn't
| | 01:10 | see either of those dialogs.
| | 01:12 | Let's return back to Visual Studio
and look at this Library Manager.
| | 01:17 | You can choose whether your
help runs locally or online.
| | 01:20 | Microsoft suggests that you run online,
so you always have the latest content.
| | 01:25 | So I'm going to click on this hyperlink,
Choose online or local help, and you
| | 01:30 | can see I already have I
want to use online help.
| | 01:34 | If I switch over and say I want to use
local help, then I have to tell it where
| | 01:38 | to put the local files.
| | 01:40 | Now if you run local help, a
special help service runs on your computer
| | 01:44 | and intercepts calls from the browser to
ensure that features like search work correctly.
| | 01:48 | I'm going to go ahead and click OK here.
| | 01:51 | Now, I need to install some
content from online to my local computer.
| | 01:56 | I'll click on this hyperlink.
| | 02:00 | The Help Library Manager will go out to
Microsoft and show me the available help libraries.
| | 02:05 | As you can see, there are
dozens of these help libraries.
| | 02:10 | I can then click on one--
let's say this IIS Developer.
| | 02:13 | The help engine tells me that
it's going to be a 23 MB download.
| | 02:17 | If I click the Update button, it will
download a copy of that file to my local
| | 02:21 | computer, install it, and
then tell me it's finished.
| | 02:26 | I am not going to do that today
because I am going to use the online help.
| | 02:30 | So I am going to click Cancel here.
| | 02:31 | Then I am going to return to choose
online or local help and go back to I want
| | 02:37 | to use online help, and then I'll choose Exit.
| | 02:43 | Let's see how the help engine works.
| | 02:44 | I'm going to open this Program.cs file
by double-clicking on it, and I'm going
| | 02:51 | to scroll down here and look at
this System.IO.FileStream class.
| | 02:56 | I'm going to click on the class and then
in Visual Studio, all you need to do is
| | 03:01 | press F1 to launch Help.
| | 03:03 | Remember, I have online help configured.
| | 03:06 | So it opens up my default browser--in this
case Firefox--and loads the MSDN help file.
| | 03:13 | You can see that this is a FileStream
class, and I can see a list of information
| | 03:18 | about the FileStream class.
| | 03:19 | I can click on this hyperlink to
read more information about it.
| | 03:24 | I can click on this C# tab
here to read the C# declaration.
| | 03:28 | Otherwise, if I was writing this
class in C#, this would be the way I
| | 03:31 | would declare my class.
| | 03:32 | I can read more about the help down here,
see some examples in my code, and then
| | 03:44 | I can see, at the bottom, the Inheritance
Hierarchy, which platform it applies to,
| | 03:51 | whether it's considered to be thread-
safe or not, and which versions of the
| | 03:55 | framework supported the FileStream class.
Virtually every single version that
| | 04:02 | has shipped has supported this class.
| | 04:04 | I can also come down here
and see the file members.
| | 04:06 | Here are the constructors of the class.
| | 04:12 | Here are the methods that are available.
| | 04:13 | Here are the properties.
| | 04:19 | Also, I might see some
events if the type supports those.
| | 04:22 | I'm going to switch back to Visual
Studio, and this time I am to click on
| | 04:26 | the Console.WriteLine.
| | 04:28 | Now there are multiple
overloads of this WriteLine function.
| | 04:31 | This WriteLine function takes a decimal value.
| | 04:35 | This WriteLine function takes a double value.
| | 04:37 | So I'm going to click on
this first one and press F1.
| | 04:40 | Now what you see is I'm showing the
Console.WriteLine method, and it shows all
| | 04:45 | the different overloads of
that method that I just showed you.
| | 04:48 | So there should be one here
for a double value. There it is.
| | 04:51 | I'm going to click on this.
| | 04:53 | That takes me to details about the
Console.WriteLine method that takes it double.
| | 04:57 | There is a new twist in the MSDN help.
| | 05:03 | If you scroll down to the bottom of
the screen, you'll see the section
| | 05:07 | called Community Content.
| | 05:09 | Microsoft is trying to make it more
like a wiki, so that you and I can change
| | 05:14 | the help and add supplemental
pieces of information to each help page.
| | 05:18 | So what I could do is if I saw something
wrong on this help page, or I wanted to
| | 05:23 | write a more useful example, I could
click on Add, log in with my Windows Live
| | 05:31 | ID, and then I could submit
my update to the help file.
| | 05:36 | There is one more interesting thing
I would like to show you about help.
| | 05:39 | If I return back to Visual Studio
and click on the Help menu, you'll see
| | 05:44 | there's a number of settings in here:
Report a Bug, Samples, Customer Feedback
| | 05:49 | Options, and many more.
| | 05:51 | The one I want to look at
here is the MSDN Forums.
| | 05:54 | If I click here, it'll take me to the
online forums, where I can ask questions
| | 05:59 | and read what other people are
doing as far as .NET is concerned.
| | 06:02 | Notice that it has loaded
it inside Visual Studio.
| | 06:06 | There is a browser window built
right into the Visual Studio engine.
| | 06:10 | Here are the different forums that
are available for me to ask questions.
| | 06:15 | So I might click on this Visual Studio
setup, and then I can see there are 7,980
| | 06:26 | different threads running inside this group.
| | 06:29 | Well, that's about all I
have to show you in help.
| | 06:32 | If you're a veteran Visual Studio
programmer, you may need some time to adjust
| | 06:35 | to this new help engine. But overall,
it's a decent implementation, and it
| | 06:39 | gets the job done.
| | Collapse this transcript |
| Setting options for the IDE| 00:00 | Visual Studio is a tool that I use almost daily.
| | 00:03 | I have strong opinions about the best way to
configure Visual Studio for the way that I work.
| | 00:08 | The Settings Options dialog is a place to tweak
the IDE so that it works the way that you want.
| | 00:14 | The first thing that I want to show
you in this movie is how to save your
| | 00:17 | settings or import existing settings.
| | 00:19 | To do that, I'm going to open Visual
Studio and go to the Tools menu, and then I
| | 00:25 | am going to scroll down to this
section here: Import and Export Settings.
| | 00:30 | So here's the idea.
| | 00:32 | When you get Visual Studio configured
the way you want, you can come into this
| | 00:35 | dialog and choose to export
your selected environment settings.
| | 00:41 | When I click on next, I can pick from a
list of settings--by default, it chooses
| | 00:48 | all the settings--and then I can click on Next.
| | 00:52 | Here's where I pick the location and
name of the file that contains my settings.
| | 00:56 | Pretty easy to do. Just
click on Finish. Success!
| | 01:03 | Later I can come back into the
Settings dialog and choose Import Selected
| | 01:06 | Environment Settings and then click on Next.
| | 01:09 | It ask if I want to say my current
settings, and I say, "No, thank you," and here
| | 01:15 | I can go out and browse for the file I
just saved, or--this is another important part--
| | 01:22 | I can choose from a set of default settings:
General Development, Visual C Sharp development.
| | 01:28 | So at any point in the future if you
want to change from being a Visual Basic
| | 01:31 | keystrokes to the C# keystroke you
just go on here and choose Visual Basic
| | 01:36 | or C# Development Settings, and
that will replace default settings for
| | 01:40 | those style of developers.
| | 01:44 | Now that you know a little bit on how
to save and import your settings, let's go
| | 01:47 | and take a look at some setting themselves.
| | 01:49 | You start by going to the Tools > Options,
down here at the bottom. And there are a
| | 01:53 | lot of settings in here. Obviously I
don't have time to cover all of them in
| | 01:58 | this movies, so I'll just look at a few of them.
| | 02:00 | I am going to start by
looking at General Settings area.
| | 02:03 | I'm going to go to Environment, and then in
here is a section called Keyboard.
| | 02:09 | This is where you can configure Visual
Studio to use the keystrokes that you want.
| | 02:14 | What I can do is find an Action.
| | 02:16 | Like here's one called File.SaveAll.
| | 02:24 | When I click on, I can see that that's
assigned to the Ctrl+Shift+S keyboard.
| | 02:29 | I can come down here and type
the new keystroke that I want.
| | 02:32 | Let's say I want to do Ctrl+Alt+S. I am
going to type in Ctrl+Alt+S, and it says
| | 02:37 | that shortcut is currently
assigned to View.ServerExplorer.
| | 02:41 | If I'm okay with replacing that, I
can click on this Assign button, and now
| | 02:44 | whenever I choose keystroke it's
going to run the File.SaveAll.
| | 02:51 | Another feature I like in
here is the Project Settings.
| | 02:56 | Let me go into Projects and Solutions
and pick General, and here are number of
| | 03:01 | settings like where do we store our
projects, the default location where your
| | 03:05 | templates are located.
| | 03:06 | All right, one here that I like to turn
on and off depending on my situation is
| | 03:12 | Track Active Item in Solution Explorer.
| | 03:15 | So what happens here is if I click on
a Code tab in Visual Studio it'll go
| | 03:19 | over to the Solution Explorer and highlight
that item, without me having to go find it.
| | 03:24 | I like to have the Always
show solution turned on.
| | 03:28 | That means that no matter whether
there I am in a single project file or a
| | 03:31 | multiple project file, it always
shows the parent node in the Tree View.
| | 03:35 | One that I like to turn off is Save new
projects when created. I am going to
| | 03:40 | select this one, and show you what that one does.
| | 03:44 | Now I can go to File > New >
Project, and when I create my project--like
| | 03:50 | this WPFapplication--
| | 03:54 | I can edit some code in
here, make some changes--
| | 04:00 | let me to make a change to Width here--
and then when I go to close the solution,
| | 04:07 | it actually hasn't saved it
in a permanent location.
| | 04:11 | Visual Studio is doing all the
changes I make in a temporary location, and
| | 04:14 | at this point it says, "Would you like to
throw out this temporary project you have created."
| | 04:18 | So if I say Discard, it
tosses out this trial run.
| | 04:21 | So it's a quick way to try out some
ideas without permanently saving the
| | 04:26 | project to my hard drive.
| | 04:27 | Go back. I'll show you one more set
of tools. Back into the Tools > Options,
| | 04:34 | and then we are going to Build and Run.
Now, what I like in this dialog is
| | 04:39 | this Before Building.
| | 04:40 | Building means to compile your application.
| | 04:42 | I will have a whole chapter on building
and the details of what goes on in that
| | 04:46 | process, but essentially I can choose
what happens if I got a file that I've
| | 04:50 | opened, and I have made some changes,
and I am ready to compile the application--
| | 04:54 | do you want it to
automatically save the changes?
| | 04:56 | That's what this section means. Or would I
like it to prompt me to save changes like this.
| | 05:04 | Now, I'll create a brand-new project.
| | 05:06 | Again, I'll choose the defaults, and then I
see this prompt me, "Would you like to save
| | 05:18 | the changes to the following items?"
| | 05:25 | There are details on all these
sections in other movies in this chapter.
| | 05:28 | Remember to use Export Setting once you get
Visual Studio configured the way you want.
| | 05:33 | That way you won't have to spend a lot
of time reconfiguring your environment if
| | 05:37 | your hard drive crashes
or you get a new computer.
| | 05:40 | That concludes our grand tour of the
Visual Studio developer environment.
| | 05:44 | As you continue in this course, you'll
discover more about certain sections of
| | 05:48 | the IDE, but the chapter goal was to
give you a good overview of the general
| | 05:52 | layout and tools available inside Visual Studio.
| | Collapse this transcript |
|
|
3. Building a Simple ApplicationCreating a simple WPF application| 00:00 | If you've taken a few minutes to look
through the table of contents of this
| | 00:03 | course, you know that I
cover a lot of different topics.
| | 00:06 | For instance, there is an entire
chapter dedicated to debugging tools and
| | 00:10 | another on deployment.
| | 00:12 | The intent of this course is not the
focus on the details, like those chapters,
| | 00:16 | but to provide an overview of how to
build an application from start to finish.
| | 00:20 | This way you get a chance to see
the basic steps to build an app.
| | 00:24 | The technology I picked for this chapter is
Windows Presentation Foundation, often called WPF.
| | 00:30 | I chose WPF for several reasons.
| | 00:33 | First, it's a great way to
build Windows applications.
| | 00:37 | By that, I mean applications that
run strictly on a Desktop.
| | 00:40 | Second, WPF contains all the latest
advances in graphics rendering as it runs
| | 00:45 | on top of DirectX framework.
| | 00:47 | Third, many of the other project types,
like Silverlight or ASP.NET, are covered
| | 00:52 | in depth in other lynda.com titles.
| | 00:54 | In this chapter, we are
going to build a RSS Reader.
| | 00:57 | I'll start by creating a new project,
I'll use the Visual Studio designer to
| | 01:01 | create a portion of my user interface,
and then I'll switch over to the XAML
| | 01:05 | markup editor to show you how
to manipulate the raw XAML text.
| | 01:09 | You'll write some code to talk to the
Bing search client and make sure that
| | 01:12 | the web calls are non-blocking by
using a worker thread and then see how to
| | 01:16 | listen for .NET events.
| | 01:18 | Then there is a movie looking at
databases, XML binding, and data sets.
| | 01:23 | Finally, you'll package up your
application and deploy it to the user's computer.
| | 01:28 | So let's get started by
building our user interface.
| | Collapse this transcript |
| Building the UI with the editors| 00:00 | To get started with my app, I'm going
to use the designer in Visual Studio to
| | 00:04 | create a basic shell.
| | 00:05 | The word designer has special
meaning inside Visual Studio.
| | 00:08 | A designer is a special window
that supports drag-and-drop and mouse
| | 00:11 | interactions for creating the user interface.
| | 00:14 | This designer is also responsible
for generating code, or XML markup.
| | 00:18 | I'm inside Visual Studio, and I have
opened the solution called InfoReader.
| | 00:23 | Inside the solution are two projects:
| | 00:25 | version 1 and version 2 of InfoReader.
| | 00:28 | I'm going to start obviously in
version 1, and I'm going to edit
| | 00:32 | this MainWindow.xaml.
| | 00:34 | If you want to know how to create your
own solution in your own project, check
| | 00:38 | out some other movies in this course.
| | 00:40 | This is the basic WPF Editing window.
| | 00:43 | This is the designer surface
on the top half of the screen.
| | 00:46 | Usually on the left edge of
the screen is the Toolbox.
| | 00:50 | If you don't see the Toolbox, you may need to
go to the View menu and turn on the Toolbox.
| | 00:55 | It also may be docked to the side over here.
| | 00:58 | Once you have the Toolbox there, you
can then pick up an element--like this
| | 01:02 | button--and drag it over
to this designer surface.
| | 01:05 | Visual Studio has this split view.
| | 01:08 | Let me shrink down the view for a minute.
| | 01:10 | I'm going to click on this Zoom to Fit
button, so I can see more of the designer surface.
| | 01:15 | And then at the bottom of the window
you can see there is a text editor, and I
| | 01:19 | can scroll this up a little bit
and then shrink this down again.
| | 01:23 | There is the designer, and here is the XAML.
| | 01:25 | So, you can choose whether you want to do
only XAML or you want to use the designer.
| | 01:30 | If you want to hide the designer, you
can click on this Collapse pane here.
| | 01:34 | I'm going to keep them both open for today.
| | 01:36 | As I size the button in the designer,
you'll see I get some sizing handles, and
| | 01:40 | when I let go off the mouse, you might
notice that some values have changed.
| | 01:45 | If I edit the XAML directly, like if I
type in 100 pixels for the Height, you'll
| | 01:49 | see that it changes in the designer.
| | 01:51 | So they're linked to each other.
| | 01:53 | Naturally, I can add other types of elements.
| | 01:55 | Let me take this TextBox here and drag
it over, and let go, and now in my XAML
| | 02:01 | it has created this element tag here, TextBox.
| | 02:03 | As I'm dragging the item around in the
designer, you'll see little lines show up.
| | 02:07 | Those are alignment cues.
| | 02:09 | So here I can see that I'm lined
up to the left edge of the button.
| | 02:13 | Now if I size the button, you'll see I
get an alignment on the right side when
| | 02:16 | they're the same size.
| | 02:17 | I'm going to make a couple of
changes on the window itself.
| | 02:21 | So I'm going to scroll up to the top of my
XAML and look at this Window class up here.
| | 02:26 | I don't want it to be this small size.
| | 02:27 | I want it to be maximized.
| | 02:29 | So, what I'm going to do is come over
here and type in "WindowState=", and
| | 02:35 | then I get a dropdown choice Maximized.
| | 02:38 | If you'd rather use a designer for this,
you can use the Property window over here.
| | 02:42 | So what you do is you select your window,
and then you go to your Property window.
| | 02:47 | On your computer, you may have the
Property window set to Categorized.
| | 02:51 | I like to switch on the alphabetical
view instead by clicking this button, and
| | 02:54 | then you can scroll down to the
bottom and find this property called
| | 02:58 | WindowState, and then you
can choose from this dropdown.
| | 03:01 | The other thing I'm going to do is I'm going
to change the title and not say MainWindow.
| | 03:05 | I'm going to call it inpho, in.pho.
readr, like so--nice modern name.
| | 03:12 | So here's what I want to do for the design.
| | 03:14 | I want to have the company
name and logo across the top.
| | 03:18 | So I need to delete this Button, hit
the Delete key, and then I'm going to
| | 03:21 | choose this TextBox and
do the same thing, delete.
| | 03:24 | What I want to have happen is I want to have a
top section with the company name and a logo.
| | 03:29 | So, I'm going to divide this into two rows.
| | 03:32 | To do that, I select my grid somewhere--
in this case I selected it down here in
| | 03:35 | the XAML. And then if you notice on the
left edge and on the top is a blue area.
| | 03:40 | If I click on this blue area,
I will add columns or rows.
| | 03:45 | So, I'm going to click over here, and
then once I've got the rows in there, I
| | 03:49 | can use this triangle to size them, or
I can come over here and click in these
| | 03:53 | items to change the sizing standard of this row.
| | 03:58 | I'm going to leave it at
the default for right now.
| | 04:00 | Inside this area, I will put a dock panel.
| | 04:02 | Dock panels are used to stick
controls to the edges of the screen.
| | 04:06 | So I will dock the text block to the left side,
and I would dock the image to the right side.
| | 04:12 | And then on the bottom half of the screen,
I'm going to put a TabControl down here.
| | 04:15 | I've already prewritten a lot of
this, and it's down here in this
| | 04:19 | InfoReader Version2.
| | 04:20 | All right, so let me close this MainWindow.
| | 04:23 | You may or may not see this Save
dialog, and then I'm going to go down to
| | 04:26 | Version2 and double-click on MainWindow.xaml.
And I need to make this my Startup Project.
| | 04:31 | When I choose to debug this application,
I have to pick which one of these two
| | 04:35 | projects to start with.
| | 04:37 | So in Visual Studio to tell it which
project I want to start, I right-click, and
| | 04:41 | I choose Set as StartUp Project.
| | 04:43 | All right, here is my newer UI.
| | 04:46 | Here's my dock panel on the top right there.
| | 04:48 | Here is the text block that says
in.pho.readr. Here is an image.
| | 04:53 | I added an image control. And then, in
order to get this picture, what I did is I
| | 04:58 | right-clicked, went to Add > Existing Item,
went out to my Exercise Files folder,
| | 05:07 | and there's an Assets folder down here.
| | 05:10 | Open the Assets folder, and then make sure
you're not looking at just C# files.
| | 05:15 | So click on this little
dropdown here and choose All Files.
| | 05:19 | And now, I can add this BlueArrow.png file.
| | 05:22 | Once I added that PNG file, then I can
click on the Image Control and go to the
| | 05:27 | Source Property here.
| | 05:29 | This is really nice because when I
click on this button with the three dots on
| | 05:32 | it to choose the image,
| | 05:33 | it shows me all of the embedded
images that are part of my application.
| | 05:37 | I picked BlueArrow.png and then clicked OK.
| | 05:40 | Then on the bottom half of the screen,
I added a TabControl--here it is--and
| | 05:46 | then I added a few tabs.
| | 05:49 | And then each tab has this
little container down at the bottom.
| | 05:51 | So like if I click here on
History, I can put some items here.
| | 05:54 | I'm going to put a ListBox in there for
later, and then later when you'll learn
| | 05:57 | about data binding we're going
to load some RSS from blog feed.
| | 06:00 | What I want to show you now, before
I end this movie, is something called
| | 06:03 | styles and templates.
| | 06:05 | The idea is--let me zoom in a
little bit so it's easier to see.
| | 06:08 | I'm going to click on this
TextBlock, and I'm going to go to its
| | 06:11 | Foreground Property.
| | 06:13 | Foreground is changing the color of
the font by applying a brush to it.
| | 06:17 | Currently, it has a black
solid color brush applied.
| | 06:20 | I'm going to click on this dropdown,
click somewhere in the green area, and then
| | 06:24 | maybe drag around here till I get the
green color I want, and then I'm going to
| | 06:27 | click outside the dialog.
| | 06:29 | Now you see I have this green text.
| | 06:31 | What I can do--let me reselect this--
what I can do now is I can go to the
| | 06:35 | Foreground Property, and I
can save this as a resource.
| | 06:39 | So I can click on this little black
diamond and say Extract Value to Resource.
| | 06:43 | That creates a GreenBrushKey.
| | 06:47 | I click OK, and what just happened
is at the top of my XAML, it created a
| | 06:52 | Resources section, and it put a
SolidColorBrush in there with an identifying Key.
| | 06:56 | Now I can go to other items, like say
this button here or this TabControl, and I
| | 07:01 | can go to its Foreground
property and click in that same area.
| | 07:06 | It doesn't have quite the same symbol
on it, as you can see. And I'll say I want
| | 07:09 | to apply a resource, and then I'll
pick the resource I just created.
| | 07:13 | So the power of this, especially if
you create styles instead of just these
| | 07:17 | stand-alone resources, is that somebody
can spend a lot of time making the look
| | 07:22 | of an element--let's say all your
buttons--and then they can put it in a style,
| | 07:26 | and then you can come in and
say, "Now, just apply that style."
| | 07:29 | As you can imagine, it would
take a long time to build that up.
| | 07:31 | So I've already written the XAML for that, too.
| | 07:34 | So, if you open up this App.xaml--
this is application wide resources--
| | 07:38 | if I open up this App.xaml file, double
-click on it, you'll see that I have a
| | 07:44 | Resources section in here.
Inside the Resources, I have two styles.
| | 07:49 | I'm clicking these little
Plus symbols on the side here.
| | 07:51 | There are two styles.
| | 07:52 | Now, a style contains property setters,
like that Foreground setter I just showed
| | 07:57 | you, and it also can contain
something called a ControlTemplate.
| | 07:59 | ControlTemplates allow me to completely
change the way a tab or a button looks.
| | 08:05 | Now right now, the style has a key.
| | 08:09 | So, if I want to apply the style,
I have to know the key name.
| | 08:12 | If I delete this key, I've now created a
style that can be applied to all TabItems.
| | 08:16 | So, I'm going to do that for the
TabItem, and I'm also going to do the same
| | 08:21 | thing for the button.
| | 08:22 | Essentially what I've done now is
I've created two styles at the global
| | 08:26 | level that apply to all TabItems in
the entire project and all the buttons
| | 08:31 | in the entire project.
| | 08:32 | Let's see what it looks like.
| | 08:33 | I'm going to press F5--or you can
also click the Debug > Start Debugging
| | 08:38 | menu, save my changes--and now you can see
that the tabs have a brand-new look on them,
| | 08:45 | and so do the buttons.
| | 08:46 | Here is the new button look.
| | 08:47 | It even has an animation as
part of the ControlTemplate.
| | 08:50 | So if I hover my mouse over the button,
you'll see that an animation shows up.
| | 08:54 | I got these styles off the
Expression Blend web site.
| | 08:57 | There's a place where you can go
and download styles from Microsoft.
| | 09:00 | So you see how changing the template
on a control can dramatically change the
| | 09:03 | way it looks and behaves?
| | 09:05 | That's just one small portion of what you
can do with the Visual Studio designer and WPF.
| | 09:10 | It's time to move on to the next topic.
| | 09:12 | Up next, writing some code.
| | Collapse this transcript |
| Working with the application code| 00:00 | It doesn't matter how good the UI
designer tools are inside Visual Studio.
| | 00:03 | They can't do everything for you.
| | 00:05 | At some point, we must write some code.
| | 00:07 | I'm going to write some code in this
project called InfoReaderV3, and I'm going
| | 00:11 | to double-click on this MainWindow.xaml file.
| | 00:13 | So let's take a minute to look around our UI.
| | 00:17 | I have a number of elements in here
from the previous movie, and I'm going
| | 00:20 | to work with these two buttons here: this
Search Bing button and this Get RSS button.
| | 00:25 | So the idea is you're going to type
some text in the Search Term textbox, and
| | 00:29 | this textbook has a name;
| | 00:31 | it's called searchText.
| | 00:33 | Names are important because in order to
talk to them in your code, they have to
| | 00:37 | have an identifying name.
| | 00:38 | The user's going to type
something in this text box.
| | 00:41 | They'll click on the Search Bing button.
| | 00:42 | Now, as you can see, this
button does not have a name,
| | 00:46 | so I need to provide it a name. And then
I'll take the results from the Bing web
| | 00:50 | site, and I'll put it done here in
this control, which is named browser;
| | 00:54 | it's a web browser control.
| | 00:55 | So before I continue, I would
like to name both these buttons.
| | 00:59 | I will click on the Search Bing button
and then go over to the Properties window
| | 01:02 | and type in a new name up here at the top.
| | 01:04 | I'm going to call this one searchAsRSSButton.
| | 01:06 | Now, by naming this, I'll also get more
readable event procedure names when I'm
| | 01:14 | ready to write my code.
| | 01:15 | So, now I'm going to go to this button.
| | 01:17 | I'll show you several ways
to add code to your project.
| | 01:20 | I'm going to double-click on the
Search Bing button, and Visual Studio will
| | 01:25 | switch me over to the Code window, and
it automatically stubs in a function with
| | 01:30 | the name of my button
and then _Click.
| | 01:33 | The other way to stub in code--I'll
switch back, I just did a Ctrl+Tab to switch
| | 01:38 | back to the other item--
| | 01:40 | I'll click on this RSS button, and
instead of double-clicking here, I can go
| | 01:44 | over to the Properties window and click
the Events tab, and then I can find the
| | 01:49 | event I want--in this case the Click
event--and double-click here, or I can type
| | 01:53 | my own name in, if I'd rather.
| | 01:55 | I think I'll just double-click on
this one to stub the code in here too.
| | 01:59 | Now I have these two buttons.
| | 02:01 | I'm going to write one more procedure,
my own custom procedure, which I'm going
| | 02:04 | to call from both these buttons.
| | 02:06 | It's going to be a private function.
| | 02:07 | It's going to return a void value.
| | 02:10 | It's going to be called SendToBing().
| | 02:15 | I need the two parentheses
to signify that it's a method.
| | 02:18 | Then I move down to the next line, and I
type the two curly braces, which signify
| | 02:22 | the section where I write
my code inside the function.
| | 02:24 | I'm going to pass in a
parameter from both of these functions.
| | 02:27 | The idea is if you click on the
searchBingButton, then I'm going to pass in a
| | 02:31 | false, because I don't want the
data returned as an RSS feed.
| | 02:34 | So I'm going to create a parameter
down here called searchAsRSS, and then up
| | 02:40 | here, I will call that function.
| | 02:43 | The nice thing about the IntelliSense
Engine in Visual Studio is that it knows
| | 02:46 | that I've just written that function,
and it shows me in my IntelliSense list.
| | 02:48 | I'm going to click the down arrow on
my keyboard and then press the Tab key
| | 02:53 | to have it finish typing,
| | 02:55 | then an open parenthesis, the word "false",
and then to end the line of code in C# you
| | 02:59 | use a semicolon.
| | 03:01 | I'm now going to copy this, right-click
and choose Copy, and then I'm going to
| | 03:07 | come down here and do a Ctrl+V to paste,
and then I need to change this to true.
| | 03:12 | Let's review what I did.
| | 03:13 | I wrote two procedures using the Visual
Studio "auto stubbing in of the code" future.
| | 03:18 | That's not the official name, but I
just double-clicked on the button, and it
| | 03:21 | stubbed in the code.
| | 03:22 | Then I wrote my own custom function
down here, SendToBing, and then I'm calling
| | 03:26 | that custom function when
I click on the two buttons.
| | 03:29 | Now, you have the basic infrastructure written.
| | 03:31 | In the next movie, I'll show you how to
test this code and also how to retrieve
| | 03:34 | information from the Bing search engine.
| | Collapse this transcript |
| Communicating with the web site| 00:00 | I'd like to show you how to connect to
web servers and retrieve information.
| | 00:04 | The site I'm going to use
for the project is bing.com.
| | 00:07 | I'll start by looking at how
Bing queries are formatted.
| | 00:10 | As you can see, I have the Bing web
site open and in the URL, I'm going to
| | 00:14 | change the query string to show you how our
queries really run against the Bing servers.
| | 00:19 | I'm going to type the word "search" and
then a question mark, and then the letter
| | 00:24 | q= and then my search
term, wpf, and press Enter.
| | 00:29 | So let's examine this search query.
| | 00:31 | The search means you want to do a search,
the q stands for query, and then the
| | 00:35 | equal sign says to assign the
WPF string to the query string.
| | 00:39 | If I change this to another word,
say "lynda", then you see I now have a
| | 00:45 | different search query.
| | 00:46 | Now, one of many interesting things
that Bing does that a lot of the other
| | 00:49 | search engines don't do is that if
you put an ampersand here, and then say
| | 00:53 | format=rss, it will return your search
results as an RSS feed, and then you can
| | 00:59 | point your RSS reader at this feed and
get a constant update for this topic.
| | 01:03 | So, I'm going to use this string when I
go talk to the Bing server in my code.
| | 01:07 | So now I need to switch over to Visual
Studio. And I'm going to come down here
| | 01:11 | and double-click on the MainWindow.xaml.cs.
| | 01:15 | This is the code from the earlier movie.
| | 01:18 | I did make one little change here. Now,
one of the things you can do in C#
| | 01:21 | when you're making a call to a function,
or a method as I like to say, is that
| | 01:25 | you can use name parameters now in C# 4.
| | 01:27 | So, this is the name of the parameter,
showAsRSS, and I can use that and then a colon.
| | 01:32 | I find that it makes my code easier to read.
| | 01:35 | My job now is to write some
code in this SendToBing function.
| | 01:39 | The first thing I'm going to do in
this function is verify whether the user
| | 01:42 | entered a search term.
| | 01:44 | You might recall that this
text box here is called searchText.
| | 01:49 | So, I'm going to write an if-then statement.
| | 01:51 | I'm doing a Ctrl+Tab now to switch back to
the other screen, and then I'm going to say if.
| | 01:56 | Now I'm going to press the Tab key
twice because "if" is a code snippet, and it
| | 02:01 | will stub in the basics of an if
statement. And then I'm going to say
| | 02:04 | string.IsEmpty, actually I mean string
.IsNullOrEmpty, and then a couple of
| | 02:10 | parentheses here and then searchText.Text.
| | 02:14 | So what this says is look at the
text box, see if the user has typed something in.
| | 02:19 | If it's null or empty, then I'm going to
show a MessageBox and say, "Please enter,"
| | 02:28 | something like that, okay?
| | 02:29 | Now let's verify that this works.
| | 02:32 | I'm going to compile my
application, save my changes--
| | 02:36 | you may not see that dialog on your
machine--and then I'm going to debug the
| | 02:39 | application by choosing Start
Debugging, which will launch my application.
| | 02:44 | Now, if I click on the Search
Bing button, I get my MessageBox.
| | 02:49 | If I click on the Get RSS
button, I see the same MessageBox.
| | 02:53 | If I type a Search Term here--wpf--and
click the button, I don't see the dialog,
| | 03:00 | but nothing else interesting happens
yet because I haven't written the code to
| | 03:02 | talk to the Bing server.
| | 03:04 | So let's do that next.
| | 03:05 | I return back to Visual Studio.
| | 03:06 | I'm going to close this Output window on
the bottom of the screen, and then the
| | 03:11 | next thing I'm going to do is do is
write a little bit of code here in the
| | 03:13 | SendToBing function to establish a
connection to Microsoft's Bing servers.
| | 03:17 | To do that, I'm going to use
a class called the WebClient.
| | 03:20 | I'll declare a variable and
instantiate an instance of this class.
| | 03:25 | Now I can format a URL and send that to
the web client, and I'll get a response
| | 03:29 | back from that server.
| | 03:31 | Next, I am going to take this code
that I have down here, and I'm going to
| | 03:36 | uncomment it by clicking on this
Uncomment button down here, and then I'm going
| | 03:42 | to cut that, Ctrl+X, and paste that in here.
| | 03:45 | So, if you say show as RSS, I'm going
to build this string using that Bing
| | 03:50 | search syntax I just showed you a few
minutes ago. And I'm going to take the
| | 03:55 | data from the text box and then
place it in the string right here.
| | 03:59 | So you see how that equals q= and
then whatever is typed in the TextBox.
| | 04:04 | Then I'll send that off to the Bing servers.
| | 04:07 | Now, I'm getting some red squiggles here
because I need a variable to hold this URL.
| | 04:12 | So I'll come up here and type in
"string url;", and then these red
| | 04:16 | squiggles should go away.
| | 04:18 | And then down here at the bottom, I'm
going to call the WebClient and say,
| | 04:22 | "I would like to download a string."
| | 04:25 | So, what I'm going to do is I'm going
to send the information over the server.
| | 04:28 | The server is going to send me back
a result, and I'm going to say, "I want
| | 04:31 | that back as a string."
| | 04:33 | And I want to do it on a second thread.
| | 04:35 | I want to do it on a
background worker thread so I don't block the UI, so
| | 04:37 | I'm going to choose this one here,
DownloadStringAsync, and one more
| | 04:50 | parenthesis right there.
| | 04:52 | So this says, "Start a second thread.
| | 04:54 | Go talk to the server."
| | 04:55 | Here, I'm doing a little magic.
| | 04:57 | I've got this plain string,
but that's not enough for this.
| | 05:00 | It needs it formatted to something called
the URI, which is a special type in .NET.
| | 05:04 | That's why I'm saying new Uri, and
then I'm passing the string, the url
| | 05:08 | into that Uri class.
| | 05:10 | I'm about ready to go.
| | 05:11 | I need to know when Bing
returns the information to me.
| | 05:14 | So, I'm going to add one more
bit of code up here at the top.
| | 05:17 | I'm going to say WebClient
.DownloadStringCompleted.
| | 05:23 | This event fires when the results
come back from the Bing servers.
| | 05:27 | So, what I'm going to do is I'm going
to press the Tab key to finish typing,
| | 05:30 | then the Plus sign, then the Equal sign,
and then Visual Studio's going to help
| | 05:34 | me write the next couple lines of code.
| | 05:36 | I'm going to press Tab once, because it
says press TAB to insert, and then it
| | 05:40 | says press TAB to generate the wc_
DownloadStringCompleted in this class.
| | 05:46 | I'll press Tab again, and it just
wrote this function for me down here.
| | 05:50 | This code will run when the download is
complete, and all I'm going to do down
| | 05:54 | here is set my browser to
NavigateToString. And I'll pass it in. The piece of
| | 06:03 | information we're going to get
returned from the web server,
| | 06:05 | it will come in this variable right here.
| | 06:08 | So I'll say e.Result.
| | 06:10 | I think I am ready to run this application.
| | 06:17 | Debug > Start Debugging.
| | 06:19 | I'll type in "wpf", and then
I'll click the Get RSS button.
| | 06:27 | I need some formatting.
| | 06:30 | I would need to do some data binding
and make sure that this gets loaded in a
| | 06:33 | list box or a treeView.
| | 06:34 | I'll show you how to do that in another demo.
| | 06:37 | Now, let me click on the Search Bing button.
| | 06:39 | I'll type in "lynda" this time. And for
some reason on my machine, I'm getting
| | 06:43 | these script errors.
| | 06:44 | I just have to click the No button
four times, and now you see my browser
| | 06:50 | running inside my application is
showing the entire HTML results coming back
| | 06:56 | from the Bing servers.
| | 06:59 | Now, we've got a working WPF application.
| | 07:01 | By using the WebClient class, we
reached out to the Bing web site and
| | 07:05 | retrieved the search results.
| | 07:06 | It would be nice to store the
history of my searches in a database.
| | 07:10 | That's the topic of the next movie,
where I show you how to work with data
| | 07:13 | and data binding.
| | Collapse this transcript |
| Connecting your data| 00:00 | A nice improvement for our application
would be to store our searches in a database.
| | 00:05 | Visual Studio contains a
number of useful data tools.
| | 00:07 | We'll look at the Data Sources window,
and the drag-and-drop binding in his movie.
| | 00:12 | I'm inside Visual Studio.
| | 00:13 | I've opened the Solution called
InfoReaderData, which contains one project
| | 00:18 | called InfoReaderV4.
| | 00:20 | I've got this MainWindow.xaml file,
which is a continuation from the other
| | 00:24 | movies in this chapter.
| | 00:26 | I'm going to take the information when
you click on either of these search buttons, and
| | 00:30 | I'm going to store it in the database.
| | 00:32 | Then I'm going to show you your
history of your searches over in this list
| | 00:36 | box on the History tab.
| | 00:38 | The first thing I need to do is add a database.
| | 00:41 | To do that, I'm going to go to my project,
right-click, and choose Add > Existing Item.
| | 00:48 | Then I'm going to navigate out to my
Desktop, find the Exercise Files folder,
| | 00:53 | and look in the Assets folder.
| | 00:56 | Be sure and change this
dropdown to show All Files.
| | 01:00 | For today, I'm going to
use a compact SQL database.
| | 01:05 | Then name of this database is History.sdf.
| | 01:08 | So I'll choose this one and click on Add.
| | 01:11 | Now Visual Studio realizes
that I've picked a data source.
| | 01:14 | It's asking me if I want to make a special
type of data access tool called a DataSet.
| | 01:18 | I'm going to go ahead and say Next on this.
| | 01:21 | Visual Studio then queries my database
and shows me a list of all of the tables.
| | 01:27 | In this example, I only have
one table with two columns.
| | 01:30 | I will check this first check box, and
| | 01:33 | then verify that I like
this DataSet name down here.
| | 01:36 | If I do, I'll click on Finish.
| | 01:39 | A number of things happened
when I click the Finish button.
| | 01:42 | First of all, it made a copy of
the History.sdf file in my project.
| | 01:47 | It also added a
connection to my Server Explorer.
| | 01:50 | If you don't have Server
Explorer opened, open it now.
| | 01:53 | You can find it in the View menu
or dock to the side of your window.
| | 01:57 | You can see I now have
this History.sdf connection.
| | 02:00 | If I expand this, I can see
the tables inside that database.
| | 02:05 | I can even right-click on this
History table and choose Show Table Data.
| | 02:09 | Currently, I have no rows in this table,
| | 02:11 | so let me put two rows in there.
| | 02:12 | I'll come over to this column,
and type in "WPF", and hit Enter.
| | 02:16 | Then I'll type in "lynda.com". Press Enter.
| | 02:21 | Then I'll close this window.
| | 02:22 | Not only did it add the database, but
it also generated this HistoryDataSet.xsd
| | 02:28 | file, and all of its companion files.
| | 02:30 | Let me double-click on the
HistoryDataSet, and show you the designer.
| | 02:35 | This is the TableAdapter that is
responsible for loading the data into my table
| | 02:39 | from the database and sending the
updated data back to the database.
| | 02:43 | So you see it has a Fill method.
| | 02:44 | I'm going to use that to get the data.
| | 02:47 | To put a new row in the DataSet,
I'm going to add a brand-new query.
| | 02:51 | I'll right-click and choose Add Query.
| | 02:54 | Then I'll click on Next.
| | 02:55 | Then I'm going to come down here and
choose Insert. It adds a new row to the table.
| | 03:00 | I'm going to click on Next.
| | 03:01 | I'm going to go ahead and accept the defaults.
| | 03:03 | This is the SQL statement
that'll be run against the database.
| | 03:07 | Just choose the defaults and click on Next.
| | 03:09 | Then I get to name my query.
| | 03:11 | Again, I'm going to choose
the defaults, InsertQuery.
| | 03:15 | Now when I'm finished, I
click on the Finish button.
| | 03:18 | You'll see at the bottom of the
TableAdapter is now InsertQuery.
| | 03:23 | Then the @p1 means that I have to pass
some sort of parameter into that query.
| | 03:29 | Now I am ready to do some data binding.
| | 03:31 | Let me switch over to my MainWindow.xaml file.
| | 03:35 | Switch to the History tab,
and select the list box.
| | 03:38 | I'm going to click on this Zoom to Fit
to make it a little bit easier to see.
| | 03:41 | There is a special window for drag-and-drop
data binding available inside Visual Studio.
| | 03:47 | It's in the Data menu.
| | 03:48 | It's called Show Data Sources.
| | 03:50 | I'm going to go ahead and click on
this stick pin, the Auto Hide button.
| | 03:54 | On my machine, it docks itself to the
bottom left-hand side of the window.
| | 03:58 | Notice that my DataSet is showing up
here, and the table that's inside my DataSet
| | 04:02 | is showing up there as well.
| | 04:04 | Now for drag-and-drop data binding, I
can pick this table up and drag it over
| | 04:10 | and drop it on my list box.
| | 04:12 | Before I do though, let me point
out my XAML that I have down here.
| | 04:16 | It's only got a few items in here.
| | 04:18 | Keep an eye on that when I drag
this over and drop it on the list box.
| | 04:23 | You see what happened is that
Visual Studio added three new lines.
| | 04:27 | It wrote a DisplayMemberPath, what to show;
| | 04:30 | it'll show this column called SearchText.
| | 04:32 | It shows that it's bound to a source.
| | 04:36 | This is the WPF and
Silverlight way to set up bindings.
| | 04:40 | This tells WTF to monitor the
underlying source and load this list box with
| | 04:45 | whatever data is inside this DataSet.
| | 04:47 | Then I also have a
SelectedValuePath for this field here, HistoryID.
| | 04:53 | That's in case the user selects an item.
| | 04:55 | I can use this as the
identifier of the row that they selected.
| | 04:58 | I'm not going to be using that in my demo.
| | 05:00 | I'll choose Debug > Start
Debugging, save my changes.
| | 05:05 | Then I'm going to click on the History tab.
| | 05:08 | There, I can see my two items from my database.
| | 05:12 | So I've successfully set up a data binding.
| | 05:14 | The next part of the demo will be when
the user types in a string here and
| | 05:18 | clicks on of these buttons,
| | 05:19 | I want to add it to the database and
make sure that this list box updates.
| | 05:24 | To do that, I'm going to
switch to the code behind.
| | 05:26 | While I'm inside my designer,
| | 05:28 | I can press F7, which
switches me over to the Code view.
| | 05:32 | Now when I did the drag-and-drop binding,
Visual Studio wrote a lot of code in
| | 05:37 | the Window_Loaded event.
| | 05:38 | It's very hard to read.
| | 05:40 | So I'm going to replace this with
a slightly more readable version.
| | 05:43 | I'm going to delete the
code here, by pressing Delete.
| | 05:46 | Then I'm going to take
this prewritten code here and
| | 05:50 | cut it and paste it right
above the Window_Loaded event.
| | 05:55 | Then I'm going to uncomment the code by
pressing Ctrl+K, Ctrl+U. This is going
| | 06:01 | to create an instance of the DataSet.
| | 06:03 | It's also going to create an instance
of that TableAdapter that knows how to
| | 06:06 | load and update and insert my data.
| | 06:08 | I'm going to do that here because I'm
going to use it elsewhere in my code.
| | 06:11 | So I'm doing it outside the
boundaries of this Loaded event.
| | 06:15 | Then I'm going to take this commented
code down here and cut it, Ctrl+X, and
| | 06:20 | paste it in the Loaded event.
| | 06:22 | Then uncomment it, Ctrl+K,
Ctrl+U. What this does--
| | 06:28 | now, I see I have an error here.
| | 06:31 | I'll click here, and then do a Ctrl+Period.
| | 06:35 | Get the smart tag, and then
say, using System.Windows.Data.
| | 06:38 | That will fix my code.
| | 06:40 | What this does is it looks in my XAML,
and finds something that was created by
| | 06:44 | Visual Studio called HistoryDataSet.
| | 06:46 | Then it creates a TableAdapter,
instantiates one, fills the data set, and
| | 06:52 | then sets up something special called a
CollectionViewSource, and then moves to the first row.
| | 06:56 | When I call the SendToBing method,
I'm going to insert a new row by calling
| | 07:03 | the ta.InsertQueryp--
| | 07:05 | that's the query we built earlier today.
| | 07:07 | We're going to pass in the string
from the text box, the search string.
| | 07:11 | Then to make sure that it updates correctly,
| | 07:14 | I'm going to call the ta.Fill, which refills
the data and then rebinds to the list box.
| | 07:21 | At this point, I think I'm
ready to run the application.
| | 07:23 | I'm going to press F5 to debug
the application. Then click Yes.
| | 07:31 | Verify that I only have
two items in the list box.
| | 07:35 | Then I'll come over here and type in
Silverlight and click the Get RSS button.
| | 07:44 | There is my RSS coming back from Bing.
| | 07:46 | When I click on the History tab, my
new item has been added to the list box.
| | 07:51 | If I were to look inside the database, I'd
also see that the data is stored in the database.
| | 07:55 | Now there is one more type of data
binding I want to talk about in this chapter.
| | 08:00 | That is called XML binding, and
it's the topic of the next movie.
| | Collapse this transcript |
| Binding to an RSS feed| 00:00 | RSS is a very popular format.
| | 00:03 | In this movie, I want to show you how
to bind your application to an RSS feed.
| | 00:07 | I'm inside Internet Explorer,
and I have my blog opened.
| | 00:10 | It's blog.wpfwonderland.com.
| | 00:13 | If I go to the end of my URL, and I
type in the word "feed" and press Enter,
| | 00:19 | I get the RSS feed view of the data,
| | 00:23 | but it's still not the raw XML
data that's coming from my server.
| | 00:26 | What I should do to get the raw feed is
right-click and then choose View Source.
| | 00:31 | I've already done that.
| | 00:32 | I've put the file inside our
project, inside Visual Studio.
| | 00:35 | So let's switch to Visual Studio.
| | 00:37 | Here is the feed file I'm
talking about, BlogExampleFeed.xml.
| | 00:41 | I'll double-click on it to
load it into Visual Studio.
| | 00:44 | Then I'm going to make my font a little bit
smaller by using the mouse wheel and the Ctrl key.
| | 00:50 | If I would like to bind part of my UI
to this data, I need to figure out how to
| | 00:55 | get down to this XML element.
| | 00:58 | You can see that the description is a child
of channel, and channel is a child of RSS.
| | 01:03 | There is a mechanism called XPath
built into XML that lets me drill into this
| | 01:07 | data using these tags.
| | 01:09 | So that's what I'm going to use.
| | 01:10 | I'm also going to use something called
an XMLDataProvider, which comes from WPF.
| | 01:14 | Let me switch to my main UI
for one minute by double-clicking
| | 01:19 | on MainWindow.xaml.
| | 01:21 | Then click on the Your Blog tab.
| | 01:24 | Then I'll click on this text
block that's right here in the UI.
| | 01:28 | Let's see what's happening here on line 82.
| | 01:31 | The text for this TextBlock is
going to come from a data source.
| | 01:35 | In WPF, the binding keyword is how we
tell the WPF engine to get the data from
| | 01:41 | this title element and put it in here
on the Text property of the TextBlock.
| | 01:44 | The curly braces are also part of
the syntax for WPF and Silverlight.
| | 01:49 | Notice that this TextBlock
is bound to the description.
| | 01:51 | Notice some dot dot stuff going on here,
because I'm going to start in my data
| | 01:57 | by looking at the individual items.
| | 01:59 | These are the individual blog posts here.
| | 02:01 | So I'm going to start my XPath by starting here.
| | 02:04 | Then I'm going to go up a
level to get to the description.
| | 02:08 | I need to write some code
to set up this data binding.
| | 02:11 | So I'm going to switch over
to my MainWindow.xaml.cs file.
| | 02:15 | Then I'm going to scroll down until I
find this section down here where it says
| | 02:19 | private void Window_Loaded.
| | 02:21 | First, I need to
instantiate the XMLDataProvider.
| | 02:26 | I'm doing it outside of the Window_ Loaded
event, so I can use it elsewhere in my code.
| | 02:30 | Then after the move current to first
bits of code, I'm going to tell that
| | 02:36 | provider where to find my blog.
| | 02:38 | So here I've typed in my feed location.
| | 02:44 | Next, I'm going to tell the provider
where to start looking inside that document,
| | 02:50 | using an XPath query.
| | 02:51 | So we start with the root element, RSS.
| | 02:54 | Then we go to channel, and then it's item.
| | 02:57 | That's where I want to start looking.
| | 02:59 | I can go up and down from
that location in my bindings.
| | 03:03 | Then lastly, I need to tell
WPF to create a DataContext.
| | 03:07 | So I'm going to go to the tab where
all my RSS controls live, called tab4.
| | 03:14 | I'm going to set the
DataContext equal to my provider.
| | 03:18 | Now what this says is any WPF element
that is inside this tab has access to the
| | 03:25 | data coming from this XMLDataProvider.
| | 03:27 | Then I'm going to press F5 to run
the application. Save my changes.
| | 03:33 | Then I'm going to click on the
Your Blog tab, and I was successful.
| | 03:37 | I'm pulling the data from my blog,
loading it into this list box.
| | 03:41 | When I click on an item in the list box,
it's loading the actual blog article in
| | 03:44 | over in this area.
| | 03:45 | I'm also pulling information about the
blog name and the blog description and
| | 03:50 | putting in these text blocks.
| | 03:51 | I showed you how that was
happening in the WPF.xaml earlier.
| | 03:55 | There is one more thing I would
like to make this application do.
| | 03:58 | I'd like to go to the search feeds, and
| | 04:00 | when you type in a new search, I'd
like to go get the RSS from the Bing site
| | 04:06 | and pass that in as the source for this page.
| | 04:09 | It's relatively easy to do.
| | 04:12 | I just need to go up to this area
where I'm downloading the string from Bing.
| | 04:16 | I'm going to say provider.Source.
| | 04:20 | I'm going to change from my
blog source to the new source.
| | 04:24 | I think that code should do it.
| | 04:28 | Debug > Start Debugging, or F5.
| | 04:31 | I will come over here and type
in my new search "Databinding",
| | 04:36 | click on the Get RSS button, and
| | 04:39 | then I'm going to on the Your Blog tab,
and see if I've pulled the data from Bing.
| | 04:45 | So these are the top ten sites from the RSS
feed that have the word "databinding" in them.
| | 04:50 | There are plenty of details I'm leaving
out about databinding in this overview.
| | 04:54 | You'll find more material about
binding elsewhere in this course.
| | 04:57 | At this point, our
application is nearly complete.
| | 05:00 | We just need to determine how to
deploy it to the end user's computer.
| | Collapse this transcript |
| Packaging and deploying the application| 00:00 | You have made it to the last step in
creating the simple RSS application.
| | 00:04 | You built and tested info reader,
and are ready to deploy it to your
| | 00:07 | customer's computer.
| | 00:08 | I'll show you two techniques
to deploy your application.
| | 00:11 | Before we ship our application, we want
to switch from Debug build to Release
| | 00:15 | build. This will ensure
that our code is optimized.
| | 00:18 | To do that, I need to be inside
Visual Studio. So, I have opened the Visual
| | 00:20 | Studio and have opened the solution
called Info Reader, which has a project
| | 00:26 | called InfoReaderVFinished.
| | 00:28 | To Release mode, I go to this
dropdown here and choose Release.
| | 00:34 | Now when I click Build, it's going to
take and compile the application and put it
| | 00:40 | in a Release-specific folder. I can
see that folder by going out to my hard
| | 00:45 | drive, right-click, choose Open Folder in
Windows Explorer, and all of my compiled
| | 00:53 | code lives in this folder here, the bin
folder, which stands for binary. And down
| | 00:58 | here, of course, is where my Release build is.
| | 01:00 | There is my exe, the most important file for
this application, and this is my database file.
| | 01:06 | So what I am going to do is I am going
to copy this Release folder and put it
| | 01:11 | somewhere my hard drive.
| | 01:12 | This is what your user would do.
| | 01:14 | They would take this folder, put it
somewhere on their computer--let's say their
| | 01:18 | desktop--and then they will double-
click on this InfoReaderFinished.exe, and
| | 01:27 | there's our application.
| | 01:29 | Let's see if works.
| | 01:29 | I will type in "RIA" and choose to
Search RSS, check the blog section to see if
| | 01:36 | it's working correctly--it looks like
it is--and look at my history. Excellent!
| | 01:40 | It's working. So, really,
it's that simple to deploy.
| | 01:44 | The user can change the name of the folder,
| | 01:46 | they can move it to a new
location. It just works.
| | 01:48 | Of course, there are some drawbacks
to this simple matter of deployment.
| | 01:52 | If you upgrade the application, you
would have to make sure that each user gets
| | 01:55 | the newer copy. Also, the simple copy
doesn't add any desktop icons or shortcuts
| | 02:01 | to the Start menu. Visual Studio
provides a normal Windows installer and a Click
| | 02:05 | once installer that address these issues.
| | 02:08 | I'd like to show you how to
create a Clickonce installer.
| | 02:10 | I am going to switch back to Visual
Studio, and then I am going to double-click
| | 02:13 | on this Properties node
in the Solution Explorer.
| | 02:16 | Next, I'm going to go down to the
Publish section, and then I'm going to click
| | 02:21 | on the Publish Wizard.
| | 02:23 | I have many more details about how to
create Clickonce applications in another
| | 02:27 | section of this title.
| | 02:29 | For now though, I am just
going to click the Publish Wizard.
| | 02:31 | This is the location on my hard drive
where the files will be published, a
| | 02:35 | folder underneath my
project. I will click Next.
| | 02:38 | I will choose to install from a CD-ROM,
and that my application will not check
| | 02:44 | for updates, and then I am finished.
| | 02:45 | I click the Finish button, wait
about two seconds, and then Visual Studio
| | 02:50 | compiles my application, publishes the
application, and then opens up Windows
| | 02:54 | Explorer and shows me the files.
| | 02:56 | Here is the file I would give my user,
and then they would double-click on the
| | 03:00 | setup.exe. And Clickonce sees that I
have not digitally signed my executable, so
| | 03:07 | it's telling the user, "I can't verify
this publisher. Are you sure you want to
| | 03:11 | install this application?"
| | 03:13 | They're going to click on Install.
| | 03:14 | Now, here's the application.
| | 03:16 | This application has a
shortcut in the Start menu.
| | 03:20 | Now before I compiled this, I should've
added our company name and our publisher
| | 03:26 | information or web site. I didn't
do that, so I got the default values.
| | 03:29 | But let's go find out where it's stored.
| | 03:31 | If I look into Start menu,
here is my Info Reader.
| | 03:34 | I can click on this link to run it.
| | 03:36 | It's also stored in All Programs.
Because I used the default, it's currently stored
| | 03:43 | in the Microsoft folder. There it is again.
| | 03:47 | Since it was a regular install, it also
is un-installable by the user if they go
| | 03:53 | to Features and click Programs and
Features, and this shows all the installed
| | 03:58 | applications, and my Info
Reader application is down here.
| | 04:03 | It shows the date it was
installed and who the publisher is.
| | 04:06 | Now the user can go and can use
Uninstall or Change, choose to remove
| | 04:11 | the application, and now all traces of
this application are gone from their computer.
| | 04:15 | That's the last topic in his chapter
on building a simple WPF application.
| | 04:19 | We created a user interface, wrote some
C# code, and determined how to use
| | 04:23 | data within the database, and
read RSS feeds from the Internet.
| | 04:27 | Just when you thought it couldn't
get any better, we had the big finale.
| | 04:30 | We created a simple
Clickonce deployment application.
| | 04:33 | Here's a tidbit for you before I go:
| | 04:35 | you will find a lot of details
about each of these phases elsewhere in
| | 04:38 | this course.
| | Collapse this transcript |
|
|
4. Exploring the Code EditorsWhat languages are supported in Visual Studio 2010?| 00:00 | Here's an amazing fact. The .NET
framework has over 140 languages that can be
| | 00:05 | used to write applications.
| | 00:07 | You heard me correctly--140 languages.
Visual Studio contains about 14 built-in editors.
| | 00:15 | A code editor is a window that lets you
write code. A good editor assists you by
| | 00:20 | including autocompletion tools,
syntax checkers and re-factoring tools.
| | 00:25 | There are editors in Visual Studio
for all the major Microsoft languages.
| | 00:28 | C# and Visual Basic are two of
the most popular .NET languages, and both
| | 00:33 | have superb editors available.
| | 00:36 | If you are a C++ programmer or are
interested in the new F# language, you
| | 00:40 | will find editors for those languages too.
| | 00:42 | Visual Studio is extensible, so third parties
can create Editors and plug them into the IDE.
| | 00:48 | Microsoft has taken that route and
created editors for their IronPython
| | 00:52 | and IronRuby languages.
| | 00:54 | On this screen is a list of all of the
default editors included in Visual Studio.
| | 00:58 | One of my favorite editors is
down near the bottom, the XAML Editor.
| | 01:04 | That's the one I use when I am
creating WPF or Silverlight applications.
| | 01:08 | I don't know about you, but I
think using a language is more exciting
| | 01:12 | than talking about it.
| | 01:13 | So let's move onto the next movie,
where I will show you how to work with
| | 01:15 | the code editors.
| | Collapse this transcript |
| Exploring basic settings for the Code Editor| 00:00 | As a programmer, you spend a sizable
portion of your workday writing text. Oh, sure.
| | 00:05 | We call it code,
but think about it for a minute--
| | 00:07 | your code is just specialized text.
| | 00:10 | And you probably write other text
documents too. For example, HTML and XML files
| | 00:15 | are common in software projects.
| | 00:17 | So it won't surprise you to learn the
Visual Studio has lots of text editors.
| | 00:20 | In this movie, I want to show you some
basic settings that you can configure
| | 00:24 | for any text editor.
| | 00:26 | I'm inside Visual Studio, and I have
opened the BasicEditor project, and then
| | 00:31 | I've open the Program.cs file.
| | 00:33 | I am going to start by showing you some
navigation aids along the left edge of the screen.
| | 00:39 | I am going to edit them
by going to Tools > Options.
| | 00:44 | You need to scroll down in Tools >
Options till you get to the Text Editor
| | 00:48 | section. Here you can see all of the
different editors that are available inside
| | 00:52 | Visual Studio. For example, down here is
the XAML editor. Up at the top is some
| | 00:58 | General settings, and
that's where I am going to start.
| | 00:59 | I am going to start by looking at
these two items here: Selection Margin
| | 01:03 | and Indicator Margin.
| | 01:05 | Selection Margin displays a
vertical margin along the left edge of the
| | 01:09 | editor's text area.
| | 01:10 | You can then click in this margin to
select an entire line of text, or click and
| | 01:14 | drag to select consecutive lines of text.
| | 01:17 | Right now, it's enabled,
| | 01:19 | so if I click on OK, I can go in my
code window, right along the edge here,
| | 01:25 | and then hold my mouse button down and
slide down the page to select multiple lines.
| | 01:30 | I can disable that, and then it will just
collapse that section down. Next to the
| | 01:33 | Selection margin is the Indicator margin.
| | 01:35 | It displays a vertical margin
outside the edge of the editor's text.
| | 01:39 | I can click here to add things like breakpoints.
| | 01:42 | This is also where we see
symbols for things like bookmarks.
| | 01:45 | Let me come down here and click right
in this area, and you can see there is a
| | 01:50 | red ball that shows up there.
| | 01:51 | That's a breakpoint, and I'm able to
turn that on and off by clicking here in
| | 01:55 | the indicator margin.
| | 01:58 | These are on by default, both these settings.
| | 02:01 | One of my favorite features in the
editors is line numbering, so let me show you
| | 02:05 | how to enable at that. That is off by default.
| | 02:07 | I am going to go back in the Tools >
Options, and then I am going to come down
| | 02:10 | here and click on All Languages.
| | 02:12 | This is going to affect
all editors in Visual Studio.
| | 02:15 | I am going to click here and
choose Line Numbers and then click OK.
| | 02:22 | And that's easy, self-explanatory, I
now have line numbers in every editor.
| | 02:27 | I find is really handy
when working in code reviews.
| | 02:30 | Another less popular feature, but one that I
like nonetheless, is the ability to do word wrap.
| | 02:36 | I am going to write a little bit of code
here in a comment. I'm going to select it
| | 02:45 | with my keyboard, do a Ctrl+C, and then
paste in another line or two here. And as you can
| | 02:52 | see, the text scrolls off
the right edge of the screen.
| | 02:56 | Now, a lot of editors that you work with--
like for instance Microsoft Word--have
| | 03:00 | the ability to wrap that line down to the
next row, and Visual Studio has that setting too.
| | 03:05 | I will go to Tools > Options > All Languages,
and then I am going to click here, Word wrap.
| | 03:15 | I can also put check mark here where
it says, "Show visual glyphs for word
| | 03:18 | wrap." What that does is it puts a little
indicator icon on the right edge of the screen.
| | 03:22 | Let me show you what I mean.
| | 03:23 | I am going to click OK. Notice now that
line 15 as a blank here and then line 16.
| | 03:31 | So this is considered to be all of line
15, even though it crosses two rows the
| | 03:36 | editor. Over here on the right it is
this little indicator glyph that says well
| | 03:40 | you have moved on to the next row.
| | 03:42 | If I make the window narrower, you see that
it adjusts the text to fit on the second row.
| | 03:47 | I'll make the window a little bit wider
for the next demo, and then I am going
| | 03:53 | to go up, and we are going to talk
about a couple of other features. Tools >
| | 03:57 | Options > All Languages.
| | 04:02 | At the top of the window is an
autolist member and a parameter information.
| | 04:07 | These are on by default, and I suggest
you leave them on. Autolist members means
| | 04:11 | that when you're typing your code, it'll
automatically give you a dropdown list of
| | 04:15 | all the coding items that
are in scope at the moment.
| | 04:18 | Parameter information is used when
you're writing a method call and the method
| | 04:22 | has multiple parameters, you can get a
tooltip that will pop up in your window, showing
| | 04:26 | you information about that particular parameter.
| | 04:29 | At the bottom of the page is
something called Navigation bar.
| | 04:32 | Navigation bar turns on this top
area in your Coding window. So if I
| | 04:38 | unselect this and go back into my
editor, you will see that those little
| | 04:42 | dropdowns have disappeared.
| | 04:44 | Those are interesting for
navigating through your code.
| | 04:46 | Let me show you how those work.
| | 04:48 | Tools > Options and select the
Navigation bar check box and then click OK.
| | 04:55 | How this works is this dropdown shows me
all of the entities like the enumeration
| | 05:01 | and the class that I have in my code.
| | 05:03 | So I can click here and go to my file
type enumeration down here, or I can go
| | 05:08 | back to my program. And then when I
am in this class, this class has two
| | 05:13 | functions: Main and Save Files. So I can
go over to this dropdown and use that to
| | 05:18 | navigate back and forth between
the Main and Save File function.
| | 05:22 | Naturally, in a real application we
have more lines of code than this.
| | 05:25 | This is a much more useful feature.
| | 05:28 | Now that we have seen the general
options available for text editors, the rest
| | 05:31 | of the movies in this chapter focus
on editors for each specific language.
| | Collapse this transcript |
| Writing a C# program| 00:00 | C# is a very popular programming
language for writing .NET applications.
| | 00:04 | Many people consider it the de facto
language for .NET, and rarely consider using
| | 00:08 | the other in-box languages.
| | 00:11 | For most of the examples in
this course, I will be using C#,
| | 00:15 | so this is a good time to explore the
basics of the C# language and its code editor.
| | 00:19 | I'm inside Visual Studio, and I've
opened this project called CSharpEditor.
| | 00:24 | I'm going to double-click on this
Program.cs file to open it up in the code
| | 00:28 | editor, and then we're going to start
writing our code here on my machine on
| | 00:32 | line 11 in this static void Main
method, which, since this is a console
| | 00:37 | application, is considered
the starting point of the app.
| | 00:40 | I just pressed the Enter key
to enter a new line of code.
| | 00:42 | You'll notice that on the left margin
there's a yellow marker over there now.
| | 00:47 | That signifies that I have made a
change since I've opened this file, but I
| | 00:51 | haven't saved the changes yet.
| | 00:58 | If I save the file by clicking on
this Save button up here on the toolbar,
| | 01:03 | you'll notice that it changes to a green bar.
| | 01:05 | That signifies that I have made some
changes since I opened the project, but I
| | 01:10 | have currently saved them to the hard drive.
| | 01:12 | So it's a status symbol.
| | 01:14 | I'm going to write some code to
read and write from the console.
| | 01:17 | I'm going to start by
writing "hello" to the console.
| | 01:20 | I'll type in the word "Console".
| | 01:23 | Notice that I get IntelliSense;
| | 01:25 | I get this dropdown window.
| | 01:27 | I have more details on IntelliSense
in another movie later in this title.
| | 01:31 | And then I'm going to press
the Tab key to finish typing.
| | 01:33 | Then I'll type dot, and
then, the word "WriteLine".
| | 01:39 | I've got enough of the
word WriteLine written now,
| | 01:43 | so I can just press the Tab key again.
| | 01:45 | Then the open paren, close
paren, and the semicolon.
| | 01:49 | In C# you always end your lines of
code with your semicolons, and you always
| | 01:54 | write your code within curly braces.
| | 01:56 | What do I want to write?
| | 01:59 | I need to put a string in here,
so I'm going to say "Hello".
| | 02:03 | If I come up and I run the application
at this moment by saying Start Debugging,
| | 02:08 | the application will start
and run, and immediately stop.
| | 02:13 | It ran so fast you might not even
have seen that flash on the screen.
| | 02:17 | I need to wait to see the
results of the application.
| | 02:20 | So, Visual Studio provides this
mechanism, Debug > Start Without Debugging.
| | 02:25 | Let's see the difference.
| | 02:27 | I'll say Start Without Debugging.
| | 02:29 | It prints my application.
| | 02:30 | My application is now terminated, but
it leaves it on the screen so that I can
| | 02:34 | see the end results.
| | 02:35 | When I'm done looking at the end
results, I can press any key on my keyboard,
| | 02:39 | and it'll then terminate that window.
| | 02:41 | If I'd rather not run using Start
Without Debugging, I can write my own line of
| | 02:44 | code to listen to the users keystrokes
by typing in "Console.ReadLine". And what
| | 02:53 | this does is it stops, waits for the
user to type something in the keyboard, and
| | 02:58 | then press Enter before it continues.
| | 03:02 | Let me modify this a bit. "Hello.
| | 03:05 | What is your name?"
| | 03:09 | Then on my ReadLine, I'm going to
declare a variable to hold the user's name.
| | 03:13 | I'm going to do that here
on line 15 on my computer.
| | 03:16 | I'm going to say "string name =", and
then I'll copy this code; Ctrl+C. Paste it
| | 03:28 | in; Ctrl+V. So, what this is
going to do it's going to say "Hello.
| | 03:31 | What is your name?" and then I'm going
to read from the Console whatever the
| | 03:36 | user types, and then I'm going to
wait for them to type something else in.
| | 03:41 | That way, the application won't end immediately.
| | 03:44 | And then down here, I am going to output
whatever the user typed in back to the console.
| | 03:50 | Now, down here, I'll say "Console.WriteLine",
and then I need my semicolon at the end.
| | 04:03 | So, what am I going to do here?
| | 04:04 | I'm going to say "Hello", and then
this is the concatenation operator, meaning
| | 04:12 | I'm going to take one string from the
user and I'm going to add it to the end
| | 04:16 | of the other string from my code.
| | 04:17 | I'm going to say "Hello "+ name.
| | 04:21 | I think that's good; let's try it out.
| | 04:23 | Debug > Start Debugging. It says, "Hello.
| | 04:27 | What is your name?"
| | 04:28 | I'm going to type my name in, "Walt
Ritscher", and then press the Enter key, and
| | 04:33 | then I see my name mirrored
back to me, "Hello, Walt Ritscher."
| | 04:36 | It's now waiting for me to press the
Enter key to terminate the application.
| | 04:42 | I showed you one way to create a variable.
| | 04:43 | There's another way to create a
variable that's very popular now in C#.
| | 04:46 | You can use the var keyword.
| | 04:48 | The var keyword says to the compiler,
"I'm going to declare a variable," demo, "and
| | 04:54 | I want you to figure out
the type of the variable."
| | 04:56 | So, if I say var demo = 6;, that's
telling the compiler to look at this number 6.
| | 05:03 | Let me hover my cursor over it.
And that is an Int32, which means it's a 32-bit
| | 05:08 | integer, and I want you to
make this variable of type Int32.
| | 05:14 | This is the same as me
typing in "Int32 demo" like that.
| | 05:22 | These are equivalent lines of code.
| | 05:24 | I'm a personal fan of using the var keyword.
| | 05:27 | I like using it a lot.
| | 05:30 | Sometimes you're going to work
with code that has long names.
| | 05:33 | Let me show you another example:
| | 05:34 | var fs = new System.IO.FileStream();.
| | 05:43 | I'm going to press the Tab
key to finish typing here.
| | 05:46 | So again, I'm asking to the compiler to
figure out the data type of this variable.
| | 05:49 | It's going to look at the right side of
the assignment operator and see that I'm
| | 05:53 | asking for it to make a new
instance of the FileStream class.
| | 05:58 | So it's going to make this
variable type FileStream.
| | 06:00 | There is a way to limit
how much typing I have to do.
| | 06:03 | I can eliminate having to type this
namespace at the beginning of the word
| | 06:07 | "FileStream" by going to the top of my code,
up here at the top of my Code window,
| | 06:11 | and typing in what's called a using
directive, "using System.IO;".
| | 06:19 | Now, down here in the body of my code,
I can eliminate the word "System.IO".
| | 06:24 | So this cleans up my code quite a bit
and makes it a little more readable while
| | 06:28 | I'm writing my application.
| | 06:30 | Now you've seen the basics of reading
and writing information to the console.
| | 06:33 | C# is a powerful language and a
perfect companion to working in .NET.
| | 06:38 | This is not a C# tutorial, however,
| | 06:40 | so if you are new to the language, you
will need to dig into the documentation.
| | 06:44 | Be sure and check lynda.com for
other movies related to this topic.
| | Collapse this transcript |
| Writing a VB program| 00:00 | Visual Basic is the other
leading language for programming .NET.
| | 00:04 | You may know it by its VB acronym or as
Visual Basic .NET, but it's officially
| | 00:09 | known within Microsoft as Visual Basic.
| | 00:12 | C# and VB share a lot of features and
are more similar than different in .NET 4.0.
| | 00:18 | According to my sources at Microsoft,
these two languages are tremendously
| | 00:21 | popular and account for the
highest percentage of .NET programmers.
| | 00:25 | In this movie, I want to show you some
basics of the VB language and its editor.
| | 00:30 | I'm inside Visual Studio, and I have
open up this project called VBEditor.
| | 00:34 | I have one code file here called module1.vb.
| | 00:37 | The vb extension signifies that this
code file contains Visual Basic code.
| | 00:42 | I'll double-click on
Module1.vb and load it in the editor.
| | 00:47 | In Visual Basic when you're working in a
console application, you have to specify
| | 00:51 | a Sub Main, a main entry
point for the application.
| | 00:55 | So you call it Sub Main, and
you put it inside a Module.
| | 00:59 | I'm going to write some code to read and
write information out to this console window.
| | 01:04 | I'll start by writing a piece of
information to the console. To do that, I
| | 01:08 | use the Console Class.
| | 01:10 | Notice that dropdown IntelliSense
I have get here inside Visual Studio is
| | 01:15 | showing me the available types I can work with.
| | 01:18 | At this point, I've typed enough for
Visual Studio to recognize the word "console",
| | 01:22 | and I can just press Tab to finish typing.
| | 01:25 | Then I'll type in period and then Wri, there I
find a recognition for Write and WriteLine.
| | 01:34 | I am pressing the up and down arrows
on my keyboard right now to get this.
| | 01:37 | Now I am going to press the enter
key while WriteLine is selected.
| | 01:40 | Visual Basic knows that this is a
method call, so it puts the open and close
| | 01:45 | parentheses automatically on that line.
| | 01:47 | This is something that VB does
that the C# editor does not do.
| | 01:51 | This would write a blank line.
| | 01:53 | I'd rather write something out of the
console so I am going to come up to this
| | 01:56 | line and type something in the parentheses.
| | 01:59 | I am going to type in, "Hello.
| | 02:03 | What is your name?"
| | 02:07 | To end a line of code in VB, you
just hit the Enter key. Nothing special is
| | 02:11 | required, like the semicolon in C#.
| | 02:13 | So I am going to move down to the next
line, and I am going to show you what
| | 02:16 | happens when I run the
application at this point.
| | 02:19 | If I'm inside Visual Studio and I
go up to Debug > Start Debugging, the
| | 02:24 | application is going to run, my code
will run, and then immediately terminate,
| | 02:28 | so don't blink while you watch this.
| | 02:30 | Did you see that window pop up?
| | 02:35 | I don't want that to happen.
| | 02:37 | I'd rather wait and see what the
results are, so there's a mechanism inside
| | 02:40 | Visual Studio to make that happen.
| | 02:43 | It's up here in the Debug >
Start Without Debugging.
| | 02:46 | Let's run the application
again using this menu item.
| | 02:50 | You see the difference?
| | 02:52 | This time the application
runs, asks me what my name is.
| | 02:56 | My application is now terminated, but
Visual Studio keeps the Console window
| | 02:59 | open so it can read the results.
| | 03:02 | Then I press any key on my
keyboard to close this window.
| | 03:05 | I'm going to press the Spacebar.
| | 03:06 | I would rather have my own code
listening to the keystrokes, so I am going to
| | 03:11 | come down to line 9 on my
computer and type in "Console.ReadLine".
| | 03:22 | That reads whatever the user types on
the console and puts it in a variable. Well,
| | 03:26 | I don't have a variable yet so let's make one.
| | 03:29 | In Visual Basic, you use the Dim
keyword to declare a variable like this, "Dim
| | 03:36 | name As string". Enter.
| | 03:41 | I am going to copy this Console.
ReadLine, paste it down here, Ctrl+V, and
| | 03:50 | then I am going to say on line
12, on my computer "name =".
| | 03:54 | So let's review what's happening.
| | 03:57 | I'm writing this string to the console
and declaring a variable, I am asking the
| | 04:03 | user for their name, and I am
storing it in this variable.
| | 04:07 | Then I'm going to write again
to the console. Choose copy.
| | 04:13 | I'll come down here under line 13.
| | 04:16 | I am going to paste it in, Ctrl+V,
and then I am going to concatenate this
| | 04:25 | variable on the end.
| | 04:26 | Concatenation just means taking two
strings and putting the second string on the
| | 04:31 | end of the first string.
| | 04:32 | So I am going to say Hello and then
ampersand, which is the concatenation
| | 04:37 | operator in Visual Basic, name, and then
I'll wait for the user to close the application.
| | 04:45 | Let's try this one. Debug > Start Debugging.
| | 04:50 | I type in my name, "Walt Ritscher", hit
the enter key. It mirrors that back to me
| | 05:00 | Hello Walt Ritscher, and then it
waits for me to press the Enter key to
| | 05:03 | terminate the application.
| | 05:06 | Next, I am going to show you how to
declare a different type of variable.
| | 05:09 | I am going to say Dim fs
As New System.IO.FileStream.
| | 05:20 | I am going to press the down arrow to move
to FileStream and then press the Enter key.
| | 05:25 | What this is saying is, "Declare a
variable and instantiate an instance of this
| | 05:31 | class, the FileStream class and
store the instance in this variable."
| | 05:34 | Now to simplify my life, I can eliminate
typing in System.IO, so I'm going to delete this.
| | 05:42 | Notice I get a blue squiggle at this moment
because it doesn't recognize the class name.
| | 05:48 | I will then scroll up to the top of
my Code module and type in an import
| | 05:54 | statement up here, "Imports System.IO".
Make sure you spell it correctly.
| | 06:02 | Now look at line 18 in my code. That blue
squiggle that was under FileStream has disappeared.
| | 06:08 | That signifies that it
recognized that as a valid class.
| | 06:12 | Putting this Import at the top of my code
simplifies the body of my code document.
| | 06:16 | So what I have showed you so far
today is how to read and write from a
| | 06:21 | console in Visual Basic.
| | 06:23 | Obviously, teaching VB
would take a course of its own.
| | 06:25 | You will see more examples
of it later in this course.
| | Collapse this transcript |
| Working with C++| 00:00 | C# and Visual Basic are the most
popular languages in .NET, but Visual Studio
| | 00:05 | caters to other language enthusiasts too.
| | 00:08 | Today I want to look at the support for C++.
| | 00:11 | I'm inside Visual Studio, and
I've opened a solution called CPlus.
| | 00:15 | Inside that solution are two projects:
| | 00:18 | ExploringCPlusClr and ExploringCPlusWin32.
| | 00:24 | There are a variety of
different kind of C++ project types.
| | 00:27 | Let's go take a look.
| | 00:29 | File > New, and then I'm going to choose
the Project menu. And then I'm going to
| | 00:34 | scroll down to the C++ section.
| | 00:38 | You can see that there is a CLR
Console Application and a Win32 Project.
| | 00:46 | If you are familiar with ATL or MFC,
there are those project types in here as well.
| | 00:52 | I've already added the projects to my solution,
so I'm going to click Cancel at this point.
| | 00:56 | I'm going to start by looking at
the ExploringCPlusWin32 project.
| | 01:03 | This is an unmanaged C++ project.
| | 01:06 | It's not my startup project, however.
| | 01:09 | I can tell that because it's not in bold.
| | 01:11 | So I'm going to right-click and
choose Set as StartUp Project.
| | 01:16 | That way when I run the application,
I'll be sure that my code runs.
| | 01:22 | Next, I'm going to open this node and
find the Source Files folder and then open
| | 01:27 | the ExploringCPlusWin32.cpp file.
| | 01:32 | The .cpp extension means that
this file contains C++ code.
| | 01:36 | I will double-click on the
file and then show you the code.
| | 01:40 | I need a starting point for my
application for this console application.
| | 01:45 | That's called _tmain.
| | 01:48 | Within that function, I'm going to
write out to the console, and I'm going to
| | 01:53 | read some information in from the console.
| | 01:55 | To do that, I'm going to use
the cout and the cin keywords.
| | 02:02 | cout does what it implies; it's console out.
| | 02:06 | And I'm using this insertion character
here to take this string and insert it
| | 02:11 | into the output stream.
| | 02:13 | I'm also going to end the line
with the special "endl" keyword.
| | 02:18 | An alternative way of doing this
would be to concatenate like this.
| | 02:22 | This is a longer sentence and then by
using the \n character within the string,
| | 02:28 | that's also the equivalent of new
line, so I don't have to use endl.
| | 02:33 | Line 18 tells the application to
wait for user input, to pause until they
| | 02:39 | press the Enter key.
| | 02:41 | Next, I'll declare a
variable. That's easy to do.
| | 02:44 | The data type of the variable is int
and then the name of my variable is age.
| | 02:49 | All lines of code in C++ end with semicolons.
| | 02:53 | Then I'm going to output this string,
and then on line 22, this is going to
| | 02:58 | take whatever the user types and
push it into this variable, using the
| | 03:02 | insertion characters.
| | 03:06 | Next, I'm going to declare a
variable of type string called fullName.
| | 03:10 | And there are a couple of
ways I can get the information.
| | 03:13 | One is to use this fflush function and
pass it these standard input buffer, or
| | 03:20 | if I'd rather, I can use this
function down here called getline.
| | 03:25 | Getline takes two arguments:
| | 03:26 | it takes this input stream from the
console, and it takes a variable that I
| | 03:30 | want to store the data in. And then
getline calls cin and pushes the data into
| | 03:35 | this fullName variable.
| | 03:39 | Lastly, I'll concatenate
them all into a single output.
| | 03:43 | Would you like to see it running? Good!
| | 03:46 | I'll go up to Debug > Start Debugging.
| | 03:49 | Visual Studio realizes that I haven't
built this application recently, so it
| | 03:54 | asks me if I would like to build it.
| | 03:55 | I'm going to say Yes.
| | 03:59 | As you can see, there are my
two strings output to the console.
| | 04:02 | It's waiting for me to press a key.
| | 04:05 | Next, it prompts me for my age, and
then my full name, and finally it outputs
| | 04:14 | that to the console and then waits
for me to kill the application by
| | 04:16 | pressing the Enter key.
| | 04:19 | Next, I would like to show you the
managed version of this, the CLR version.
| | 04:25 | It's much easier to
program, and it's more like C#.
| | 04:28 | Because it uses the .NET libraries, it's
easy to use the thousands of .NET types
| | 04:32 | that exist in the .NET assemblies.
| | 04:34 | So I'm going to show you that code.
| | 04:37 | I'm going to right-click on this project and
make it my startup, Set as StartUp Project.
| | 04:44 | And then I'm going to expand this node,
find the Source Files folder, and then
| | 04:49 | double-click on this cpp file
right here, the ExploringCPlusClr file.
| | 04:55 | Like the other application,
I need a starting function.
| | 04:58 | Here, it's called main.
| | 05:01 | And then you'll notice that I'm
going to be working with .NET classes.
| | 05:06 | The .NET class I want to work with is
Console, and the method, or function, that I
| | 05:11 | want to call is called WriteLine.
| | 05:13 | If you're already familiar with C# or
Visual Basic, this should look vaguely familiar.
| | 05:17 | The main difference is you use the colon,
colon between the class name and the member name.
| | 05:23 | I'm also using a prefix here in the
string, the L prefix on the string.
| | 05:28 | So what am I doing in this code?
| | 05:30 | I'm writing a prompt the user on line
11, and then on line 12, I'm declaring a
| | 05:36 | variable named fullName, and the
datatype of that is the string.
| | 05:41 | Then I'm prompting the user, and then
I'm reading from the console and pushing
| | 05:45 | the data into this fullName variable.
| | 05:48 | Once I'm done with that, I will
write once again to the console, and I'm
| | 05:51 | going to do a concatenation operation,
putting the two strings together, and
| | 05:55 | then a blank ReadLine.
| | 05:57 | Would you like to see that
running? Okay, here we go.
| | 06:00 | Debug > Start Debugging.
| | 06:03 | Once again, Visual Studio prompts me to
build the application. I'll choose Yes.
| | 06:10 | And there's my prompt and your full name.
| | 06:15 | And there's my
concatenation to the output screen.
| | 06:17 | I'll press Enter one more
time to kill the application.
| | 06:21 | As you can see, there's still a
place for C++ in Visual Studio.
| | 06:25 | Granted, languages like C# are easier to
use and generally more popular with the
| | 06:30 | current generation of developers, but
if C++ is your language of choice, you
| | 06:34 | should be happy to see it
living proudly in Visual Studio.
| | Collapse this transcript |
| Working with F Sharp| 00:00 | F# is a new language that
ships with Visual Studio 2010.
| | 00:04 | It is based on the functional OCaml language.
| | 00:08 | F# is largely a functional
language, though it has traces of object
| | 00:11 | orientation and imperative languages too.
| | 00:14 | Writing safe concurrent programs has
become a concern recently, as multi-core
| | 00:18 | CPUs have become widespread.
| | 00:20 | Functional language helps support
concurrency by encouraging the use of
| | 00:23 | immutable data structures that can
be passed between threads and machines
| | 00:27 | without worrying about
thread safety or concurrency.
| | 00:30 | I'm going to show you a brief demo of
the FSharpEditor here inside Visual Studio.
| | 00:33 | I have already opened a
project that called FSharpEditor.
| | 00:36 | I'm going to double-click
on this Program.fs file.
| | 00:42 | You might guess that FS stands
for FSharp. That means that this file
| | 00:46 | contains F# code.
| | 00:47 | So I'll double-click, and this is the editor.
| | 00:51 | I'm going to use the printfn function
to print information out to the console.
| | 00:57 | I'm also going to use this #light
compiler flag, which makes it a little bit
| | 01:04 | easier to write the F# code.
| | 01:07 | This means I don't have to end the
line of code with the colon, colon syntax,
| | 01:12 | which is the normal way of doing that.
| | 01:13 | On the other hand, it means that
indentation takes more significance.
| | 01:18 | The way you indent your code is more
important when used the #light switch.
| | 01:22 | So I'm going to use the open keyword
to resolve namespaces, so that I can do
| | 01:29 | things like a System.Console.WriteLine.
| | 01:32 | This means I could leave off
this system prefix if necessary.
| | 01:35 | It's kind of like the import or
using statements in Visual Basic and C#.
| | 01:38 | I'm going to print out of the
console the word Hello, or I can use the
| | 01:44 | System.Console.WriteLine mechanism
for doing that. That's familiar.
| | 01:48 | Again, if you're comfortable with .NET
classes, this code should look familiar to you.
| | 01:52 | Next, I am going to declare a variable.
| | 01:54 | So the let keyword allows me to that in F#.
| | 01:58 | There are some differences.
| | 01:59 | Variables are immutable in F#,
which means that once you declare the
| | 02:03 | variable and you load the initial data
into it, you can't change it anymore.
| | 02:07 | But that also makes it the thread-safe.
| | 02:10 | So on line 21 what I am doing is
I'm declaring a variable called age.
| | 02:14 | I'm doing a mathematical addition there: 34 + 2.
| | 02:18 | Notice that I don't have to put
anything special on the end of the line here
| | 02:21 | because of that #light
compiler flag I have up there.
| | 02:25 | Then once this variable
is declared, I can use it.
| | 02:28 | Now if I try to do something like
age = 80, I'll get a blue squiggle.
| | 02:33 | I'm getting that kind of useless
message here that says the expression should
| | 02:37 | have a type of 'unit',
but it has a type of 'bool.'
| | 02:40 | Basically, it's saying you
can't assign another value to this.
| | 02:43 | If I try to use the let keyword,
I'll get a different squiggle.
| | 02:48 | This time it's telling me I have already
declared a variable called age, and you
| | 02:52 | need to come up with a new name.
| | 02:54 | So let me comment this out, and we'll continue.
| | 02:57 | So next, I write out, "Enter your
full name." And then I'm going to use the
| | 03:02 | Console.ReadLine to read the
contents and store them in this variable.
| | 03:07 | Then I'm going to print them out.
| | 03:09 | Then I'm going to use this %s as a token,
and this %d as a token to say, take the
| | 03:14 | full name here and place it
within the string where this token is.
| | 03:19 | Then also do the same thing
with age and place it here.
| | 03:23 | All right, does that make sense?
| | 03:25 | Let me show you one another feature
that a lot of people really like about
| | 03:30 | F# is that functions
are first-class citizens.
| | 03:34 | You can declare them in line and
then pass them around as arguments to
| | 03:37 | elsewhere in your code.
| | 03:38 | For instance, you can create a
function and then pass that in as an argument
| | 03:42 | to another function.
| | 03:43 | So what I'm doing on line 37
is I am declaring a function
| | 03:47 | called calculateSalesTax.
| | 03:49 | And then I'm saying that this
function expects to have two incoming
| | 03:53 | parameters: sale and tax.
| | 03:57 | Then on the right side of the Equal sign, I'm
writing the code that's part of that function.
| | 04:02 | I'm saying, multiply sales by tax.
| | 04:06 | Now, I can come down here and run that
function that I just declared, by using
| | 04:10 | its name and then
passing these two arguments in.
| | 04:13 | Notice that I'm not
separating those with commas;
| | 04:15 | I'm just passing them in.
| | 04:16 | That's a common metaphor in
F#--not using commas here.
| | 04:19 | There is an alternative syntax
down here on line 42, where I say let
| | 04:24 | calculateTotalSale, and then I can use what's
called a tuple in order to set off items.
| | 04:30 | By using the parentheses here, I'm
saying I have an argument that called sale,
| | 04:36 | an argument called commission, and
| | 04:38 | then again, just like before, I write
the code for the function on the other
| | 04:41 | side of Equal sign.
| | 04:42 | Then I call it the same as before,
but now because I declared it like this,
| | 04:45 | I can pass the arguments in
with a comma separating them.
| | 04:48 | Want to see this run? I do.
| | 04:51 | I'm also going to put a break
point right here by pressing F9.
| | 04:56 | I'll go up to the Debug menu and
choose Start Debugging. Go ahead and say Yes
| | 05:02 | to save these changes.
| | 05:04 | You can see I have put a
few items to the console.
| | 05:08 | I'll enter my name.
| | 05:09 | I do the string concatenation, and then I
press Enter, and you can see I just hit my
| | 05:18 | break point back here.
| | 05:19 | I have a section on working with
breakpoints and debugging, but right now, I'm
| | 05:23 | just going to step through my
code by clicking on this Step button.
| | 05:29 | Notice what happened.
| | 05:30 | I declared this function up on line 37.
| | 05:32 | I'm running line 39, but you can see it
just stepped back, and it's showing me
| | 05:36 | I'm running this code up here: sale *
tax, which should be a value of four, if I
| | 05:41 | hover over that, and this
one has a value of five.
| | 05:45 | Then I'm going to press just
Step Into a couple more times.
| | 05:49 | And then I'll hover over the total, and
you'll see that that has a value of 20.
| | 05:53 | So you see that these in-line
functions work exactly the same as if they
| | 05:56 | were regular functions.
| | 05:59 | Learning a new language is a
great way to appreciate your current
| | 06:02 | programming language.
| | 06:03 | I think F# is a great
candidate to look at for your next language.
| | 06:07 | What do you think about giving it a go?
| | Collapse this transcript |
| Font and color options| 00:00 | What developer doesn't want
absolute control over their coding editor?
| | 00:04 | Perhaps you prefer a dark background
and neon blue text for your code.
| | 00:08 | When you configure your own fonts
and color options, it's one way to
| | 00:11 | indulge your inner artist.
| | 00:12 | Let me start by showing you how to
increase the font size in your code.
| | 00:17 | I'm inside a project in Visual
Studio called FontAndColor.
| | 00:21 | I'm going to open these two code files
down here: Book.cs--double-click on that
| | 00:26 | one--and I'm going to open Program.cs.
| | 00:28 | I'm going to move over to Book.cs, and then
I'm going to use a feature of the mouse
| | 00:36 | scroll wheel in Visual
Studio to enlarge this font size.
| | 00:38 | Before I do that though, let me close this
error list on here in the bottom of the
| | 00:41 | screen, and this output window. There you go.
| | 00:44 | I'm going to hold down the Control key,
and then I'm going to use my mouse
| | 00:50 | wheel, and I'm going to zoom
in to make the text larger.
| | 00:55 | Obviously, you would do this if
you're having trouble reading the code.
| | 00:58 | If I switch over to Program.cs, and
notice that it only affects the one file.
| | 01:04 | The font size here is the
same size as it was originally.
| | 01:06 | If I switch back to Book.cs,
you'll see that it is the large size.
| | 01:11 | Now if I scroll the opposite
direction, what you think will happen?
| | 01:14 | Ah, you figured it out; the text gets smaller.
| | 01:18 | If you want more control over the
individual words inside your document, then
| | 01:22 | you need to go to the Tools > Options menu.
| | 01:24 | Let's go there. Tools > Options.
| | 01:28 | And then you're going to click on the
Environment node, and go to the Fonts
| | 01:32 | and Colors section.
| | 01:33 | The first thing I will look
at is the basic font, Consolas.
| | 01:37 | This affects the entire code window;
| | 01:39 | any text that is in the code
window will use the Consolas font.
| | 01:42 | If you want to change this
font, click on this dropdown.
| | 01:44 | Since programmers tend to like to use
monospaced fonts, this dialog shows you
| | 01:50 | all the fonts in your system, but it
points out the ones that are monospaced by
| | 01:54 | putting them in bold.
| | 01:56 | Personally, I like the Consolas font,
so I'm going to leave it at the default.
| | 02:01 | Next, I'm going to start
changing some of the items in my code.
| | 02:05 | For instance, maybe I
want to change my comments.
| | 02:09 | I'll press C, which moves me down to
the C section. Click on Comment, and
| | 02:14 | you can see that the normal comment
is a white background with a nice dark
| | 02:18 | green colored font.
| | 02:21 | I'm going to change my background to a
slightly yellow color. I'll click here.
| | 02:25 | And now these colors make me happy,
so what I'm going to do is I'm going to
| | 02:29 | click on the custom button
and choose a light yellow color.
| | 02:35 | I am thinking that looks
pretty good right there.
| | 02:38 | I'll click OK, and then I'll click OK again.
| | 02:42 | And you can see that all my comments now
have green text with a light yellow background.
| | 02:48 | I like that.
| | 02:49 | Next, I want to talk about these curly
braces and these parentheses. Those are
| | 02:53 | called matching braces, so
I can color those as well.
| | 02:58 | Go up to Tools > Options, find the
Brace Matching, and I can choose the
| | 03:05 | brace matching rectangle.
| | 03:07 | And right now this kind of this
light gray color. That's boring.
| | 03:13 | I'm thinking the some sort
of--I had yellow earlier.
| | 03:19 | I'm thinking maybe some sort of green now
for this. How about that color right there?
| | 03:23 | Looks good to me. Click OK and then click OK.
| | 03:27 | Now watch what happens if I move
my cursor next to the parenthesis.
| | 03:32 | The matching parenthesis is
highlighted in this green color.
| | 03:35 | If I move down here, the same thing
happens. And if I move to this curly brace,
| | 03:42 | it shows the matching curly
brace further down the page.
| | 03:45 | It shows me that this one on line 28
is the match for this one on line 22.
| | 03:49 | Very handy for seeing where your
beginning and ending sections of your code are.
| | 03:55 | There are a lot of other changes you can make.
| | 03:56 | I can change the colors of
the line numbers down the side.
| | 04:00 | I can change the colors of the string;
for instance, over here, the color of the
| | 04:05 | items that are between beginning and
ending double quotes can be changed.
| | 04:09 | This list is endless, as you can see.
| | 04:15 | So let me show you just one other one though
that I like to use a lot, and that's to
| | 04:19 | differentiate the difference
between a value type and a reference type.
| | 04:23 | So I'm going to scroll down to the User
Type section, right here, and I'm going
| | 04:28 | to pick user types, which would be a class.
| | 04:32 | And I'm going to change the background
of this to light blue. I'll pick Custom.
| | 04:35 | I'll pick a nice light
blue color and then click OK.
| | 04:41 | And then for the value types--things
like enumerations or structures would be a
| | 04:46 | better example, because there
are separate ones for Enums here--
| | 04:49 | for a structure type, I'll pick a
light pink background, like this one.
| | 04:56 | Click OK and then click OK.
| | 04:58 | And now let's look what happens in my code.
| | 05:00 | Everywhere where I declare an insistence
of a class, it uses the blue background.
| | 05:05 | Everywhere where I declare a
structure type, it uses the pink background.
| | 05:09 | When I write code to instantiate
those types, it does the same colors.
| | 05:13 | Here, I'm saying a new book.
| | 05:14 | Here, I'm saying new file information.
| | 05:17 | So I find this very handy to keep my
mind clear which is a reference type and
| | 05:22 | which is a value type.
| | 05:24 | Couple of more settings.
| | 05:25 | You can go to the Tools, Options and
by clicking on this dropdown up here,
| | 05:30 | you can change all their parts of Visual Studio:
| | 05:33 | the toolbars, the different
windows that are docked to the side,
| | 05:36 | you can change the fonts that
are used for printing output.
| | 05:39 | Let me show you just one another
one in here, the Statement Completion.
| | 05:42 | This affects the dropdown
windows for the IntelliSense.
| | 05:46 | So if I go right now and choose to
change this to a 16-point font, like so, and then
| | 05:52 | click OK, and go to my code and type
in "fi", the dropdown and the tooltip that
| | 06:02 | are popping up at this
moment are now using my 16-point
| | 06:04 | font, whereas before they were using the 9-point font.
| | 06:07 | I am just about done.
| | 06:09 | I would like to reset my environment
so that for future movies it uses the
| | 06:13 | default settings, so I'm
going to go back in to Tools.
| | 06:17 | Of course, you don't have
to do this on your computer;
| | 06:19 | you can keep your own settings.
| | 06:21 | But for these movies, I'm going
to set it back to the defaults.
| | 06:23 | I'll click on Options, and then I am
going to use Use Defaults, and then I'll go
| | 06:29 | to the Text Editor and click on Use Defaults.
| | 06:34 | And now my beautiful color comments
and all the other configuration changes I
| | 06:38 | made have disappeared. It's a sad day.
| | 06:41 | Now, you can spend an hour
experimenting all the font settings in this dialog,
| | 06:45 | and before long, you'll have
your own custom code editor
| | 06:47 | that will be the envy of your peers.
| | Collapse this transcript |
|
|
5. Working with CodeFormatting your code| 00:00 | Visual Studio assists you in writing
readable code by automatically indenting
| | 00:04 | and aligning your text.
| | 00:06 | Many people use the default settings
without a second thought, but with a little
| | 00:09 | effort you can configure the formatting
so that you have better control over
| | 00:12 | how your code is arranged. Let me show you.
| | 00:15 | I am inside Visual Studio,
and I have opened a project
| | 00:18 | called FormattingYourCode.
| | 00:20 | This has two C# files: Book.cs--
I'll double-click to open that--and
| | 00:25 | Program.cs. And it also has a WPF
user control, SampleWpfControl, which
| | 00:31 | contains some XML markup down here.
| | 00:34 | I'm going to open the Book.cs
file, and I'm going to show you the
| | 00:39 | SmartIndent feature.
| | 00:41 | Now, SmartIndenting means what happens
when you hit the Enter key on one line and
| | 00:45 | move to the next line.
| | 00:46 | So to configure this setting,
| | 00:48 | I'm going to go to Tools > Options, and
I'm going to open the Text Editor node,
| | 00:54 | and then I'm going to
click on the All Languages.
| | 00:58 | This way I can make the changes,
and it will affect every code editor.
| | 01:01 | I'm going to click on Tabs, and I'll
start by choosing None for the Indenting.
| | 01:06 | Now, when I indent my code, like here,
notice that when I move to a new line,
| | 01:11 | the cursor moves to the beginning of the line.
| | 01:13 | Now if I want to get over here to align
it with these other comments, I have to
| | 01:17 | either press the Spacebar or the
Tab key to get over there, like so.
| | 01:25 | If I used a SmartIndenting, it
will automatically handle all that.
| | 01:30 | I'll use what's called a
context-sensitive indent.
| | 01:33 | It looks at where it belongs--
| | 01:35 | is it in other curly braces, is it
inside an 'if-then' statement?--and
| | 01:38 | determines where to put it.
| | 01:40 | So watch what happens now.
| | 01:41 | When I hit the Enter key, it stays aligned.
| | 01:45 | Now, the tabs are probably
off by a couple of spaces.
[0:01:49.17]
"This is a demo", like so. Now watch
what happens if I write an 'if' statement.
| | 01:52 | 'if' now I pressed 'if' and tab
tab twice to stub in some code for me.
| | 01:57 | Now I'm going to come down
here and hit the Enter key.
| | 02:00 | That's what I mean by context-sensitive.
| | 02:02 | It knows I'm inside these curly braces,
so it indents it at a further
| | 02:06 | level than it did above.
| | 02:08 | Now, my comment goes here, and more.
| | 02:12 | If I move outside the curly braces
and hit Enter, it indents it at a
| | 02:16 | slightly different level.
| | 02:17 | This is the default setting, and I find it
invaluable to have for arranging your code.
| | 02:22 | You probably have a personal
preference on whether you like tabs or spaces.
| | 02:25 | There is a lot discussion
out there on which is better.
| | 02:28 | I won't argue which is better, but you
can choose in Visual Studio which one you
| | 02:31 | want to use by going to Tools > Options
and then coming here and say, when I do
| | 02:36 | a tab, I want you to Insert spaces.
| | 02:38 | In other words, if I press Tab and I
say that the Tab size is equal to 2, and
| | 02:42 | Indent size is equal to 2, when I
press the Tab key, if the Insert spaces is
| | 02:47 | turned on, then it will put two spaces in there.
| | 02:49 | If I have the Keep tabs selected, then it will
put a tab that happens to be two spaces wide.
| | 02:55 | So I'll come down here, and I'll press
the Tab key twice. And you see that it
| | 02:58 | moves over apparently four spaces.
| | 03:01 | If I go up and I choose Edit >
Advanced > View White Space, you can see these
| | 03:07 | little dots here. Those represent spaces.
| | 03:12 | I'll go back up and change my settings
to Keep tabs, and then I'll move down a line
| | 03:18 | and press the Tab key. And now you
see instead of spaces, I get the little
| | 03:22 | arrows. That signifies that
I'm using tabs, not spaces.
| | 03:25 | Again, it's your choice. Most often
you'll have View White Space turned off.
| | 03:29 | My personal preference is to use a two-
space tab instead of four-space tab,
| | 03:34 | because I can get more on the screen that way.
| | 03:40 | I'll turn off View White Space now.
| | 03:41 | Next, I want to show you what happens
when you're working in an editor like C#
| | 03:46 | and you want to
automatically format your document.
| | 03:49 | There are some settings and tools
options specific to different editors.
| | 03:55 | For example, if I go to the C# editor
and go to Formatting, I can set up rules
| | 04:01 | inside here of how my C# code is
indented. The same with the XAML,
| | 04:06 | I can go down to the XAML section
and I specify XAML specific settings.
| | 04:12 | All right, I'll start with the C# settings.
| | 04:13 | I'll go up here, choose C#, and
I'm going to talk about New Lines.
| | 04:19 | What this says is, where do you
want to put the new line for a brace?
| | 04:23 | Do you want the brace be
on a new line for types?
| | 04:26 | You get a preview in
Visual Studio. Here is a class.
| | 04:29 | If I place the open
brace on a new line for type,
| | 04:31 | you'll see that this brace
here is on the second line.
| | 04:34 | If I uncheck this, you'll see that it
moves up, and the brace stays on the same
| | 04:38 | line as the class declaration.
| | 04:40 | Same thing with methods here.
| | 04:41 | I'll click on moving the Method opening
curly brace to this top line, instead of
| | 04:45 | having it on a separate line like this.
| | 04:47 | All right, now I'm going to go
ahead and unselect these two.
| | 04:49 | I'm going to return back to my code.
Notice that my class up here has the
| | 04:54 | curly brace where it was.
| | 04:56 | It doesn't automatically update your
code once you make the changes. But if I
| | 04:59 | were to write a new function right now,
I can force a formatting anytime by
| | 05:06 | going to Edit > Advanced > Format.
| | 05:12 | I got two choices here: Format
Selection and Format Document.
| | 05:15 | I use this shortcut all the time, Ctrl+K,
Ctrl+D. Let me show you how that works.
| | 05:21 | I just ran that on this document, and
now it knows all the methods you are
| | 05:24 | supposed to have their opening curly
brace on the same line as the function name.
| | 05:28 | I can force that again at anytime.
| | 05:30 | I'm going to go over here to my XAML.
| | 05:32 | I'm going to go over here.
| | 05:34 | I am going to do a Ctrl+K, Ctrl+ D. Now,
I want you to keep an eye on this line
| | 05:38 | 10 here, Ctrl+K, Ctrl+ D, Ctrl+
K, Ctrl+D. Nothing happened.
| | 05:43 | Maybe I need to go change the setting.
Tools > Options. Scroll down to the XAML
| | 05:51 | section, pick Formatting, and
then I'm going to go to Spacing.
| | 05:56 | Right now, the setting is
Preserve New lines and Spaces.
| | 05:59 | In other words, don't touch my XAML.
| | 06:01 | I'm going to change that to say
Position each attribute on a separate line.
| | 06:04 | I'm going to tell to collapse
multiple lines down into a single blank line.
| | 06:09 | I'll put a few extra spaces in here like
that, and now I'm going to do my secret
| | 06:13 | keystroke: Ctrl+K, Ctrl+D,
and keep an eye on line 13.
| | 06:17 | It will autoarrange my code.
| | 06:20 | It put each attribute on its own separate
line, and those multiple spaces that I had,
| | 06:25 | it collapsed them down to a single blank space.
| | 06:27 | Again, try that here. Just several
blank lines there, Ctrl+K, Ctrl+D
| | 06:31 | removes those blank lines.
| | 06:33 | I can't tell you now many times a day I use
Ctrl+K, Ctrl+D to format my code as I work.
| | 06:38 | I find it indispensable to my
sense of order while working,
| | 06:41 | and I think you will too.
| | Collapse this transcript |
| Navigating your code| 00:00 | As soon as you have more than a couple
of pages of code in a project, you'll
| | 00:03 | want to use the Visual Studio navigation
features to move intelligently through your code.
| | 00:08 | Let me show you how that's done.
| | 00:09 | I'm going to open up Visual Studio and
load this NavigatingCSharp project, and
| | 00:14 | then I'm going to open these two
C# files: Book.cs and Program.cs.
| | 00:19 | The first thing I want to talk to
you about is this outlining feature.
| | 00:22 | This is turned on by default, and
what happens in Visual Studio in say a C#
| | 00:27 | application is that every subsection of
your code--like this function here--has
| | 00:31 | an outline automatically added to it.
| | 00:33 | So I can click on this Minus sign here,
and collapse that down temporarily, so I
| | 00:38 | don't have to look at the
code that's inside that section.
| | 00:40 | You might notice there are three dots over here.
| | 00:42 | If I hover over those three dots, I'll
get a preview of what's inside there.
| | 00:46 | So I don't have to open it up just
to look at the code that's in there.
| | 00:50 | Another way of creating sections of
code is to use what's called a region.
| | 00:54 | So what I can do is I can come up
here and type in "#region", and then some
| | 01:00 | string, "hide this code", and then on line
12, I'll add a #endregion--all one word,
| | 01:09 | and you see what
happened over here in the outliner?
| | 01:11 | It added a new outlining section.
| | 01:14 | So now I can collapse my custom-made
section, and then my custom comment shows
| | 01:19 | up here for the header of that collapsed section.
| | 01:22 | This is very handy for hiding
code that you're not using anymore.
| | 01:25 | It's also used to put in things like all
your events for a class can go inside
| | 01:30 | a collapsed section called events, and
then you know how to find them later.
| | 01:33 | To navigate through your code,
| | 01:35 | there are a couple of techniques you can use.
| | 01:37 | I'm going to switch over to the Book class temporarily.
| | 01:40 | I'm looking at this code in this
function UpdatePrice, and there are a number of
| | 01:44 | curly braces in here.
| | 01:45 | There is a clue that I get from Visual
Studio about matching curly braces when I
| | 01:49 | put my cursor in front of this top
curly brace. If you look down on line 21,
| | 01:52 | you'll see that the curly brace
down there has a gray outline on it.
| | 01:57 | That's how Visual Studio tells me that
these two braces are related. But I can
| | 02:00 | also navigate down to that curly brace
by holding down the Ctrl key and tapping
| | 02:06 | the right square bracket.
| | 02:08 | Notice how my cursor moved down the
line 21. Now, I'll do Ctrl+Right Bracket, and it
| | 02:14 | moves back up to line 12.
| | 02:16 | Very handy when you have big code
files and you want to quickly move to the
| | 02:19 | bottom of the code section.
| | 02:21 | When you're looking at code, there
are a multiple ways that you can view
| | 02:25 | separate views of the code.
| | 02:27 | So I've got this Book class, and I
want to compare it with another file, say
| | 02:31 | this Program.cs file.
| | 02:33 | Right now, I can compare them, but I
have to keep toggling back and forth.
| | 02:36 | What I can do is I can pick one of these
tabs up and drag it out and place it on
| | 02:42 | the side or the bottom of the page.
| | 02:44 | Now I've got this little helper in the
center of the window that's showing me--
| | 02:47 | right now it's going to dock to the left.
| | 02:49 | And I want to move it down here,
so it's docking to the bottom.
| | 02:53 | Now I've got two separate tab sections,
and I can have multiple tabs down here
| | 02:56 | in this bottom tab section and multiple
tabs in the top tab section, and then I
| | 03:00 | can compare the text.
| | 03:01 | When I'm done, I can grab this tab
and slide it back up and push it back
| | 03:06 | into the top section.
| | 03:08 | I can also do the same thing with
multiple views of this book class.
| | 03:12 | There are two methods I can use.
| | 03:14 | One is I can go to the
Window menu and choose New Window.
| | 03:20 | And since my Book tab is selected, it
will create another copy of the book tab.
| | 03:24 | And then I can use the technique
I just showed you to grab this.
| | 03:27 | And this time I'll move it over and
slide it side by side, and now I've got two
| | 03:32 | separate views of the same physical file.
| | 03:36 | The other way of splitting the view is
to use this little button over here on
| | 03:40 | the top of the scrollbar, the
one that has the two arrows on it.
| | 03:43 | If I grab that and slide it
down, I'm splitting the view.
| | 03:46 | I still have one tab, but I have two views.
| | 03:49 | To unsplit it, I can take this and
either slide it up, or the fast way to do it
| | 03:54 | is to double-click on this black line.
| | 03:57 | Next, I want to show you how to
navigate to name sections of your code.
| | 04:01 | I have this class called Book.
| | 04:02 | It has a function called UpdatePrice.
| | 04:05 | It also has a function
down here called OtherMethod.
| | 04:07 | So I'm going to go over here to this
Program file, and I'm going to show you
| | 04:12 | that I am calling UpdatePrice here, and
I'm instantiating the Book class here.
| | 04:17 | So what I can do is put my cursor on
Book and then press F12 to navigate over
| | 04:21 | to that Book class.
| | 04:23 | If I am on the method and I press F12,
then it will navigate over to the method.
| | 04:29 | See, now I am on line 11.
| | 04:31 | If I choose a class that I didn't write,
like this Console.WriteLine, and I press
| | 04:37 | F12, then what Visual Studio does is it
opens up a text file that contains the
| | 04:42 | structure of this Microsoft class.
| | 04:44 | It shows me I was working with this
public static void WriteLine method.
| | 04:48 | Also, there is something
called reference highlight.
| | 04:51 | So if I select this newPrice variable--
it's actually a parameter coming into
| | 04:56 | this function--then notice that after
about a one-second delay, that line 13 and
| | 05:01 | line 15 light up and show me that
I'm using this parameter in those two
| | 05:05 | sections of my code.
| | 05:06 | This also works if I click on this
variable up here. After about a one-second
| | 05:11 | delay I see them using it on line 15,
and I'm also using it on line 25--a nice
| | 05:16 | way of visualizing the sections of your code
| | 05:19 | To continue on with this mode, I'm
going to go back to the Program file, and
| | 05:23 | this time I'm going to right-
click on this UpdatePrice method.
| | 05:27 | I'm going to use this Find All References.
| | 05:29 | What that does is open a separate
window and show me those three places in my
| | 05:33 | code that have references to UpdatePrice.
| | 05:36 | The first one, if I double-click on it, is
the place where the function is declared.
| | 05:41 | The second one, it shows me where I'm
calling it here on line 11, and when I
| | 05:45 | double-click of this one, it
shows I'm calling it on line 18.
| | 05:47 | I can also do something called
View Call Hierarchy, and this is a
| | 05:51 | slightly different view.
| | 05:52 | Now I can see all the calls
that are coming into UpdatePrice.
| | 05:55 | There is a call coming from AnotherDemo
and one from the Main function. And I
| | 05:59 | can also see that within that this
method, somewhere within my UpdatePrice
| | 06:04 | function, I am calling the OtherMethod,
which is a method that I wrote, and
| | 06:08 | I'm also calling the WriteLine function
that Microsoft created in their console class.
| | 06:13 | Finally, if none of these navigation
features are making you happy, we can use
| | 06:17 | our own custom bookmarks.
| | 06:20 | I can place little tokens in my code
and then use those as navigation aids.
| | 06:23 | First, let me close these two windows anyway.
| | 06:25 | I'm going to close the Call Hierarchy
window, and I'll close the Find Symbol
| | 06:28 | Results window by clicking on the X.
| | 06:30 | And then I'm going to drop a bookmark.
| | 06:32 | I'm going to move my cursor right here
on line 9, and then I'm going to use the
| | 06:35 | bookmark keystroke, which is Ctrl+K,
and then I press Ctrl+K a second time.
| | 06:40 | And if you look on the very left edge
of the screen, you'll see that there is
| | 06:43 | now a gray and blue rectangle
over there. That's my bookmark.
| | 06:47 | I put a couple more in.
| | 06:49 | I put one here, Ctrl+K, Ctrl+K, and
then I'll switch over to my Book class, and
| | 06:54 | put one here, Ctrl+K, Ctrl+K.
And now here comes the power.
| | 06:59 | I can use those bookmarks I've
dropped in there, and I can come up to this
| | 07:02 | Bookmark toolbar and click back or forward.
| | 07:04 | So I'll click Back.
| | 07:06 | It will then cycle me through each of my
bookmarks, and those keep going around and around.
| | 07:13 | This is really handy when you have a
big project and you're currently working
| | 07:16 | on three or four files and you're
working in separate areas and you want to
| | 07:19 | remember where you are.
| | 07:20 | So you put these temporary bookmarks in.
| | 07:23 | They affect your copy of the
project, not any other developer's copy.
| | 07:28 | And when you're done, you can just use
the same keystroke--Ctrl+K, Ctrl+K--to
| | 07:33 | turn off the bookmarks.
| | 07:34 | As you can see, there are many ways
to navigate your code besides using the
| | 07:38 | traditional up down arrows and mouse scrollbars.
| | 07:40 | Why don't you try a few of these
new techniques on your next project?
| | Collapse this transcript |
| Using the Task List| 00:00 | I suspect you keep a list of
tasks somewhere in your office.
| | 00:03 | If you want, you can manage some of
your tasks from within Visual Studio.
| | 00:07 | I will show you how to do that.
| | 00:08 | I am inside Visual Studio, and I have
opened this project called TaskList.
| | 00:11 | There are two files I want to open.
| | 00:14 | I am going to double-click on
Samples.cs and then double-click on Program.cs.
| | 00:19 | Next, I want to turn on the TaskList window.
| | 00:22 | If it's not visible on your computer,
you need to go to View > Task List.
| | 00:28 | It then lays down here on
the bottom of your screen.
| | 00:30 | Now it has a normal "add my own task" feature.
| | 00:34 | To do that, I go to this dropdown, and
I choose User Tasks, and then I click
| | 00:39 | on this Create User Task.
| | 00:41 | You can see I already have one in here.
| | 00:43 | You probably won't have
this one on your computer.
| | 00:45 | I'll click here to add a new task,
and then type in the word "Demo".
| | 00:49 | And then when I am finished the task,
I can click on this check box to say
| | 00:52 | I am done with that.
| | 00:53 | That's not really the task I want to show you.
| | 00:55 | I am more interested in the code
comment tasking that's available.
| | 00:58 | Let me show you how that one works.
| | 01:00 | I'll click on this dropdown again,
and choose Comments. And then I am going
| | 01:05 | to go up to this line of code here on line 10,
and then I want to write an Event Handler.
| | 01:09 | So I am going to type the word "TODO" on
the beginning of this line, like that.
| | 01:13 | And after about a one-second wait, you
will see that that shows up on my list.
| | 01:18 | As I continue to add items--I'll go to the
Samples file now, and I will put one more in here;
| | 01:23 | I'll put "TODO" on this line--
| | 01:24 | I should get another item showing up on my list.
| | 01:27 | Now I can use this as a navigation aid,
I can double-click on this to do, and it
| | 01:31 | takes me to that section of my code.
| | 01:32 | I've also got a bug in my code.
| | 01:35 | I'd like to be able to put bugs on this list.
| | 01:37 | What I would like to have happen is
come over here and type the word "BUG",
| | 01:40 | and have it show up.
| | 01:41 | Visual Studio doesn't support that
out of the box, but I can configure it
| | 01:45 | to accept that keyword, by going
to Tools > Options, finding the
| | 01:50 | Environment section, then clicking
on this node here, Task List. And then
| | 01:56 | these are the known tokens.
| | 01:58 | There you can see the TODO one.
| | 01:58 | What I am going to do now is type in my
own name, BUG, then click the Add button.
| | 02:07 | Also, I can specify a Priority level:
| | 02:09 | Low Priority or High Priority.
| | 02:11 | I think it should be a High
Priority and then click OK.
| | 02:14 | Now you see what happened to
my task list on the bottom?
| | 02:16 | It now shows BUG, and has an
exclamation point in this first column.
| | 02:21 | Now that you've mastered the task list,
let's move on to commenting your code.
| | Collapse this transcript |
| Commenting your code| 00:00 | There are two kinds of documentation
that are frequently written by developers.
| | 00:04 | First, we have code comments,
and second, we have help files.
| | 00:07 | In this movie, I'll cover code
comments, which explain the purpose of the
| | 00:11 | current code and help make
the code self-documenting.
| | 00:13 | I am inside Visual Studio, and I have
opened a solution called Comments, which
| | 00:18 | contains two projects:
| | 00:20 | a C# project and a Visual Basic project.
| | 00:23 | I am going to spend the majority of my
time commenting code in the C# project,
| | 00:28 | but nearly everything I show you works
exactly the same in Visual Basic, WPF,
| | 00:32 | Silverlight, HTML--you name it.
| | 00:35 | Let's start by open this Book class.
| | 00:38 | Here I have got a single-line comment.
| | 00:40 | C# comments are started by using two slashes.
| | 00:43 | I can also do multi-line comments.
| | 00:45 | Let me open the Program.cs file.
| | 00:48 | This is what a multi-line comment looks like.
| | 00:51 | You start by putting the slash and
the asterisks, and then you end the
| | 00:56 | multi-line comment by using an
asterisk and a slash. That comments out all
| | 01:00 | the code between those.
| | 01:01 | Another common use of comments is to
take lines of code out there that you
| | 01:05 | are not using anymore.
| | 01:06 | Let me show you how to do that part.
| | 01:07 | I am going to go down to these two lines
of code here and select them with my mouse.
| | 01:11 | Now there is a keystroke that I like to
use, which is Ctrl+K, Ctrl+U and Ctrl+K,
| | 01:18 | Ctrl+C; U stands for
Uncomment and C stands for Comment.
| | 01:22 | So let's start by doing Ctrl+K. Visual Studio
has this rather odd way of doing keystrokes.
| | 01:29 | There are so many keystrokes in Visual
Studio that sometimes they need to have
| | 01:32 | multiple keystrokes.
| | 01:33 | Now I have done a Ctrl+K. If you
look on the lower left-hand corner of my
| | 01:36 | screen, you will see that it says, "Ctrl+
K was pressed, waiting for second key of
| | 01:40 | chord." And what they're saying is they
are waiting for me to type Ctrl+C now.
[00:01:44 .84]
So let's do that Ctrl+C. Then that
little tip on the bottom of the screen goes
| | 01:48 | away, and the two lines that I
had selected are now commented out.
| | 01:52 | This keystroke also works in
Visual Basic. Let me show you.
| | 01:56 | I'll go over to this VB file,
| | 01:58 | Module1.vb, and then I will select a
few lines of code over here. And then I
| | 02:03 | will do the same keystroke:
| | 02:04 | Ctrl+K, Ctrl+C. This time it
comments out those three lines, but it uses
| | 02:09 | the Visual Basic-specific comment character,
which is the single quote or apostrophe.
| | 02:14 | To uncomment, I do Ctrl+K, Ctrl+U, and
if I am the kind of person that would
| | 02:20 | rather use a button on the Toolbar, I
can use either of these two buttons.
| | 02:26 | This button here is the Comment button,
and this button is the Uncomment button.
| | 02:30 | It's really a simple system.
| | 02:31 | Comment systems are core features in every
programming language I've ever worked in.
| | 02:36 | It only takes a few minutes to show the parts
of the comment system here in Visual Studio.
| | 02:40 | For a more complex documentation story,
be sure and watch the next movie in
| | 02:44 | this chapter.
| | Collapse this transcript |
| Documenting your code| 00:00 | Creating an external help file is a
common type of documentation. Note:
| | 00:04 | For this movie, I am not talking about
help for the end user of the application,
| | 00:08 | but rather about code documentation.
| | 00:11 | This documentation is intended for
other programmers to use, to get a better
| | 00:14 | understanding of a code API.
| | 00:17 | In order to build the Help files later
in this movie, you need to install some
| | 00:20 | additional open source tools.
| | 00:23 | These tools are available in your
Exercise File folder, in the Installers folder.
| | 00:31 | There are three tools that you need to install:
| | 00:33 | GhostDoc, Sandcastle, and
SandcastleBuildersSetup.
| | 00:37 | Sandcastle itself is the
help-generation tool from Microsoft.
| | 00:41 | It only works at the command line.
| | 00:43 | SandcastleBuilder is a graphics
interface that works with Sandcastle, and I am
| | 00:48 | going to show that later in this movie.
| | 00:50 | GhostDoc is a plug-in for Visual Studio
that helps you write the XML comments you need.
| | 00:56 | So install those and
then continue with the movie.
| | 00:59 | Now I am in Visual Studio.
| | 01:00 | I am inside a project called BookLib,
which is inside the Documenting Solution.
| | 01:06 | Both C# and Visual Basic support
special help comments called XML comments.
| | 01:11 | When Visual Studio encounters these XML
comments, it can generate a separate XML
| | 01:15 | file as part of the compile process.
| | 01:17 | Then this XML file is used by the Help
tools as a basis for creating the help files.
| | 01:22 | My plan is to document this
class here, called the Book class.
| | 01:25 | So I will double-click on it.
| | 01:27 | It's a simple class that has three
public properties, and I see I need to make
| | 01:32 | this class public for later. And then
it has two methods called UpdatePrice,
| | 01:38 | two overloads of the same method.
| | 01:41 | So my idea is that I want to
document this method and make it easier for a
| | 01:45 | developer who is using this
function to know how it's going to work.
| | 01:48 | What I do is I go to the line
immediately above this method, and I type in three
| | 01:53 | comment slashes. And you see the Visual
Studio stubs in these special XML tags
| | 01:59 | that are inside these three slashes.
| | 02:01 | This is a summary about what this function does.
| | 02:04 | So I will say, "Update the
price of a book based on ID."
| | 02:13 | Then I can come down here and say
something about these different parameters
| | 02:19 | "as a number", right?
| | 02:20 | Whereas these other overload down here
will have a percentage value passed into it.
| | 02:30 | Now, I am going to compile the application.
| | 02:31 | It asks me if I want to save the changes.
| | 02:36 | I am going to say yes. You may
not see that dialog on your computer.
| | 02:40 | Let's look at this from the perspective
of a developer who is using this class.
| | 02:44 | Over here in this code, I have
instantiated an instance of the book, and I am
| | 02:47 | calling UpdatePrice.
| | 02:50 | Now I am going to move my cursor
right here, and I am going to do a
| | 02:52 | Ctrl+Shift+Spacebar, which
launches the IntelliSense.
| | 02:58 | You can see that it says here "Update
the price of a book, based on ID."
| | 03:02 | And then I am going to go over to this
one, and you will see in the bottom it
| | 03:05 | says, "new price as a number".
| | 03:07 | So those comments that I just wrote are
now showing up in the IntelliSense for
| | 03:11 | the developer that's calling my method.
| | 03:13 | So you can go through your application,
sprinkling useful comments in there,
| | 03:17 | and you will get a much better
application in the long run, for the other
| | 03:19 | programmers that are using it.
| | 03:21 | Now I can formalize this and put this in a
Help file too, and then the user could press--
| | 03:26 | user in this case being another
developer--could click on this and press F1 to
| | 03:30 | launch the Help file and
read more help about it.
| | 03:32 | So what I need to do now is
continue to add XML comments.
| | 03:36 | For instance, I can come up and
add them at the class level up here.
| | 03:39 | Now this can get tedious, so there is a
special tool that you can add to Visual
| | 03:44 | Studio, called GhostDoc.
| | 03:46 | Let me show you how GhostDoc works.
| | 03:48 | It's in my Tools menu, right up here at the top.
| | 03:51 | That's because I installed it earlier today.
| | 03:55 | I'll click on this public int ID, and
then I'll go to Tools > GhostDoc and
| | 04:01 | choose Document this.
| | 04:03 | So this is very similar to me
writing the slash, slash, slash, but the
| | 04:07 | difference that GhostDoc give me is that it
attempts to write something useful in here.
| | 04:11 | So it's used the value of The ID, and
it's used the words "gets or sets the ID".
| | 04:16 | That's the main difference in using this one.
| | 04:18 | So I will do this few more times.
| | 04:19 | All right, and then I will come up to
the class, public class Book, like that,
| | 04:37 | and now I am ready to generate my Help file.
| | 04:40 | So to generate the Help file, I need to
go into my project, Properties section.
| | 04:45 | Double-click on this property item
over here in the Solution Explorer.
| | 04:48 | I need to go to Build -- let me close
this window down here in the bottom, so
| | 04:52 | you can see more--and then down here in
the bottom section of the Build tab, I
| | 04:57 | need to check the XML documentation check box.
| | 05:01 | Now when I do my compile, it's going
to generate an additional XML file.
| | 05:05 | Let me show you that file.
| | 05:07 | I will right-click on my Project,
choose Open folder in Windows Explorer.
| | 05:12 | I'll go to my compile folder, which is
bin > Debug, and there is my executable,
| | 05:20 | and there is my XML file.
| | 05:22 | Now I am ready to make the actual Help file.
| | 05:26 | To do that, I need to run that Sandcastle tool.
| | 05:31 | So I am going to go out to my
Start menu and type in "Sandcastle".
| | 05:36 | I am using this one here, the Sandcastle
Help File Builder GUI. Maximize the window.
| | 05:43 | Then I will go to File > New Project.
| | 05:46 | I'll put this on my Desktop, in my
Exercise Files folder, Chapter 05.
| | 05:55 | You can see I have already
generated some earlier today.
| | 05:57 | I'll Click on Save.
| | 05:59 | I am going to call this one FirstTrial. Oops!
| | 06:03 | I went one folder too deep.
| | 06:05 | Let me go back one folder. There we go.
| | 06:06 | FirstTrial. I am nearly done.
| | 06:09 | There are a couple of changes you need to make.
| | 06:11 | One is you need to change your output folder.
| | 06:13 | If you're storing your files in the
Desktop, you cannot compile the Help file.
| | 06:17 | So I need to click on this sort
alphabetical, and then scroll down here to the
| | 06:23 | WorkingPath. And I need to pick a
folder name somewhere that won't get erased.
| | 06:28 | My C drive. And then can
over here I can say BuildHere.
| | 06:32 | Now what's going to happen is when I
compile my Help, it's going to generate
| | 06:35 | this folder, make all the files, and then
when it's done it's going to erase this folder.
| | 06:39 | So don't point this to any folder
that you care about your files, okay?
| | 06:43 | I am ready to do my Build.
| | 06:45 | Now I just need to tell it where to
find that XML file. So I come over here to
| | 06:48 | the Documentation Sources, right-click,
and choose Add Documentation Source, and
| | 06:53 | then I need to go over to that bin
folder I showed you a few minutes ago.
| | 06:56 | Documenting > BookLib > bin, now
I am going to the Debug folder.
| | 07:01 | Let's choose this XML file and click Open.
| | 07:04 | At this point, I think I am
ready to make the Help file.
| | 07:07 | So I go to Documentation
and choose Build Project.
| | 07:12 | My build is done. The Help folder was
generating when I did the Build.
| | 07:16 | You'll see that there is
a Documentation.chm file.
| | 07:20 | I can run that from this folder, or I
can run it from the Sandcastle build tool.
| | 07:24 | I'll run it from here. And as you
can see, it's a basic Help file.
| | 07:29 | It shows I have a class called Book.
| | 07:30 | There is my description I
wrote: "An example of a Book".
| | 07:34 | I can click on this and see the
syntax for the Book, and you see that it
| | 07:38 | drives from System.Object.
| | 07:40 | I can go over here and look at the Book methods.
| | 07:42 | There are two overloads of the
UpdatePrice Method. And again, my comments
| | 07:47 | are showing up here.
| | 07:48 | If you look at the parameters, you'll
see the newPrices. The new price has a
| | 07:53 | number, so that carried over.
| | 07:55 | I am getting some warnings, because I
haven't provided all of the information in
| | 07:59 | the XML comment file, so the compiler
caught this and said, "You never provided
| | 08:03 | any information about that other
parameter," and there are the Properties.
| | 08:07 | Creating Help is a critical
part of a product lifecycle.
| | 08:09 | These Help tools are good for
creating help for other programmers.
| | 08:13 | If you are fortunate enough to be
building developer tools, then GhostDoc and
| | 08:16 | Sandcastle are just the tools you need.
| | 08:18 | If you are building consumer-based
applications, these tools are of little value.
| | 08:22 | For consumer help files, you need
to investigate third-party tools.
| | Collapse this transcript |
| Using IntelliSense effectively| 00:00 | IntelliSense is one the most helpful
features I can think of within Visual Studio.
| | 00:04 | When you are writing code, it gives you
instant, automatic, context-aware help.
| | 00:09 | I really can't imagine working
without its helpful suggestions.
| | 00:12 | There are some IntelliSense
features that are powerful but less
| | 00:15 | intuitive, however.
| | 00:16 | In this movie, I will illustrate the
countless ways in which IntelliSense helps
| | 00:19 | you write your code.
| | 00:20 | To show you IntelliSense, I
have to have a code window open.
| | 00:23 | So I'm going to go into Visual Studio
and open this UsingIntellisense project,
| | 00:28 | and I'm going to open this Programs.cs file.
| | 00:30 | I've already got some code in here,
which I'll use later in this demonstration.
| | 00:34 | For now, I'm just going to
declare a variable of type book.
| | 00:37 | Book is one of the classes that
I have inside this application.
| | 00:40 | So I'm going to typing "var b = new", and
I'm going to start typing the word "book".
| | 00:47 | I don't even need to get
the capitalization correct.
| | 00:49 | I'll type "bo", and you can see that
this IntelliSense dropdown shows that there
| | 00:53 | are four types that meet my
requirements: Book, bool, Boolean. The last one is a
| | 00:59 | bit of a stretch, but you can see that
in the middle of the word is the word
| | 01:02 | Bound, and that has Bo in it.
| | 01:04 | To write the rest of my code I'm
going to press Tab, or I'll go back here.
| | 01:09 | Now I backed up a couple of letters,
and I don't get the automatic dropdown.
| | 01:13 | To force the dropdown again, I can
use one of my favorite keystrokes inside
| | 01:17 | Visual Studio: Ctrl+Spacebar.
| | 01:20 | Hold down the Ctrl key, tap
the Spacebar, and there's Book.
| | 01:24 | The reason Book is selected is because
it remembers the last word that I used in
| | 01:29 | this IntelliSense window.
| | 01:30 | Now I'm going to type the open
parenthesis, and you see what happened?
| | 01:35 | I didn't have to type O and K. I
just had to type the open parenthesis.
| | 01:38 | It's also works with things like semicolon.
| | 01:41 | We'll come back here. Do a Ctrl+Spacebar.
Book is selected. Now, I'm going to
| | 01:45 | type the semicolon, and you see it finished
typing the word "Book" and then put my semicolon in.
| | 01:50 | Force yourself to make a
habit of learning these shortcuts.
| | 01:53 | Next, I'm going to call a
method on the console class.
| | 01:56 | So I'm going to close method called WriteLine.
| | 01:59 | Now it just so happens that the author
of the console class wrote 19 different
| | 02:04 | versions of the WriteLine function.
| | 02:06 | So the IntelliSense knows that there's 19
different versions of the WriteLine
| | 02:09 | each one varying slightly by the
type of parameters they have, and it's
| | 02:13 | showing me that there are 19.
| | 02:15 | I can click on this down arrow to refresh
my memory what these different methods are.
| | 02:19 | For example, this one takes
a one value of type char.
| | 02:22 | I also get some specific text down here
telling me more about this function.
| | 02:27 | Let me delete that line of code.
| | 02:29 | Next, I'm going to show you
something called Smart tags.
| | 02:31 | "var x = new". And I'll start typing a
class that doesn't exist, like the Barn class.
| | 02:42 | There is no Barn class here.
| | 02:46 | So the IntelliSense engine is going to
warn me that I have done something stupid
| | 02:50 | by putting the red squiggle there.
| | 02:52 | If I hover my mouse over it, it says,
"The type or namespace Barn could not be
| | 02:56 | found," and give me some
suggestions of what to do.
| | 02:58 | That's because it's examined all my types
and can't find the definition of Barn anywhere.
| | 03:03 | Now if I select the word Barn, you will
see that I get this thing called a smart tag.
| | 03:07 | Now, smart tags are kind of hard to activate.
| | 03:09 | You have to move your mouse over
this, click here one time, and then I'll
| | 03:14 | see this dropdown menu.
| | 03:15 | It says, "Would you like to
generate a class for the Barn."
| | 03:19 | I'll show you how to generate a class later.
| | 03:20 | Let me just show you how to
use this Generate new type.
| | 03:22 | I'll click here, and I get a dialog,
and it's asking me the access scope
| | 03:26 | for this new type, what kind of type I want:
class or struct or an interface or an enumeration--
| | 03:32 | I think I'm going to choose struct--
and then what file to create barn.cs.
| | 03:37 | Then I'll click OK.
| | 03:38 | Now you notice that my red squiggle goes away.
| | 03:41 | I now have a new file in my Solution
Explorer. And if I pop that open and take a
| | 03:46 | look inside, you'll see that there
is a public struct Barn in there.
| | 03:49 | Now let me return back to the
code I was working with a second ago.
| | 03:52 | I'll go back to Program.cs.
| | 03:54 | I also have a green squiggle here.
| | 03:56 | That's because I've declared this
variable, but I haven't used it anywhere in my
| | 03:59 | code--suggesting that either I forgot to
do something with it, or I should erase
| | 04:04 | this line of code, because I'm not
using anywhere else in my application.
| | 04:07 | Next, I want to show you
something called autocompletion.
| | 04:10 | Let me delete this line of code,
and I'm going to type in "var x = new".
| | 04:17 | Now watch carefully what I'm going to do.
| | 04:19 | I'm going to type a capital S and then a capital L.
This is really neat what's going on here.
| | 04:24 | There is a type available that has
a capitalized letter S and an L for
| | 04:29 | SortedList, and it's
picked that out of the list.
| | 04:31 | There is no word that starts with SL,
| | 04:33 | so it's showing me the SortedList.
| | 04:35 | Now all I need to do is press one of those
keystrokes I showed you earlier. Space, Tab, Enter.
| | 04:41 | I'm going to press the Tab key,
and it stubs in that new SortedLlist.
| | 04:45 | This also works if you have your own variables.
| | 04:47 | I will come over here and
say "var ReallyBigNumber = 9".
| | 04:54 | Then down here I'm going to type in
"RBN", and it's finding that in the list.
| | 04:59 | Even when I only had typed RB, it shows
me that there's really big number, and
| | 05:03 | there is something called
EncoderReplacementFallbackBuffer.
| | 05:05 | So I'm going to type in N, and now I'm
going to press Tab key to have it finish typing.
| | 05:11 | Let Visual Studio do the typing for you,
and it'll save you lot a keystrokes.
| | 05:15 | Next, I want to show you
something called generate from usage.
| | 05:18 | I already have a real customer class
over here, but I'm going to sabotage my
| | 05:21 | application by coming over here and
removing this customer class, which of
| | 05:26 | course will break my code.
| | 05:28 | Now I get the red squiggle,
and I get the smart tag.
| | 05:31 | I am going to come over here to my smart
tag and choose Generate Customer, which
| | 05:34 | adds this new file, Customer.cs.
| | 05:36 | Now here is the neat part.
| | 05:38 | I can go down to the first name and right-
click on that or click on the smart tag.
| | 05:43 | Sometimes it's hard to get these to pop up.
| | 05:44 | So I can force them to show
by using Ctrl+Period like that.
| | 05:48 | I find that easier than trying to mouse over it.
| | 05:50 | So I'm going to say, generate a property stub.
| | 05:52 | I am going to click on this one and do Ctrl+Period.
| | 05:55 | Do the same thing two more times. Ctrl+Period.
| | 05:59 | Then on line 26, this is a function
call, because of the parentheses.
| | 06:04 | So when I do Ctrl+Period
| | 06:05 | down here, it says, "Would you like
to generate a method stub for Save."
| | 06:09 | Now all of my squiggles go away, and if I
look inside my Customer.cs file, you'll
| | 06:13 | see that it's stubbed in
some properties and a method.
| | 06:16 | Now I've still got some work to do, but
I no longer have my IntelliSense errors
| | 06:20 | over in the other file.
| | 06:22 | My last demo is going to be showing you
how to work with these using statements
| | 06:25 | at the top of my code window.
| | 06:27 | You might recall that using statements
are there so that I don't have to type
| | 06:30 | all of the names of the class down
here in the code section of my window.
| | 06:34 | What I can do in Visual Studio is I can
right-click up here and choose Organize Using.
| | 06:39 | Then I can sort them in alphabetical
order. Or what I'm going to choose is I'm
| | 06:43 | going to sort them in alphabetical
order and also remove any usings that I'm
| | 06:46 | not currently using.
| | 06:48 | So that removed three of the four using
statement I had at the top of the page.
| | 06:52 | I can't say enough good things about the
IntelliSense support inside Visual Studio.
| | 06:55 | It's a feature that I simply couldn't
live without, because it is so helpful
| | 06:58 | when I write my code.
| | Collapse this transcript |
| Working with code snippets| 00:00 | A few years back, Visual Studio
introduced the idea of code snippets.
| | 00:03 | Snippets are small sections of code
that can be automatically generated and
| | 00:06 | pasted into your code.
| | 00:08 | They show up in IntelliSense and are
customizable, so that you can modify
| | 00:11 | the injected snippet.
| | 00:12 | Let me show you how to use a C# snippet.
| | 00:16 | I am inside Visual Studio,
and I have opened this solution
| | 00:18 | called UsingCodeSnippets.
| | 00:20 | I have two projects in here:
a C# project and a Vb project.
| | 00:24 | I'll start by opening this Program.cs
file, and then I'll add my first snippet.
| | 00:29 | I will start by typing in the word "if".
| | 00:32 | Now if you look in my IntelliSense
dropdown, you will see the word "if" and to
| | 00:36 | the immediate left of it is a symbol that looks
like a piece of paper with some writing on it.
| | 00:40 | That is the symbol for a code snippet.
| | 00:42 | Now if I press the Tab key twice, once
will dismiss that dialog, and the second
| | 00:48 | time I press Tab, it stubs in the code snippet.
| | 00:51 | In my case, you see it wrote a little
bit of code on the line and also wrote two
| | 00:55 | curly brackets for me.
| | 00:56 | Some of the code snippets
are much bigger than this.
| | 01:00 | For my next snippet, I am
going to use a for snippet.
| | 01:02 | It's going to be slightly
larger than the if statement.
| | 01:04 | I will type in "for" and
then press the Tab key twice.
| | 01:09 | Now what I want you to see here is
that the letter I is in their three places,
| | 01:13 | and notice how each one of
them has some gray around it.
| | 01:15 | So I am going to type in a new word for
i. I am going to type in the word "counter".
| | 01:20 | Now when I press the Tab key, it's going
to move me to the next area, which is length.
| | 01:25 | Also notice what happened to those i's.
| | 01:27 | They turned into the word counter.
| | 01:28 | So they are linked together.
| | 01:30 | If I go back here and make a change and
press the Tab key again, you see that
| | 01:35 | it's constantly monitoring that first item and
changing the other two items. That's very handy.
| | 01:40 | Next, I will show you an even larger one.
| | 01:42 | This is a property for WPF.
| | 01:44 | I am going to type in "prop dp".
| | 01:47 | That's stands for dependency property.
| | 01:49 | Now I will press Tab twice, and you
can see it's stubbed in about eight or
| | 01:53 | nine lines of code.
| | 01:54 | Again, I've got these placeholders.
| | 01:56 | So I can type in the word "Boolean"
here or "bool", and then Tab, "IsReady", and
| | 02:02 | you'll see that everywhere where it
says MyPropertyProperty now, when I press
| | 02:06 | the Tab key now, those
change to say IsReadyProperty.
| | 02:10 | This property down here is changed to be
Boolean, to match what I typed in up here.
| | 02:14 | So did this one.
| | 02:16 | Now those clever people over in the
Visual Basic team, they are working really
| | 02:19 | hard to simplify code entry.
| | 02:21 | They have created about 200 snippets,
which is more than the C# team did.
| | 02:25 | So I am going to switch over to this
Module1.vb file and show you how to add a snippet here.
| | 02:30 | The trigger in Visual Basic is to type
the question mark and then the Tab key.
| | 02:36 | Now, I see several folders.
| | 02:37 | Each one of these folders
has lots of snippets in it.
| | 02:40 | I encourage you to explore these snippets.
| | 02:43 | I only have time to show you one.
| | 02:44 | So I'm going to go down here and show you
some Office Development code snippets.
| | 02:48 | I am going to open this one up and
then choose Word and then Formatting, and
| | 02:53 | here are some of the things I can
put in if I'm doing some automation and
| | 02:56 | adding items to the Word object model.
Like I am going to choose this Highlight a Range,
| | 03:00 | and you can see that it stubbed this piece in.
| | 03:03 | So I encourage you to spend
sometime looking at the vb snippets.
| | 03:06 | I'd rather spend the time
writing our own code snippets.
| | 03:09 | To do that, I need to write some code,
and I need to generate an xml file.
| | 03:14 | To make it easier, there is a free tool
for you to use, called Snippet designer.
| | 03:19 | I have already installed it on my machine.
| | 03:20 | Let me show you where it is.
| | 03:21 | I can go to Tools > Extension Manager,
and there you can see that I have
| | 03:27 | installed extensions.
| | 03:28 | I have this one called Snippet Designer.
| | 03:30 | You're not going to see this on
your computer probably, unless you've
| | 03:32 | already installed it.
| | 03:34 | On your machine, you're going to want to
click on the Online Gallery, which takes
| | 03:38 | you out to a Microsoft web site and
shows you all of the available extensions.
| | 03:42 | I have a separate movie in this title
that deals with adding your own extensions
| | 03:47 | and using extensions.
| | 03:48 | Later in this course, there's a
movie showing you how to write your own
| | 03:51 | extensions and how to use
these pre built extensions.
| | 03:54 | For right now, just follow these instructions.
| | 03:56 | Type the word "snippet" over here in
the Online Gallery search engine,
| | 04:03 | then pick snippet designer, and then
you are going to click on a download.
| | 04:07 | I don't have a download button, because I've
already installed it, but you will have one.
| | 04:11 | Click on that, and about 10 seconds later you
will have Snippet Designer on your machine.
| | 04:15 | Now you need to restart Visual Studio.
| | 04:18 | Once that's done, I can go find some code.
| | 04:21 | I've got some C# code.
| | 04:22 | So I am going to switch back to my
Program.cs file, and I am going to uncomment
| | 04:26 | these four lines of text,
Ctrl+K, Ctrl+U for uncomment.
| | 04:32 | Now I'm going to right-click on them
and choose Export as Snippet, which will
| | 04:38 | send them to that
extension tool I just showed you.
| | 04:41 | Here is that Snippet Designer.
| | 04:42 | Now what I want to have happen when
you enter this snippet is I want you to
| | 04:46 | be able to type in a new value here
and replace it on line two and on line
| | 04:51 | three and on line five.
| | 04:53 | So what I do is I right-click on
this and say Make Replacement.
| | 04:56 | That puts a token in there called myList,
and that tells snippet engine to use
| | 05:02 | that replaceable syntax. So now I am ready.
| | 05:05 | I am going to close this.
| | 05:06 | It says, "Would you like to save the changes."
| | 05:08 | I am going to say yes.
| | 05:09 | I need to save this snippet file out
in the location where Visual Studio
| | 05:13 | looks for these snippets.
| | 05:15 | That would be in Documents > Visual
Studio 2010 > Code Snippets > C#, and of
| | 05:22 | course, here's My Code Snippets folder.
| | 05:24 | I will open this one up and then click Save.
| | 05:27 | For some reason, on this computer
I keep getting this error dialog.
| | 05:30 | I'm not sure if you are going to see it or
not, but the snippet seems to work correctly.
| | 05:33 | So I am going to click OK.
| | 05:35 | Now in order for Visual Studio to load
that snippet, I need to restart Visual Studio.
| | 05:39 | Then I want to create a brand-new project.
| | 05:42 | I am going to make sure it's a C# project.
| | 05:44 | I am going to put it out in my movie folder.
| | 05:46 | I'm in the chapter 05, in movie 7.
| | 05:48 | And if I did everything correctly, I
should be able to use my code snippet.
| | 05:53 | Right-click. Choose Insert Snippet.
| | 05:56 | There's my Code Snippets Folder, there's my
Snippet File number1, and there's my list.
| | 06:03 | Then again, if I did everything correctly,
I should be able to type in a new name
| | 06:05 | here, fileNames, and press the Tab key.
| | 06:09 | The Tab key is not working for me.
| | 06:10 | How about if I just
clicked down here? That worked.
| | 06:13 | That is Awesome! Look at that!
| | 06:14 | The file name changed in all four places.
| | 06:16 | So as you have seen, code snippets are
incredibly useful, and they are so easy to create.
| | 06:21 | Have fun exploring and see what
you can do with code snippets.
| | Collapse this transcript |
| Refactoring your code| 00:00 | Writing code is a constant push
and pull on your development skills.
| | 00:04 | First, you write some code and test it
to see if it works the way you intended.
| | 00:07 | Then as the project progresses, you think
about ways to improve and change the code.
| | 00:11 | Some people call this rewriting refactoring.
| | 00:14 | Regardless of how you define it, Visual
Studio has refactoring tools to help.
| | 00:17 | If you are a C# developer, then you
don't have to do anything special, as there
| | 00:21 | are about 10 refactoring
tools built into Visual Studio.
| | 00:25 | If you are a Visual Basic programmer,
you should download the free CodeRush
| | 00:28 | Xpress tool, which provides
dramatic code refactoring support.
| | 00:32 | I'm inside Visual Studio, and I've
opened the RefactorYourCode Solution, which
| | 00:38 | contains a C# and a Visual Basic project.
| | 00:41 | I will start my refactoring
journey by going to Program.cs.
| | 00:45 | A common refactoring is when
you need to rename something.
| | 00:48 | I am going to rename this variable.
So I will put my cursor on the variable, and
| | 00:52 | I will right-click and choose Refactor.
| | 00:54 | Then I will choose Rename.
| | 00:57 | Also note the keystrokes Ctrl+R,
Ctrl+R. Now during this refactoring, a
| | 01:03 | dialog box will appear.
| | 01:04 | I can type in my new variable name,
and then I can choose to preview the
| | 01:09 | changes inside the dialog.
| | 01:11 | I can also choose where to search for this word.
| | 01:14 | I am going to click OK. Here's my preview screen.
| | 01:17 | It shows that it's going to change line 13.
| | 01:19 | It's a little bit harder to see, but it
shows it's also going to change line 18.
| | 01:23 | These changes look good to me, so I am
going to click the Apply button, and I
| | 01:28 | successfully changed those two lines of code.
| | 01:30 | Next, I want to save the method name.
| | 01:32 | So I'll click on the save book, which I am
using twice. I am using it on line 21 and on line 18.
| | 01:38 | I am going to click here and use the
keystrokes Ctrl+R, Ctrl+R. Make a slight
| | 01:44 | change. Call this SaveTheBook.
| | 01:47 | Again, choose to preview reference
changes, and it shows in my preview screen
| | 01:52 | several places where this
code is going to be updated.
| | 01:54 | I can click through these
different elements up here to look.
| | 01:57 | I'll click on Apply.
| | 01:58 | And if I look in the Book.cs
file, I also see that it changed it
| | 02:03 | successfully over here.
| | 02:05 | Next, I want to go to the MathLib file
and show you the Extract Method Refactor.
| | 02:11 | I look at this code, and I'm thinking,
"I want to reuse this in my application.
| | 02:16 | It shouldn't live in entirely
inside this CalculatePaintNeeded Method."
| | 02:19 | So what I can do is come over here,
choose Refactor > Extract Method.
| | 02:25 | I'll get the dialog box again, and I think
the new name should be called CalcArea,
| | 02:32 | and as you can see Visual Studio wrote
a new function for me, copied my code
| | 02:36 | down there to line 24, and then changed
my code on line 17 to call that method
| | 02:41 | and pass in the necessary
information that's needed for this function.
| | 02:45 | Next, I am going to switch back to
the Book and show you the reorder
| | 02:48 | parameter function.
| | 02:50 | I have three parameters here.
| | 02:51 | We decided on our team that this
first string variable belongs at the end.
| | 02:56 | So what I do is I put my cursor on
SaveTheBook, choose the Refactor menu, and then
| | 03:02 | choose this one, Reorder Parameters.
| | 03:05 | Another dialog crops up.
| | 03:06 | It shows the three parameters.
| | 03:08 | I am going to pick this first
parameter and then the down arrow, click on it
| | 03:11 | twice to move it to the
end of the parameter list.
| | 03:14 | I feel good about not previewing my
changes, so I am going to click on OK, and
| | 03:18 | now you see the parameter has
moved to the third position.
| | 03:20 | Now this a bit tricky, because I'm
calling this function from a couple of areas
| | 03:24 | in my code--calling it over here in
Program.cs--so let's verify that it changed
| | 03:28 | correctly over there.
| | 03:29 | Yes it did, look on line 18.
| | 03:31 | File path is now the last argument, and this
string down here is also the last argument.
| | 03:37 | My last refactoring for C# is
going to be Extract Interface.
| | 03:41 | That's a common refactoring pattern.
| | 03:42 | I am going to go to this Book
class, and I want to take these two
| | 03:45 | properties, title and price, and I
want to pull them out of the class and
| | 03:48 | put them in a common interface.
| | 03:50 | So I will choose the Book class,
invoke the Refactor menu, choose
| | 03:54 | Extract Interface, and then look
at this interface name and see if it
| | 03:57 | meets my requirements.
| | 03:58 | I like IBook. Next, I come down,
and I choose the two properties.
| | 04:02 | I want to leave the method
inside the class and then click OK.
| | 04:06 | Visual Studio creates a brand-new
file over here--IBook.cs--extracts my
| | 04:11 | interface and then correctly
fixes up my code in the Book class to
| | 04:16 | implement that interface.
| | 04:17 | If you are a Visual Basic
programmer, you don't have refactoring in
| | 04:20 | Visual Studio by default.
| | 04:22 | The good news is if you like what you see in
Express, it's also available for C# programmers.
| | 04:26 | If you plan on following along with me in
this demo, you need to install the Express tools.
| | 04:31 | I've included those in the Installers
folder inside your Exercise Files folder.
| | 04:37 | So you go to Installers and
then run this Express Installer.
| | 04:40 | When I ran Visual Studio the next time
after installing DevExpress, it asked me
| | 04:44 | how you would like to run DevExpress,
and I told it to let me load it manually
| | 04:49 | when I start up the application.
| | 04:50 | So I am going to go to this
menu, DevExpress, and pick Load.
| | 04:53 | And immediately you will see
some changes in my C# code.
| | 04:56 | It's now drawing a red line between
my curly braces and other colored lines
| | 05:01 | between like my if statements here.
| | 05:03 | It will also do the same
thing over in my vb code.
| | 05:06 | There are more
refactoring tools in Visual Studio.
| | 05:08 | I have only shown a few of them in this movie.
| | 05:11 | If you like Visual Basic, then
check out the VB refactoring title in
| | 05:14 | this chapter.
| | Collapse this transcript |
| Understanding code generation| 00:01 | Often, when writing your application,
you will come across large areas of
| | 00:05 | boilerplate code in which the same
code pattern is repeated over and over.
| | 00:09 | Whenever you find yourself repeating the
same mindless task, you should consider
| | 00:13 | automating the process.
| | 00:15 | The good news is other people have
considered this problem before you and have
| | 00:18 | written code generation tools.
| | 00:20 | Code generation is where software--
rather than a human--is used to write program
| | 00:24 | components automatically.
| | 00:26 | A number of commercial and free code
generators are available in the market.
| | 00:30 | Visual Studio uses generators
in many of the UI designers.
| | 00:34 | They have the drag-and-drop
designers generate the XML or code.
| | 00:39 | Another popular use of code generators is
in object relational mapping tools, or ORM.
| | 00:44 | ORM tools take the tables and other
constructs inside a database and turn them
| | 00:50 | into language objects.
| | 00:52 | There is an active community of third-
party vendors making code generation tools.
| | 00:56 | Here is a list of some of the
code generators on the market.
| | 00:59 | The most common pattern I see is
for code generators is the following:
| | 01:03 | You create a template file, which
contains your portions of your code.
| | 01:08 | The template file also contains tokens.
| | 01:12 | You take your template file and you
pass the template file to a generator.
| | 01:17 | Optionally you pass other external
parameters to the generator--say a
| | 01:20 | database connection string.
| | 01:21 | Here is an example, on the bottom of
this slide, of a token from CodeSmith.
| | 01:27 | This has an angle bracket, a percent, a
word called current time, and then the
| | 01:32 | closed percent and close angle bracket.
| | 01:34 | This will tell the code generator to
calculate the current time and place it in
| | 01:38 | your code template at this point.
| | 01:41 | When you run the generator,
you'll pass in parameters.
| | 01:44 | Some of the things you might pass
in are connection strings to open the
| | 01:47 | database, evaluate database schemas,
find files in your hard drive, or read
| | 01:52 | information from your UI tools.
| | 01:55 | Then the code generator will replace
the tokens with the runtime information.
| | 02:00 | Visual Studio has a code generator, known as T4.
| | 02:04 | I would like to show you how that works,
and it's the topic of the next movie
| | 02:08 | in this chapter.
| | Collapse this transcript |
| Generating code with T4| 00:00 | Visual Studio includes a code
generator that creates files from a simple
| | 00:04 | template definition.
| | 00:05 | It has an awkward name;
| | 00:06 | it's known as the Text
Template Transformation toolkit.
| | 00:10 | Most of us just call it T4.
| | 00:12 | I am inside Visual Studio, and I
have opened the CodeGenWithT4 project.
| | 00:17 | Let's think about what we
are going to do for a minute.
| | 00:19 | I have code that I write over and over
again that has some replaceable bits.
| | 00:23 | So what I am going to create is a
scaffolding of the basics of my code, and then
| | 00:28 | I am going to have these little
replaceable parts that the code generator will
| | 00:32 | replace on my behalf.
| | 00:33 | To create this code template, I need to
add a T4 template, by right-clicking and
| | 00:38 | choosing Add > New Item. And I'll type
in "template" in the Search dialog and
| | 00:44 | pick this simple Text Template right
here, and then I am going to call this one
| | 00:48 | SimpleDemo and click Add.
| | 00:53 | I get warned by Visual Studio that
this template may harm my computer.
| | 00:57 | I am going to go ahead
and risk it by clicking OK.
| | 00:59 | At the top of this template is
some instructions of what to build.
| | 01:03 | It's going to create a C#-specific
class, and the extension on the file that
| | 01:08 | it's going to generate is txt.
| | 01:10 | So if you come over here and look
underneath these SimpleDemo.tt file, by
| | 01:14 | expanding this node, you'll see
that there is a text file there.
| | 01:18 | I want this to be a C# file, so I am
going to change the extensions from .txt to
| | 01:24 | .cs, and then I am going to build
the application. I'll accept the risk.
| | 01:31 | And if you look over in the Solution
Explorer now, you'll see that this it
| | 01:34 | says SimpleDemo.cs.
| | 01:37 | Next, I'll create the basic scaffolding.
| | 01:40 | Some of this text is never going to
change, like a Using statement, at the top of
| | 01:44 | my screen, using System,
or public class Customer.
| | 01:52 | This is known as a text block, which
means it's entered exactly as I type it here.
| | 01:57 | Watch what happens now when I do a build.
| | 01:59 | I'll choose Build Solution,
click through these dialogs.
| | 02:04 | Then I am going to open the
SimpleDemo.cs file, and you see it's starting to
| | 02:08 | generate some code for me.
| | 02:10 | There are more sophisticated kinds of
instructions you can put in your template.
| | 02:14 | You can create something called an
expression block. That computes the value, and
| | 02:17 | then inserts it into your output file.
| | 02:19 | You can also create something called
the statement block, which allows you to
| | 02:22 | run any arbitrary code.
| | 02:24 | You can also create something called a
class feature block, which allows you to
| | 02:27 | embed code sections within the template.
| | 02:29 | Let me show you how to do an expression block.
| | 02:32 | I'll switch back to the
template, "public void DoWork".
| | 02:50 | What the DateTime.Parse () method
does is it takes a string and tries to
| | 02:55 | convert it into a DateTime.
| | 02:56 | I am going to get the time
from the operating system.
| | 03:00 | So inside these double quotes, I am
going to put my expression block.
| | 03:05 | I need the special symbol for T4, which
is the open angle bracket, pound sign,
| | 03:10 | and then the pound sign
and a close angle bracket.
| | 03:13 | And then I'll use the
assignment operator here, equal.
| | 03:15 | I think I've got my code correct.
| | 03:23 | Let's see if it compiles. Excellent!
| | 03:27 | Now let's go back to SimpleDemo.cs, and
you'll see that it created this function
| | 03:31 | and inside the function is this line
of code that's declaring a variable, and
| | 03:34 | here is the piece of data that
came from the code template generator.
| | 03:37 | Rather than write a bunch of code, I
am now going to switch over to this
| | 03:40 | MakeFunction.tt file, which
is a more complete example.
| | 03:45 | Here is the same bit of code
I showed you few minutes ago.
| | 03:47 | I am also creating a variable here and
asking the computer for the machine name.
| | 03:51 | I am then going to declare a variable
that's going to hold the list of string,
| | 03:55 | one of the generic types in .NET.
| | 03:57 | And then look what is happening here.
| | 03:59 | I am calling a function that I
declared in my template code.
| | 04:03 | So down here in this section, with this
special starter--angle bracket, pound, plus sign,
| | 04:08 | then it ends here on line 36--
| | 04:10 | I've declared a function called
PrintFileNames, which basically goes out, given
| | 04:14 | a folder name, and looks to
see if the directory exists.
| | 04:17 | If it does, it walks through all of
the files in that folder and prints their
| | 04:22 | name out to my code file, and
then I call that function up here.
| | 04:26 | Let's see what happens when I run this example.
| | 04:28 | Here is that code: var fileName = new
List<Strings >, and then I am using the
| | 04:33 | initializer syntax to add
these string names to the list.
| | 04:36 | Another really exciting thing to do
with templates is to work with a database.
| | 04:41 | So in this project, I added a database
file, this Northwind.mdf file, and then I
| | 04:46 | created the data model for
that, a linked data model.
| | 04:49 | Now I can go to this code template,
this CreateBusinessClasses code template,
| | 04:55 | and in this section right here, I am
calling that Northwind data using this
| | 05:02 | thing called the NorthwindDataContext.
And then I am foreach-ing over the
| | 05:06 | database, and finding each table inside
that database. And I am looking to see
| | 05:11 | if it starts with the word "dbo.", and
if so, then I am stripping some of the
| | 05:15 | database characters off of it, so that I have
a class name that makes sense in my C# code.
| | 05:20 | Then down here, I am declaring a class,
and I am saying I want that tableName.
| | 05:25 | Then within the class, I am doing a
foreach over the DataMembers--this would be
| | 05:30 | like the column names of the table--and
I am writing a little bit of code here
| | 05:34 | to create a public property.
| | 05:35 | So when I run this template, what I end up with
is a list of classes that map to the database.
| | 05:41 | That's why they call it
Object Relational Mapping.
| | 05:43 | There is a table named Categories.
| | 05:45 | I created a class called Categories.
| | 05:47 | There was a Territories up here,
and it has a TerritoryDescription;
| | 05:50 | that's the column name inside the
database, and that becomes a public
| | 05:53 | property on my type.
| | 05:54 | This is a very powerful mechanism.
| | 05:57 | I've used it very successfully on
some big projects, where we generate our
| | 06:00 | schemas automatically.
| | 06:01 | And if somebody changes the database
schema, the next time we build our code,
| | 06:06 | let's say they change this
column name in the database,
| | 06:09 | it automatically changes our codes to
reflect the real name inside the database.
| | 06:14 | I think code generation done right, it
can be a productivity boost for you and
| | 06:18 | your development team.
| | 06:20 | The included T4 Editor is a little
sparse, but you can always install a free
| | 06:24 | Visual Studio extension from a company
called Tangible, for better syntax help.
| | Collapse this transcript |
| Using the Class View, Class Designer, and Class Diagram tools| 00:00 | Classes and structures are the heart of
building an object-oriented application.
| | 00:04 | When you are creating these classes, you
can use a tool called Class Designer to
| | 00:08 | simplify integration tasks.
| | 00:10 | If you are looking at existing
classes, you navigate through your class
| | 00:12 | structure with the Object
Browser or the Class View.
| | 00:15 | Let's start by looking at the Class View.
| | 00:17 | I am inside Visual Studio, and I have
opened a project called UsingClassWindows.
| | 00:21 | You can see I have two folders
in here that contain classes.
| | 00:24 | I have a BookSeller folder, which
contains five classes, and a DataServices
| | 00:29 | folder, which contains this DataShuttle class.
| | 00:32 | I can investigate my class
structure by using the Class View.
| | 00:35 | To View the Class View, I go to the
View menu and then choose Class View.
| | 00:41 | It lives over here next
to the Solution Explorer.
| | 00:44 | It's usually a tab on the same area
of the screen as the Solution Explorer.
| | 00:48 | If you look at the top of
this Class View, you'll see three
| | 00:50 | different namespaces.
| | 00:52 | The first namespace contains one
class, my console program class.
| | 00:56 | The second namespace,
| | 00:58 | the BookSeller namespace, contains
these five classes I showed you, and then
| | 01:01 | this last namespace here contains one class.
| | 01:05 | Now if I double-click on one of these
classes, it takes me over to the source code.
| | 01:09 | I am thinking that I put this
class in the wrong namespace,
| | 01:13 | so what I can do is come up here and
edit my code, and say this belongs in
| | 01:16 | the BookSeller namespace. And after a
few seconds, when you look over in the
| | 01:20 | Class View, you'll see that the third
namespace is disappeared. And now if I
| | 01:24 | expand this section, you'll see
that the DataShuttle is now part of the
| | 01:28 | BookSeller namespace.
| | 01:29 | The Object Viewer is another way to
View the classes and their members.
| | 01:33 | The difference between the Object Viewer
and the Class View is that the Object
| | 01:36 | Viewer provides a drilldown into the
objects in any reference .NET assembly,
| | 01:40 | not just the ones that you have written.
| | 01:42 | Again, I am going to return to the View menu.
| | 01:45 | This time I'll choose the Object Browser.
| | 01:48 | It lives in the center of the screen.
| | 01:50 | This shows all of the reference
namespaces that I have in my project.
| | 01:54 | I can search for something like, say
the Console window, and once I find the
| | 01:59 | type that I am interested in, I can
click on it and in this section of the
| | 02:03 | screen, I can see all of its members.
| | 02:04 | There are all the methods that are part of
System.Console, and here are all of the properties.
| | 02:09 | When I click on one of these properties,
I can read about it down here in this
| | 02:13 | bottom section. Same with the methods.
| | 02:16 | So it's another way of examining
the classes that somebody else has
| | 02:20 | written. Very handy.
| | 02:22 | The Class Designer exists to make it
simple to diagram and model your classes.
| | 02:26 | It provides a visual representation
of your classes and the relationships
| | 02:30 | with other entities.
| | 02:31 | It also provides a way of
modifying those classes.
| | 02:33 | Here is how you create a Class Diagram.
| | 02:35 | You find one of your types, say this
Book class. Before I go any further, let me
| | 02:40 | close this Object Browser. Close that window.
| | 02:42 | Right-click on this Book.
| | 02:44 | This time I am in the Class View,
and I will choose View Class Diagram.
| | 02:48 | What you see is a visual
representation of your class, the structure of your
| | 02:52 | class, not the code itself,
but the structure of this class.
| | 02:55 | I can click on these little chevrons
here, and see the Properties on my type.
| | 02:59 | I can also, on the bottom of the screen
see the methods, properties, and fields
| | 03:03 | that are part of this type.
| | 03:04 | An alternative way of adding a
Class Diagram is to go to your solution,
| | 03:09 | right-click on your project, choose Add
> New item, type in the word "class" here
| | 03:14 | in the Filter box, and
then pick this Class Diagram.
| | 03:17 | That adds this file here, BookSellerDiagram.cd.
| | 03:25 | That ClassDiagram1.cd was created when
I right-clicked on the book class and
| | 03:29 | told it to view the Class Diagram.
| | 03:32 | Now let's look and see what it says
here. "The Class Designer allows you to
| | 03:35 | visualize and work with types in your code.
| | 03:38 | Create new types by
dragging items from the Toolbox."
| | 03:41 | If you go over and open your Toolbox--
you may not see it on your screen, but you
| | 03:44 | can go to View, Toolbox--
| | 03:45 | here is a listing of the things I can create.
| | 03:47 | I am going to come back and show
you how to use that in just a minute.
| | 03:50 | It also says you can, "visualize the
existing types by dragging items from the
| | 03:53 | Class View or Solution."
| | 03:55 | That implies I can take this book,
and drag it over and drop it on the
| | 03:58 | surface, and then I could take this Person
class and this Employee class and drag those over.
| | 04:05 | Because the Employee and the Person
Class have a object relationship, the
| | 04:10 | diagram shows this relationship arrow.
| | 04:13 | If I double-click on the Employee model, I
can see that Employee derives from Person.
| | 04:19 | I can use the Class Diagram to diagram
other classes that aren't part of my project.
| | 04:25 | To do that, I need to go over to my
Class View, and then up here in the Search
| | 04:28 | dialog, I'll type the name of a namespace or
type that exists in one of the .NET assemblies.
| | 04:33 | For instance, I am going to look at System.IO.
| | 04:35 | Now, I'll slide these over and make some room.
| | 04:38 | I'll open the System.IO namespace, and
then I am going to take this File class
| | 04:43 | and drag it over, or this Stream class
and drag that over. And then I can click
| | 04:48 | on this dropdown to learn more about
the type, and I can see its methods and
| | 04:52 | other details in the bottom of the screen.
| | 04:54 | Let me switch back to this
ClassDiagram1 that was generated earlier today.
| | 04:58 | I am supposed to be able to go over
to the Toolbox and pick up one of
| | 05:01 | these items over here--
| | 05:02 | this Interface, or this Class, or
this Delegate, and these Inheritance
| | 05:06 | arrows--and I am supposed to be able to take
these and drag them over to my designer surface.
| | 05:11 | When I drag the Class over, it says,
what kind of class would you like to create?
| | 05:15 | I think I'll create a Customer class.
| | 05:16 | It's going to go in this file, Customer.cs.
| | 05:19 | Then I can come down to this bottom
section and add Methods, like the Save
| | 05:23 | method. Or I can add a
property, FirstName, and so on.
| | 05:29 | And if you double-click on this model,
it takes you over to where the code is
| | 05:33 | being generated, and I can see that
it created a class with this property
| | 05:37 | and this Save method.
| | 05:39 | So let's review what we have seen.
| | 05:40 | If you want to look at objects in
compiled assemblies, use the Object Browser.
| | 05:45 | If you want to examine or modify the
classes in your own code, use the Class
| | 05:49 | View or the Class Designer.
| | Collapse this transcript |
| Refactoring VB with CodeRush Xpress| 00:00 | If you are a Visual Basic
programmer, you don't have refactoring in
| | 00:03 | Visual Studio by default.
| | 00:04 | There is a free CodeRush Xpress tool,
however, that is sanctioned by Microsoft.
| | 00:08 | If you plan on following along with me in
these demos, you need to install the Xpress tool.
| | 00:12 | I have included a copy of the installer
in the Exercise Files folder, and then
| | 00:17 | the Installers folder.
| | 00:18 | This is the file you want
to install, CodeRushXpress.
| | 00:21 | Once you've installed CodeRushXpress,
you need to restart Visual Studio. Then
| | 00:25 | the next time you run it, you
may need to enable DevExpress.
| | 00:29 | On my machine, I have to go to DevExpress load.
| | 00:32 | For my first refactoring, I am going
to open this Program.vb file, and I am
| | 00:37 | going to do a simple rename refactoring.
| | 00:39 | I am going to click on the letter b, and
I am going to invoke the Refactoring menu.
| | 00:44 | It's down here.
| | 00:45 | It has an exclamation on end of the name.
That's how we know it's the CodeRush Refactor.
| | 00:51 | The refactoring that I want
to do today is Rename Local.
| | 00:53 | I am going to press the Enter key, and
notice that there are two blue bounding
| | 00:57 | boxes added to my window: one
on line 7 and one on line 12.
| | 01:02 | Those two variables are now linked, and
if I type a new name here at the top, it
| | 01:07 | appears online 12 as well.
| | 01:09 | Later, if I go back and click on this item
again, you'll see that the link still exists.
| | 01:14 | I can then choose to rename it.
| | 01:16 | Next, I am going to do an
extract method refactor.
| | 01:19 | To do that, I am going to
open this file, MathLib.vb.
| | 01:23 | This refactor, I am going to
take this small calculation here, and
| | 01:26 | I am going to extract it out into a
separate method, because I think I might
| | 01:29 | reuse it later in my application.
| | 01:31 | I am going to select this text
and then choose refractoring menu,
| | 01:34 | Refactor!.. > Extract Method.
| | 01:37 | Now you can see a lot of things happening here.
| | 01:39 | There are some blue arrows pointing to my
code and some green arrows pointing to my code.
| | 01:42 | I am going to just hit the Enter key,
and then I get this red line that lets me
| | 01:47 | choose where I want to put the new method.
| | 01:49 | I think I want to have the new
method go down here on line 12.
| | 01:51 | I will press Enter, and again, you
see I get those blue bounding boxes.
| | 01:55 | This time I can type in my new name, "CalcArea".
| | 02:01 | Now I want you to notice one more thing:
on line 9 there is a blue triangle up there.
| | 02:06 | That is a marker.
| | 02:07 | It was dropped when I did the refactoring.
| | 02:09 | Now I can press the Escape key. Keep
an eye in line 9 when I press the Escape
| | 02:14 | key, and you'll see that
the cursor moves back there.
| | 02:16 | I am pressing it now.
| | 02:17 | It was also a little shrinking blue
circle that shows I will move to that point.
| | 02:22 | Next, I would like to refactor
this method over in the Book class.
| | 02:27 | I am going to switch to the Book class.
| | 02:29 | I am going to reorder the
parameters in this method.
| | 02:32 | I am going to invoke the Refactor
| | 02:34 | key on my keyboard, which is Ctrl+
Backtick character, and then I am going to
| | 02:39 | choose Reorder Parameters from
this list, and then press Enter.
| | 02:42 | Now again, you see there is a blue
bounding box, and this time there is an arrow
| | 02:46 | that is pointing to the right.
| | 02:48 | What I can do now is I can press the
right and left arrows on my keyboard to
| | 02:52 | move these parameters.
| | 02:54 | I am going to move this parameter over
to the next position, and then when I am
| | 02:58 | happy with this, I can press the Enter key.
| | 03:00 | Now of course, I can keep going back and forth.
| | 03:03 | Right now, I am happy
with it as a last parameter,
| | 03:05 | so I am going to press the Enter key,
And then the Refactoring tool takes me
| | 03:09 | over to my Main() method
and shows me that on line 12,
| | 03:13 | I made a call to that function,
and it needs to be rewritten.
| | 03:17 | If I approve this change, I click on
the check mark. Then it moves to line 15
| | 03:21 | and says, "Do you want to change the
code here," and I approve that one as well.
| | 03:26 | Up at the top of my Book class, I
have an autoimplemented property.
| | 03:31 | There is a refactor that lets me
turn this into an implemented property.
| | 03:34 | I'll choose Title and then Ctrl+Backtick.
| | 03:37 | I'll choose to Create Backing Store.
| | 03:39 | Notice the red mark there showing
it's going to replace that text.
| | 03:41 | I will press Enter, and you see it's
rewritten it to about six lines of code.
| | 03:46 | Later, if I want to switch it back, I
could choose this item and then switch it
| | 03:50 | back so it would look
like this property down here.
| | 03:53 | There are lots of other refactorings.
Let me show you just one more.
| | 03:56 | I am going to show you the Extract Interface.
| | 03:58 | I will click on Book, choosers Refactor!,
choose to extract the interface, and
| | 04:04 | you see that it pull the Interface out,
used the code down here to implement
| | 04:07 | the interface, wrote the properties
for me, Title, and made sure it says
| | 04:12 | Implements IBook.Title, and it has
also got the blue bounding markers, showing
| | 04:15 | that there is a link.
| | 04:16 | So if I want to change the name of the
interface to something like IWhatever,
| | 04:21 | it's changing all the linked locations.
| | 04:24 | So as you see, the Xpress tools take a
more dramatic approach to refactoring.
| | 04:28 | If you write C# code, then you'll be
happy to know that CodeRush has refactoring
| | 04:31 | for your language too.
| | Collapse this transcript |
|
|
6. Understanding the Project TypesWorking with project and item templates| 00:00 | When you create a project in Visual
Studio, it creates a lot of files for you.
| | 00:04 | The number of these files and the file
types are determined by a project template.
| | 00:09 | Simply stated, a project template
instructs Visual Studios which files to
| | 00:13 | generate and load into the IDE.
| | 00:16 | I'd like to start this movie by
looking at the existing templates.
| | 00:19 | I am inside Visual Studio, and I
am going to create a new project.
| | 00:22 | I will go to File > New > Project.
| | 00:26 | I am going to select the Console Application.
| | 00:31 | This is one of the templates that's available.
| | 00:33 | There are Visual Basic templates and C#
templates, C++ templates, and many more.
| | 00:40 | I am going to go to C# and then
Windows, and I am going to choose
| | 00:45 | Console Application.
| | 00:47 | Next, I have to pick the location on
my hard drive where to put these files.
| | 00:51 | Choose a location that makes
sense for your application.
| | 00:54 | For my application, I am going to browse out
to my Chapter 06 folder and then Chapter 01.
| | 01:02 | I will call this one
ConsoleApplication1. Accept the defaults.
| | 01:10 | Now what I want you to see is that the
template loaded a file called Program.cs,
| | 01:17 | and there is also another code file
that's hidden inside this Properties folder
| | 01:23 | called AssemblyInfo.cs.
| | 01:25 | So it created two files for me.
| | 01:27 | Now if I create a different kind of
application--let's say I add another new
| | 01:31 | project and choose WPF--
| | 01:37 | this template loads more files.
| | 01:40 | It loads something called
App.xaml and App.xaml.cs.
| | 01:46 | It also created two files that start
with the name MainWindow. And if I create a
| | 01:52 | web application, like this ASP.NET
web application--again, accepting the
| | 02:01 | defaults here--I get dozens of files.
| | 02:05 | I get a folder called Account, and one
called Styles, and a number of aspx files.
| | 02:12 | These are all coming from different templates.
| | 02:14 | So where are these templates stored?
| | 02:18 | Let's go and take a look.
| | 02:19 | I am going to switch over to Windows
Explorer, and I am going to go into my
| | 02:25 | Program Files folder.
| | 02:26 | On my machine, I am using the one
that's entitled x86. And then I am going to
| | 02:32 | scroll down and find Microsoft Visual
Studio 10 > a Common7 folder > IDE, and then
| | 02:43 | I need to scroll down and find the
Project Templates folder. And now I can
| | 02:48 | finally see the different template areas.
| | 02:50 | I am looking for a C# template, and I
am looking for the Windows template.
| | 02:58 | One more folder. The 1033 folder is
the English folder, and here is that
| | 03:04 | ConsoleApplication template.
| | 03:06 | It's stored in a ZIP file.
| | 03:07 | So if I open this ZIP file, I'll see
that there is a program.cs file and an
| | 03:13 | assembly.cs file, plus some other files.
| | 03:16 | So the instructions to Visual Studio
are to create all these separate files.
| | 03:20 | Now let's see what happens if I
double-click on this program.cs file.
| | 03:25 | It loads it inside Visual Studio.
| | 03:27 | On your machine, it may
not load into Visual Studio.
| | 03:29 | It might use a different tool to open the file.
| | 03:32 | It should use some text editor, though.
| | 03:34 | Now let's take a look between this
file and the program.cs that was created
| | 03:39 | when I ran the template.
| | 03:41 | Here I ended up with a
namespace called ConsoleApplication1.
| | 03:46 | In the template, there is
a token sitting in here.
| | 03:50 | It's replaced when the template is unwrapped.
| | 03:53 | This means to go figure out what the
name of the project is and put it here.
| | 03:57 | Also, note there is 'if'
statement up at the top that says, "If the
| | 04:00 | targetframeworkversion is greater
than or equal to 3.5, then add an
| | 04:05 | additional using statement."
| | 04:07 | So in my file, you can see that
System.Ling was added, because I created a 4.0
| | 04:13 | version of the ConsoleApplication.
| | 04:14 | Now the nice thing about templates is
that you can create your own templates.
| | 04:19 | I have some friends that have a
consultancy, and they create the same projects
| | 04:22 | over and over again for their clients.
| | 04:25 | If you were to find yourself in this
situation, you might consider making your
| | 04:28 | own custom project or item templates.
| | 04:31 | I am going to start by
creating a custom item template.
| | 04:34 | I am going to go over to my
ConsoleApplication. And I think before I do that I am
| | 04:38 | going to close all these windows.
| | 04:39 | So on Visual Studio, I can go to the
Windows menu, and then choose Close All Documents.
| | 04:44 | Then I am going to go to my
ConsoleApplication, and I am going to say Add > Class.
| | 04:51 | I am going to call this one BoilerPlateClass.
| | 04:59 | Next, I am going to put some of
the known template tokens in here.
| | 05:05 | I am going to create a comment that
says, "created on", and then I will use one
| | 05:11 | of the tokens, move down to the next line,
and then here I will put in another token.
| | 05:28 | This one is registeredorganization, and
then lastly, I am going to replace this
| | 05:36 | class name with the token itemname.
| | 05:45 | Let me save that file.
| | 05:47 | To make a template out of this, I am
going to go to File > Export Template.
| | 05:54 | I am going to create two templates.
| | 05:56 | The first one I am going to
create is an Item template.
| | 05:58 | So I'll select Item template, I
will click on Next, and I will pick my
| | 06:03 | BoilerPlateClass as the Item
template, and then click Next.
| | 06:09 | Here, I can add references.
| | 06:10 | When the user uses my template,
| | 06:12 | it's going to add these
DLLs in the references folder.
| | 06:15 | Let me pick those three.
| | 06:18 | Then I will click on Next.
| | 06:21 | Here I give my template a name.
| | 06:22 | I am going to call this one
BoilerPlateClass, and then I'll click Finish.
| | 06:37 | This file actually got
copied to two different places.
| | 06:40 | It got copied to the My
Exported Templates folder.
| | 06:43 | It also got copied to my Visual Studio
folder inside the My Documents folder.
| | 06:48 | Now, let's close the solution,
| | 06:54 | create a brand-new project--
| | 06:56 | I am going to use a ConsoleApplication--
and then I am going to right-click over
| | 07:02 | here and say, Add > New Item,
and there is my BoilerPlateClass.
| | 07:07 | So I will choose that one, click
Add, and as you can see, it replaced the
| | 07:14 | date and it replaced the class name.
| | 07:18 | Next, I want to create a project template.
| | 07:20 | So let me close this solution, and not
save the changes, and then I am going to
| | 07:27 | go and open an existing
project that I already have.
| | 07:29 | I am going to say Open > ProjectSolution.
| | 07:32 | I am going to open this project in the
InfoReader folder, called InfoReader.sln.
| | 07:43 | Next, I am going to make a template out of it.
| | 07:45 | You might recognize this.
| | 07:46 | This is the one I did in
Chapter 03, where I created the Info
| | 07:49 | Reader application.
| | 07:51 | It's got a database and a picture
and an xsdf file and a xaml file.
| | 07:57 | So I am going to make a template out of this.
| | 07:58 | I am going to go to File > Export
Template, make it a project template, call it
| | 08:05 | InfoReader or lynda.com or
whatever you want to call your template.
| | 08:10 | Click on Finish, and then in the future
I can come in and say I want to do File >
| | 08:16 | New > Project. Come over here and type "info".
| | 08:24 | There is my InfoReader project.
| | 08:25 | I click on OK, and there is my
InfoReader1, and you see how it automatically
| | 08:30 | brought in all of my files.
| | 08:33 | So as you can see, the Export feature
makes it dead simple to create your own
| | 08:36 | item an project templates.
| | Collapse this transcript |
| Creating a console application| 00:00 | Since the dawn of computer
programming, there have been
| | 00:03 | command-based applications.
| | 00:04 | The Windows operating system still
supports command-line applications, and
| | 00:08 | you won't be surprised to learn that Visual
Studio has a console application template.
| | 00:12 | The main reason I see for creating
console apps is when you have a utility
| | 00:15 | program that doesn't
need a fancy user interface.
| | 00:18 | I'm inside Visual Studio, and
I've opened up a solution called
| | 00:21 | CreateConsoleApplication,
which contains two sample apps.
| | 00:25 | I'm going to start by looking at the
standard Program.cs that came from the
| | 00:29 | Visual Studio console template.
| | 00:30 | I'm going to double-click on Program.cs.
| | 00:33 | This file has a static void Main, which is
the entry point for all .NET applications.
| | 00:38 | Now, console applications have standard
inputs, standard outputs, and standard errors.
| | 00:45 | If I want to do the standard output, I
use Console.Write or Console.WriteLine.
| | 00:53 | The main difference between the two is
that Console.WriteLine will output text
| | 00:57 | and then move to a new line.
| | 00:59 | The text that I want to print to the
Console is going to say, "Type a file name."
| | 01:09 | I want to run the application at this point.
| | 01:12 | To do that, in Visual Studio,
I can choose Debug > Start.
| | 01:15 | Now since I have a really short
application, it's not going to stay on the
| | 01:18 | screen for very long.
| | 01:20 | Watch what happens.
| | 01:24 | The screen shows up and
then immediately disappears.
| | 01:27 | To mitigate that, Visual Studio has
this menu called Start Without Debugging.
| | 01:31 | Now, my text shows up, and I'm given
an opportunity to watch the last results
| | 01:36 | before closing the window.
| | 01:37 | I'll press the Spacebar to close this window.
| | 01:41 | If I want to control that situation
myself, I can use Console.ReadLine, and
| | 01:49 | while I'm doing this, I might as
well use this to grab the file name.
| | 01:52 | So I'm going to create a
variable called fileName =.
| | 02:00 | There are some fun things
you can do with the console.
| | 02:02 | I'm going to copy this little bit of
text down here at the bottom of the screen.
| | 02:05 | I'll copy this and then paste it right up here.
| | 02:09 | I'm going to change the
ConsoleColor before I output the text.
| | 02:13 | Let me show you what that looks like.
| | 02:14 | Debug > Start Debugging.
| | 02:19 | As you can see, I now have
yellow text in my command window.
| | 02:22 | I've seen some interesting console
applications that use characters and
| | 02:24 | colors successfully.
| | 02:26 | Check out the Heroic Adventure application
in the exercise folder to see an example.
| | 02:30 | I'm now going to press any key.
| | 02:34 | Sometimes you don't want to output your
information to the standard console out.
| | 02:38 | You'd rather output it to
another area on your computer.
| | 02:41 | A common example here is
to output to a file stream.
| | 02:44 | So, I'm going to take this code down
here and copy it, and paste it right here--
| | 02:50 | Ctrl+V to paste--and then I'm going to
uncomment the code by pressing Ctrl+K,
| | 02:54 | Ctrl+U. Now, this little
bit of code I just wrote,
| | 03:02 | it says, "Get the current output," which is a
TextWriter, "and store it in this variable.
| | 03:07 | Now switch to a new StreamWriter based on
this file name that the user is going to type in."
| | 03:13 | Then I'm going to set the console
to use the StreamWriter to that file.
| | 03:20 | Now we'll no longer write to the
standard output, but we'll write into the file.
| | 03:24 | So I write a line of code
here, which goes into the file.
| | 03:27 | I flush the file so that the
information is stored in the file.
| | 03:30 | I switch back to the old output on this line
of code, and then I want to see the results.
| | 03:35 | So what I'm going to do on line
25 is I'm going to start a process.
| | 03:40 | .NET has a class called Process,
which I can use to launch any application.
| | 03:44 | Here I'm launching an app
based on the file name that I send.
| | 03:48 | So if I chose the file name
mydemo.xls, it would open up Excel.
| | 03:53 | Let me show you this application running.
| | 03:55 | Debug > Start Debugging.
| | 03:57 | I'm going to type a file name
Demo.txt, which is a text file.
| | 04:05 | So when it launches the process, it's
going to run in notepad. There is Notepad.
| | 04:10 | As you can see, my output successfully
ended up inside the file. Close Notepad.
| | 04:18 | Press the Enter key to close this window.
| | 04:21 | Next, let me switch over to this second project.
| | 04:23 | I'm going to set this as my Startup by
right-clicking and then choosing Set as StartUp.
| | 04:31 | Then I'm going to double-click on Program.cs.
| | 04:34 | Here's the standard pattern you see quite often.
| | 04:38 | You go out to your file system and you type
in "xcopy" and then a file name and a location.
| | 04:47 | This utility will take this file and
copy it to this secondary location.
| | 04:52 | So you see what I'm doing is I'm
passing two arguments into this application.
| | 04:57 | So if I want to mimic this in my
application, I can do that by having a String
| | 05:02 | array as a parameter on a static void Main.
| | 05:05 | So what I'm going to do in this example
is I'm going to check the argument array
| | 05:11 | up here to see if there's any data in it.
| | 05:14 | So I check if args.Length is greater than 0.
| | 05:18 | Then I know there's at
least one item in the array.
| | 05:19 | I then loop over to all the items
in the array and I check to see if it
| | 05:25 | starts with a -file name:.
| | 05:28 | If it does, then I'm going to split
off the backend of it, whatever text
| | 05:32 | comes after the colon.
| | 05:34 | That's what I'm doing here.
| | 05:35 | I'm splitting on this colon, and then
I'm grabbing the second part of this array
| | 05:40 | that was just generated here.
| | 05:42 | Arrays in C# always start at zero.
| | 05:44 | So this is second part of the array.
| | 05:46 | I'll then compile the application, and
then I would go out to the command prompt
| | 05:49 | and then run the application
and pass the parameters in.
| | 05:52 | For convenience sake, Visual Studio has
this nice little feature where I can go
| | 05:56 | to my Properties, double-click here,
go to Debug, and what I'm saying here is
| | 06:03 | whenever you run a Debug session, here
is the arguments I want to pass into the
| | 06:07 | application, which means I don't have
to go out to a command prompt to do this.
| | 06:11 | So you see I'm passing an -file name:
| | 06:14 | and then file.txt, and then I'm
passing in a second argument right here.
| | 06:18 | So now when I come up here and I choose
to Debug > Start Debugging, it's going
| | 06:24 | to pick up those parameters,
pass them into my program--
| | 06:27 | I will kill the application--
and then what did it do?
| | 06:36 | Then it used that file name to change
the standard output and write to that file.
| | 06:42 | You can see that file if you go over
here and choose Open Folder in Windows
| | 06:48 | Explorer, and go to the bin
folder and then go to the Debug folder,
| | 06:53 | there is the file that was created
for me, file.txt. So let me review.
| | 06:57 | The main reason you create a console
application is when you are creating a
| | 07:00 | utility that doesn't need a UI,
except for a standard console screen.
| | Collapse this transcript |
| Creating a class library| 00:00 | It is common nowadays to create a separate
DLL that contains shared logic for applications.
| | 00:05 | That way you have a centralized
assembly that contains the compiled code.
| | 00:09 | You also have one project where you
can write and debug that shared code.
| | 00:13 | In .NET, you create a class
library to accomplish this.
| | 00:16 | When you compile that class
library, it generates a DLL.
| | 00:20 | I'm inside Visual Studio,
and I've opened this Solution
| | 00:23 | called CreateClassLibrary.
| | 00:25 | There are two projects already in here.
| | 00:27 | There is a Visual Basic console
application, and there is a WPF application
| | 00:33 | that's written in C#.
| | 00:35 | Now I'm going to add a shared library.
| | 00:38 | I'm going to go to the solution, right-
click and choose to add a new project.
| | 00:43 | I'm then going to choose this Class
Library Template and then type in the name
| | 00:49 | TextLibrary for the Name, and then click OK.
| | 00:57 | Now, I'm going to compile the
application to show you what happens.
| | 01:01 | When I build the solution, it's going
to compile all three projects. And if I
| | 01:07 | right-click on the TextLibrary and
choose Open Folder in Windows Explorer,
| | 01:11 | you'll see that if I drill down into
the bin folder and then the Debug folder,
| | 01:17 | that it's creating this DLL.
| | 01:19 | Now, I'm going to come back
to this folder in a minute.
| | 01:22 | Let me switch to Visual Studio.
| | 01:25 | I'm going to generate a class called
LoremGenerator, which generates random
| | 01:29 | sentences of Latin words.
| | 01:31 | So, I think I should rename this Class file.
| | 01:33 | So I'll come over here to my project and
type in "LoremGenerator", and then press Enter.
| | 01:44 | Visual Studio then prompts me and asks
me if I would also like to rename the
| | 01:48 | class inside that file.
| | 01:50 | That seems like a great idea to me.
| | 01:51 | So I'm going to click Yes.
| | 01:52 | Now you see that it has
this new LoremGenerator class.
| | 01:56 | I'm not going to write all the code today live.
| | 01:59 | I do have the code up
here in this Assets folder.
| | 02:03 | There is a TXT file in here,
which I'm going to double-click on.
| | 02:06 | Then I'm going to copy all
of the code in this class.
| | 02:14 | Back to my LoremGenerator.cs file.
| | 02:17 | Then I'll replace these three lines of
code, Ctrl+V. I won't go through all of
| | 02:22 | the code, but let me just point out that
there are two methods I'm going to use:
| | 02:26 | the GetWords method where I pass the
numberOfWords in, and it gives me back say
| | 02:31 | eight words, and a GetSentence, which
has the same parameter, but this time of
| | 02:36 | course it's going to put a
period on the end of that sentence.
| | 02:38 | Now I'm ready to use this.
| | 02:40 | This is a C# library.
| | 02:42 | I'm going to go to my Visual Basic
Application and add a reference to this library.
| | 02:47 | I'm going to right-click, and I'm
going to choose to add a reference, and I'm
| | 02:53 | going to click the Browse button, and
then I'm going to go back to Windows
| | 02:57 | Explorer and copy this
path to that Debug folder.
| | 03:01 | Copy it, Ctrl+C. Switch back here, and
I'm going to paste it down here where
| | 03:06 | it says File name, and then press Enter.
And there is the DLL that I want to
| | 03:12 | have the reference to.
| | 03:13 | So I'll choose that one and then click OK.
| | 03:14 | Now, just for good measure, I'm going to
recompile my application and click Yes here.
| | 03:21 | So now when I compile this application,
it's telling it to make sure that I can
| | 03:27 | use the code that's
inside that DLL. Let's use it.
| | 03:30 | I'm going to double-click on Module1.vb,
and then I'm going to go here, and I'm
| | 03:35 | going to declare an instance of that
generator, Dim g As New TextLibrary.
| | 03:42 | There is my namespace. There is my class.
| | 03:45 | Now I'm going to write to the console,
and I'll also read from the console.
| | 04:02 | Next, I'm going to go onto line 6, and
I'm typing in "g.GetSentence", and let's
| | 04:15 | say I wanted eight words out of that.
| | 04:16 | Let's test this out. Debug > Start
Debugging, and there is my randomly generated
| | 04:23 | sentence, including the period on the end.
| | 04:27 | Next, I'm going to switch
over to the C# application--
| | 04:30 | that would be this one down here
WpfClient--and I'm going to open the
| | 04:35 | MainWindow.xaml.cs file.
| | 04:40 | Then inside my constructor, I will declare
the C# version of that variable, var g = new.
| | 04:49 | Well, this isn't going to work,
because I forgot one step.
| | 04:51 | I forgot to add the reference to the library.
| | 04:54 | See, I'm getting a red squiggle here.
| | 04:55 | So let me comment this code out and
then go over to my References folder and
| | 05:00 | show you an alternative
way of writing a reference.
| | 05:03 | Right-click > Add Reference.
| | 05:07 | Last time I browsed out and pointed to
a physical location, but Visual Studio
| | 05:11 | can simplify my life by letting me do
something called a project reference.
| | 05:15 | I click here and then I say whenever
you compile this TextLibrary, I would like
| | 05:19 | to know about it and get
a fresh copy of that DLL.
| | 05:22 | So, I click OK here, and then if you
scroll down and look at the Reference here,
| | 05:28 | you'll notice it says Copy Local equal True.
| | 05:31 | So not only will it update the DLL,
but it'll copy the DLL into the same
| | 05:35 | folder as my application.
| | 05:36 | So, when I deploy the app, I've got both files.
| | 05:40 | Now that I've added that Reference, I
can go back to this code that wasn't
| | 05:43 | working before, uncomment it,
and then go to this line.
| | 05:50 | There is the TextLibrary I was looking
for, and then period, and there is the
| | 05:54 | LoremGenerator, and then
finally, "var words = g.GetWords".
| | 06:02 | And there is my new function.
| | 06:03 | So, the takeaway from this is both
the Visual Basic application and the C#
| | 06:12 | application are sharing the code
that's inside that TextLibrary DLL.
| | 06:17 | As you can see, it is very easy to add
references to other libraries in Visual
| | 06:20 | Studio and that the Class Library
template provides a trouble-free way to make
| | 06:24 | that custom library.
| | Collapse this transcript |
| Creating a web site with ASP.NET| 00:00 | Programmers have been creating web
applications with Visual Studio for years.
| | 00:04 | When Visual Studio first debuted in 2002,
it included a new way of building web
| | 00:08 | sites with .NET-managed libraries.
| | 00:11 | For those of us who were building ASP
classic sites, this was a welcome change.
| | 00:15 | Now ten years later, we continue to
see phenomenal growth in web tools from
| | 00:19 | Microsoft and other vendors.
| | 00:22 | In this movie, I will show you how to
create some ASP.NET web sites. Just so
| | 00:26 | you know, however, this is a big topic--much
bigger than we can cover in a single movie.
| | 00:31 | But you can find other
ASP.NET titles here on lynda.com.
| | 00:35 | I am inside Visual Studio, and I am
going to create a new ASP.NET web site.
| | 00:40 | To do that, I go to the File
menu, and choose New > Web Site.
| | 00:46 | The default location for this
application will be in My Documents folder, inside
| | 00:51 | the Visual Studio web sites folder.
| | 00:53 | It may be a different location on your computer.
| | 00:56 | I want to make sure this is a C# application,
and then I will choose ASP.NET Web Site.
| | 01:00 | Now, creating this kind of web site
project in Visual Studio gives you a very
| | 01:07 | simple folder-based site.
| | 01:09 | Any files that you add to this folder
or its sub-folders are considered to
| | 01:12 | be part of this site.
| | 01:14 | You can see I have a Default.aspx
page and several other pages in here.
| | 01:20 | There is also something called a master page.
| | 01:22 | There are CSS documents,
menus, user management controls.
| | 01:28 | It also includes the jQuery JavaScript library.
| | 01:31 | All of these items are part of the
standard ASP.NET web site library.
| | 01:36 | To run this application, I can choose
to debug it, or I can go over to one
| | 01:41 | of these items--let's say this Default page--
and right-click and say View in Browser.
| | 01:45 | I can even select my browser by
choosing Browse With, and then it will show all
| | 01:50 | the registered browsers.
| | 01:52 | Now you may notice that in the lower
right-hand corner of my screen, a dialog is
| | 01:55 | popped up down here that
says ASP.NET Development Server.
| | 01:59 | This means that I don't have to
have a production-quality web server on
| | 02:03 | my developer machine.
| | 02:04 | I have this dedicated light-weight
developer-only Development Server available.
| | 02:10 | It's picked a local host and a
port on my machine for this web site.
| | 02:15 | I am going to browse with
Firefox and then click Browse.
| | 02:21 | Choose No to this dialog.
| | 02:24 | There is my sample page.
| | 02:25 | Notice I have got a couple
of links across the top here.
| | 02:28 | I can go to the About page or back to
the Homepage. And again, it's loaded it with
| | 02:35 | the correct port number up here in the browser.
| | 02:41 | There are several other types of
web applications you can build.
| | 02:45 | We can find more here in the
File menu, under New > Project.
| | 02:49 | Remember last time I chose Web Site?
| | 02:51 | This time I am going to
choose Project and then click Web.
| | 02:56 | Over here, you can see an
ASP.NET Web Application.
| | 03:00 | This is a more developer-
centric model of the web application.
| | 03:04 | It's going to compile some DLLs.
| | 03:06 | It's a little more sophisticated
than the one I just showed you.
| | 03:09 | There is also a number of
other types of ASP projects here.
| | 03:13 | Model View Controller is one
of the very popular new ones.
| | 03:16 | It's called ASP.NET Model View
Controller 2 Web Application.
| | 03:19 | I encourage you to look at some
of these other types down here.
| | 03:23 | I am going to look at just this one here:
| | 03:24 | ASP.NET Web Application.
| | 03:27 | This time it's going to look out on my
hard drive, to my Exercise File folder.
| | 03:32 | Again, on your machine, you can choose
wherever you like to put these files.
| | 03:36 | I am going to go ahead and click OK.
| | 03:37 | It doesn't look much different.
| | 03:42 | It's just what happens when you compile
the application. And now in order to
| | 03:45 | add an item to this project, you can no longer
just add it to the folder on your hard drive.
| | 03:49 | You will have to go to your Web Application,
right-click, and choose Add > New Item,
| | 03:56 | so that Visual Studio knows about it.
| | 03:59 | I thought I would edit a little bit of code.
| | 04:01 | I am going to click on the Designer,
and then I am going to go to this section
| | 04:05 | right here after this text and hit
Enter, which adds a new paragraph, and then I
| | 04:10 | can add controls by dragging and dropping.
| | 04:12 | I can also edit this in my HTML, if I'd like.
| | 04:15 | I am going to add a button, and
then I am going to add a hyperlink.
| | 04:18 | Now, this hyperlink is editable
by going to the Properties window.
| | 04:24 | For instance, I can change the text to
say something like "Go to About page", and
| | 04:34 | then for the location of where to
navigate to, I can click on this property, and
| | 04:38 | choose from this dropdown, and pick
any of the pages that are in my web site.
| | 04:42 | So I am going to tell it to
go to the About.aspx page.
| | 04:48 | Again, I can browse by
choosing right-click > Browse With.
| | 04:53 | This time I will just view in browser,
which I believe on this machine will
| | 04:57 | launch Internet Explorer.
| | 05:03 | Now, I'll click on the hyperlink,
and it moves me to the About page.
| | 05:07 | I can also put code, and I
can debug ASP.NET applications.
| | 05:11 | Let me show you how to do that.
| | 05:12 | I am going to close this browser.
| | 05:13 | I am going to go move to this button,
and I am going to double-click on it
| | 05:19 | which will stub in some C# code
for me. Double-click on the button.
| | 05:23 | It switches me over to the C# view of
the code behind page, and inside this
| | 05:28 | procedure here, I am going to
modify the text on that hyperlink.
| | 05:33 | The hyperlink control is called
Hyperlink1, and I am going to change the text to
| | 05:38 | say "lynda.com", and then I want
to change the navigate location.
| | 05:49 | That's called NavigateUrl.
And I think I am ready.
| | 06:04 | Since I want to debug the application,
I no longer need to go over here and
| | 06:09 | right-click and say View in Browser.
| | 06:11 | I need to go up here to
Debug and say Start Debugging.
| | 06:14 | That will attach the debugger to
the browser. So let me do that.
| | 06:20 | I will choose Debug > Start
with Debugging. Save my changes.
| | 06:25 | I have the Development Server launched.
| | 06:33 | When I click on the button, it
changes to text, and now when I click on
| | 06:36 | the hyperlink, we no longer go to
the About page, but we go to the
| | 06:40 | lynda.com web site.
| | 06:43 | I told you I could debug this,
| | 06:44 | so let me switch over to Visual Studio,
and I am going to put a breakpoint here
| | 06:48 | by clicking on this margin.
| | 06:50 | If you don't have the margin, you can press F9.
| | 06:52 | Now, there is a breakpoint there.
| | 06:55 | I will switch back to my browser, click
on the button, and notice what happens.
| | 07:01 | I switch over to Visual Studio, and
there is now yellow marker on the line of
| | 07:05 | code where I had the breakpoint.
| | 07:07 | Now, I can step through my code,
| | 07:11 | I can hover over these values to
see what's currently stored in there,
| | 07:14 | I can edit the values,
| | 07:16 | I can rewind my code, and re-run that
line of code again by dragging the yellow
| | 07:20 | arrow up to the previous line.
| | 07:23 | So as you can see, there are lot of ways
of working with web sites in Visual Studio.
| | 07:27 | The key point today is that Visual
Studio provides all the tools you need to
| | 07:31 | edit, build, debug, and deploy those web sites.
| | Collapse this transcript |
| Creating a rich internet application with Silverlight| 00:00 | Silverlight is one of the shining
jewels in the Microsoft toolkit.
| | 00:03 | If you are new to Silverlight, this
movie will give you a quick overview.
| | 00:07 | If you want to learn more about
Silverlight, there are a number of titles here
| | 00:10 | on lynda.com that provides more
details about this rich platform.
| | 00:13 | What is Silverlight?
| | 00:15 | It is Microsoft's framework for
building complex, visually rich, cross-browser
| | 00:20 | and cross-platform applications.
| | 00:23 | Developers can create web applications in
familiar languages, like Visual Basic and C#.
| | 00:28 | Unlike HTML-based application,
Silverlight has zero browser compatibility issues,
| | 00:34 | but it does require a browser plug-in
to be installed on the user's computer.
| | 00:38 | Silverlight is also one of the programming
platforms for the new Windows Phone 7 devices.
| | 00:43 | I am going to create a brand-new
Silverlight project inside Visual Studio.
| | 00:47 | To do that, I need to go to File >
New > Project and then choose the
| | 00:54 | Silverlight section.
| | 00:55 | If you want to do a Silverlight 4
project, make sure you have the Visual Studio
| | 00:59 | extensions installed.
| | 01:02 | Next, I'll choose Silverlight Application.
| | 01:05 | Make sure I'm in the
folder that I want to be in--
| | 01:07 | in this case, it's the movie folder in my
Exercise Files folder--and then click OK.
| | 01:12 | Visual Studio asks me if I
would like a companion web site.
| | 01:18 | I think that's a great idea,
| | 01:19 | so I am going to click OK.
| | 01:24 | If you look in the Solution Explorer,
you'll see that there are two projects now:
| | 01:28 | the SilverlightApplication
and an ASP.NET application.
| | 01:33 | What happens when I run the
application is it compiles the
| | 01:36 | Silverlight application,
| | 01:37 | copies the special files over to
the ASP.NET application, launches
| | 01:43 | ASP.NET, and then loads my
Silverlight control onto the page.
| | 01:48 | Before I run the application, let's put
a little bit of user interface in here.
| | 01:51 | I'm going to go to my toolbox and find
the Silverlight Button and drag that over
| | 01:56 | to the designer surface.
| | 01:58 | If I'd rather used the XAML editor, I can
use the bottom half of the split screen.
| | 02:03 | Here I can hand type my own controls.
| | 02:06 | For my case, I think I just
might change the text on the button.
| | 02:09 | So I am going to go to the Content
property, and type in Change Text.
| | 02:15 | Next, I will grab a TextBlock from the
toolbox and drag that over and drop it on
| | 02:23 | my designer surface.
| | 02:25 | Then I'm going to double-click on
this button to stub in some code.
| | 02:29 | Visual Studio wrote this little bit of
code for me. And then I'm going to write
| | 02:34 | one line of code here, textBlock1.Text = hello.
| | 02:45 | Now we are ready to test this application.
| | 02:47 | If you wanted to debug your Silverlight
application, you can choose to Debug > Start Debugging.
| | 02:53 | If you notice in the lower right-hand corner,
the ASP.NET Development Server has started up.
| | 03:02 | Now my ASP.NET page is loaded
and then embedded inside that page,
| | 03:07 | the entire working area of the
browser is now a Silverlight plug-in.
| | 03:12 | I can click on this Change Text button
and you see the hello string shows up.
| | 03:16 | Now this is a rather plain application.
| | 03:19 | I thought I would show you
some interesting applications.
| | 03:21 | I am going to show two more
applications in this movie.
| | 03:25 | I'll start by loading a
project from our Silverlight 4 title.
| | 03:28 | I'll go to File > Open > Project.
| | 03:32 | I am going to go to our movie chapter
and open this UsingBingMapControl, and
| | 03:41 | then I am going to load this SLN file.
| | 03:47 | Next, I am going to open the
Silverlight application and find this page
| | 03:52 | called ChangeMapView.
| | 03:54 | I also want to verify that this is
my starting page, so I am going to
| | 03:57 | double-click on this App.xaml.cs and
make sure that it says, in line 29, that I
| | 04:05 | want to load the ChangeMapView.
| | 04:10 | Then I'll switch over to my XAML.
| | 04:14 | What I am using in this example is a
plug-in control called the BingMapControl.
| | 04:19 | I have written a text block across
the top and three buttons and a couple
| | 04:22 | of text blocks across the bottom that show
our current latitude and longitude on the map.
| | 04:27 | When you click on this New York button,
I've written a little bit of code that
| | 04:31 | changes the map mode and then uses the
latitude and longitude position to change the map.
| | 04:37 | This number here represents
the zoom factor on the map.
| | 04:42 | When you click on the Grand
Canyon button, I do something similar.
| | 04:46 | I change to a slightly different map
mode, show the user the Grand Canyon with
| | 04:51 | a higher zoom level.
| | 04:54 | I also put a couple of
custom controls on the map.
| | 04:57 | These are called pushpins.
| | 04:58 | Not only do I put the pushpins on
the map, but I also set a tooltip
| | 05:02 | so when the user hovers over these
pushpins, they can read this text.
| | 05:06 | Let's see this in action.
| | 05:07 | Debug > Start Debugging and then click Yes.
| | 05:11 | That was the Save dialog, and
here is my Bing map control.
| | 05:20 | I can drag the map around and look at
the map, double-click to zoom in, and then
| | 05:26 | I can click on this New York button to
move me over to the New York coordinates.
| | 05:32 | Then I can click on the zoom in to Grand
Canyon button to move back to the Grand
| | 05:35 | Canyon, switch to the aerial view.
| | 05:38 | As I promised, here are the two pushpins, and
I can hover over those and see the tooltips.
| | 05:45 | The second example I want to
show you is on the Internet--
| | 05:49 | this is as an open source project--
| | 05:51 | I'm particularly interested in this
application because some of the pixel shader
| | 05:55 | tools that I wrote are
included in this application.
| | 05:58 | It's called Fantasia, and it's at this URL.
| | 06:02 | This is a Silverlight photo editing application.
| | 06:05 | So, I can draw on this application by
choosing different brushes, like this
| | 06:10 | Basic Furball brush.
| | 06:13 | I can tint the pictures.
| | 06:17 | I can use these pixel effects down here,
like the Emboss effect, or Invert Colors
| | 06:23 | effect and some of my favorites are
Swirls where I can drag the center point
| | 06:30 | around to any location.
See how fast and responsive this is?
| | 06:33 | I'll choose cancel on that one.
| | 06:35 | And let me show you one more
before I close: Drain Flush.
| | 06:38 | That gives me a nice swirling effect.
| | 06:40 | There is a lot more we
could talk about in Silverlight.
| | 06:49 | Silverlight supports auto browser
applications and a host of other
| | 06:52 | interesting features.
| | 06:53 | Be sure and check out our
other titles here on lynda.com.
| | Collapse this transcript |
| Creating a classic Windows application with Windows Forms| 00:00 | Forms have been a part of the
Microsoft programming lexicon since the first
| | 00:04 | version of Visual Basic appeared back in 1991.
| | 00:08 | Form is the term used to
designate an interactive window.
| | 00:11 | .NET is a replacement for those early
com-based systems, but Microsoft continues
| | 00:15 | the notion of form-based development.
| | 00:17 | This framework is called Windows Forms.
| | 00:20 | Windows Forms have been extremely
popular in corporate applications, forming the
| | 00:23 | basis of thousands of useful applications.
| | 00:26 | They are easy to develop, and
within Visual Studio support a drag-and-
| | 00:29 | drop interface designer.
| | 00:31 | Despite their popularity,
WinForm development is on the wane.
| | 00:36 | WinForm uses GDI as it's rendering
engine, which some consider inferior for
| | 00:41 | building modern interfaces.
| | 00:43 | Because of this, Microsoft created a
replacement technology for Windows Forms.
| | 00:47 | This new UI/API is called Windows
Presentation Foundation, and it is covered in
| | 00:53 | the next movie in this chapter.
| | 00:55 | Even though I'm a diehard WPF fan, I want to
show you some of the features of Windows Forms.
| | 01:00 | It is a powerful tool, and is still
used by many development shops as their
| | 01:04 | leading Windows client development framework.
| | 01:06 | I'm inside Visual Studio, and I've
opened a solution called CreateWindowsForms.
| | 01:12 | Currently, there are no
projects within this solution.
| | 01:15 | I am going to right-click on the
solution and choose Add > New Project.
| | 01:23 | For today, I'm going to create a
Visual Basic template because it has some
| | 01:29 | interesting Windows Forms templates to use.
| | 01:32 | So I am going to choose Visual Basic,
and then Windows Form Application, and
| | 01:37 | then I am going to go ahead
and leave at its default name of
| | 01:39 | WindowsApplication1 and click OK.
| | 01:44 | Here is my main screen of my application.
| | 01:47 | I would like to add a
couple of more Windows templates.
| | 01:50 | To do that, I'm going to right-click on my
WindowsAplication, choose Add > New Item.
| | 01:59 | If I click on the Windows Forms node
here, under Common Items, I can see about 20
| | 02:05 | different form templates.
| | 02:07 | I am going to use the Splash Screen, and I
am going to add one more: the Explorer Form.
| | 02:19 | As you can see, the Explorer
Form has a complex UI in place.
| | 02:26 | It has a menu, a toolbar, a
split screen and many other features.
| | 02:31 | That's all I want to show in that designer.
| | 02:35 | The Splash Screen is designed and pop
up when the application starts. To tell
| | 02:39 | Visual Studio to launch this
first and then my main form,
| | 02:42 | I need to go to My Project, double-click
here, scroll down to the bottom of this
| | 02:48 | Application section, and choose Splash
Screen. Choose your appropriate form.
| | 02:56 | While I'm in this dialog, I'll also
make sure that my start up form is correct.
| | 03:00 | I want to start with Form1 as my main screen.
| | 03:03 | Now, I'm ready to test. Debug > Start Debugging.
| | 03:09 | On my machine, it prompts me
if I want to save my changes.
| | 03:12 | There is the Splash Screen,
and there is our main UI.
| | 03:19 | I'll click the Close button to shut
down this form, and then I'll close
| | 03:23 | these two windows here:
| | 03:24 | the WindowsAplication
properties and the SplashScreen.
| | 03:27 | So, for the next couple of minutes, I'm going
to creating some user interface for this screen.
| | 03:33 | I am going to start by
dragging a few items to the designer.
| | 03:38 | First comes the menu strip.
| | 03:40 | So I am going to go to my Toolbox.
| | 03:42 | I am going to open this All Windows
Forms section and find the MenuStrip
| | 03:48 | control and drag that over.
| | 03:51 | Notice that Visual Studio
automatically docks that to the top.
| | 03:55 | I'll also find the
StatusStrip and drag that and drop it.
| | 03:59 | That gets docked along the bottom
edge of my screen. I need a ListBox.
| | 04:07 | Here it is. I'll drag that over and make it a
little bit wider and taller. And then the last
| | 04:14 | item I want to add is
something called the PropertyGrid.
| | 04:16 | Now, you've seen the
PropertyGrid inside Visual Studio.
| | 04:21 | It lives over here;
| | 04:22 | at least on a Windows Form Application, it does.
| | 04:26 | This control is also
usable in your applications.
| | 04:29 | So I am going to find it. Here it is.
| | 04:34 | I'll drag that over, and the next
step I want to take is to dock this
| | 04:41 | PropertyGrid to the right edge of the screen.
| | 04:43 | To do that, I'm going to go to the
Visual Studio property grid, scroll down to
| | 04:49 | the Dock property, select it,
and choose this dropdown.
| | 04:56 | This represents the edges of the
window where I can dock the control.
| | 05:00 | If I click on this button right here,
it means to dock to the right edge.
| | 05:07 | And I'll make it about 50% of the width of the screen.
| | 05:11 | Now, I am ready to write some code.
| | 05:13 | I need some menu items up here, so I am
going to click at the top of the screen.
| | 05:18 | I currently have no menu text.
| | 05:20 | There is a very simple way to add a lot
of menu items in a hurry in Visual Studio,
| | 05:25 | by going to this little special icon
over here, the black triangle, clicking
| | 05:30 | once and then choose Insert Standard Items.
| | 05:35 | See what happened?
| | 05:36 | It added a File menu, an Edit menu, and
the one I'm interested in--the Tools menu.
| | 05:43 | Now, I am going to add my own menu item right
here, so I just click once and start typing.
| | 05:53 | Double-click on this one, and it
switches me over to the Visual Studio editor,
| | 05:59 | and it has stubbed in this Visual Basic code.
| | 06:01 | Now, what I want to do is go
out to a folder in my hard drive.
| | 06:05 | I am going to go to my Desktop folder,
and I am going to search for text files,
| | 06:10 | files that end with a TXT extension.
| | 06:12 | Now, I have already created two
empty text files on my Desktop.
| | 06:17 | If you're following along, make sure
that you point your example to a folder
| | 06:21 | that contains some
files with a TXT extension.
| | 06:24 | I am going to start by
getting my desktop folder.
| | 06:28 | We'll declare a variable called location,
and then I'm going to go to the .NET
| | 06:34 | engine, and I'm going to query the
Environment, using this method called
| | 06:40 | GetFolderPath. And watch what happens
when I hit the open parenthesis--Visual
| | 06:43 | Basic suggests this enumerated value.
| | 06:48 | I am going to be looking in my Desktop folder.
| | 06:53 | So this is saying to .NET, "Somewhere
on my hard drive is a Desktop folder.
| | 06:59 | I don't know the exact location of it.
| | 07:01 | Would you look that up and find
the path of that and store that here?"
| | 07:04 | Now next, I want to write a link query
to talk to that file system and query
| | 07:10 | the data that's here.
| | 07:11 | Now rather than hand type that, I have
an Assets folder here that contains a
| | 07:15 | text local link code.
| | 07:17 | I am going to open up this text file,
and then I am going to copy this text and
| | 07:23 | paste it into this code window right
here. And I see I have this blue squiggle.
| | 07:30 | This happens occasionally.
| | 07:31 | It means I am missing an Import
statement at the top of my code window.
| | 07:34 | That should fix it.
| | 07:41 | So what this says is I'd like to
start a query and store the query in this
| | 07:45 | variable. Go out to the hard drive and
walk through all of the files in this
| | 07:50 | folder--remember that's my Desktop
folder--and then here is the query part.
| | 07:55 | Do a Where clause on that and say I'm
only interested in files if they have a
| | 08:00 | TXT extension on them.
| | 08:02 | Once you've done that query, I want you
to select out the information and store it
| | 08:07 | in this data type called the FileInfo.
| | 08:09 | Now FileInfo contains information like
when the file was last saved and what the
| | 08:13 | file name is and things like that.
| | 08:15 | So I am going to select that out.
| | 08:18 | On line 12, I am going to execute the
query by calling ToList, which returns the
| | 08:21 | results of the query as a list.
| | 08:23 | I am going to assign them to the list
box as data source, and then I am going to
| | 08:27 | tell the list box which property on
the FileInfo class I'd like to show.
| | 08:31 | I want to show the Name of the file.
| | 08:34 | The next piece of
information comes from the ListBox.
| | 08:39 | As the items are loaded in the ListBox
I am going to click on them, and then
| | 08:43 | I want to take the data, the details about
those files, and load them into this PropertyGrid.
| | 08:47 | So I need to write an event
procedure for the ListBox.
| | 08:50 | So I'll switch back to my
designer, I clicked in the ListBox.
| | 08:53 | I am going to go over to this property
grid and click on the lightning bolt, and
| | 08:58 | then I am going to find this
property called SelectedIndexChanged.
| | 09:01 | That's an event that fires whenever the
user selects a new item in the ListBox.
| | 09:05 | I'll double-click right here on the edge,
right in this column here, and Visual
| | 09:10 | Studio will stub in this code on my behalf.
| | 09:15 | If I go to this text file up here,
SelectedIndexChanged.txt file, I've got the
| | 09:19 | code I am going to write in there.
| | 09:22 | So I'll copy that over
and then paste it in here.
| | 09:29 | What this code says is, "Make sure that the
user has selected something in the ListBox.
| | 09:35 | If they've selected something, then
wherever it is they selected assign it
| | 09:39 | to the PropertyGrid.
| | 09:40 | The PropertyGrid then interrogates
that object and shows me all the details
| | 09:44 | about whatever that type is."
| | 09:47 | I think I am ready to run the
application. Debug > Start Debugging.
| | 09:57 | Click Tools > Show Files.
| | 10:01 | It found two files on my desktop.
And hen I click on the file, it loads the
| | 10:06 | information into the
PropertyGrid about that item.
| | 10:09 | For instance, you can see the file
name is abc.txt, and the last time it was
| | 10:16 | accessed was at 2:15 this afternoon.
| | 10:18 | There is lot more I can show you with
Windows Forms, but that's about all the
| | 10:21 | time I have in this movie.
| | 10:23 | Remember this: Windows Forms are
still a viable technology for developing
| | 10:27 | Windows applications, and they are
common in many corporate environments.
| | Collapse this transcript |
| Creating a dramatic Windows application with Windows Presentation Foundation (WPF)| 00:00 | Windows Presentation Foundation,
| | 00:02 | more commonly called WPF, provides a rich
model for building modern user interfaces.
| | 00:07 | It contains incredible data binding tools,
a superb animation toolset, and is the
| | 00:12 | best Microsoft technology for
building desktop applications.
| | 00:16 | As you suspect, Visual Studio contains a
project template for creating WPF applications.
| | 00:21 | I'm going to go to Visual Studio's
File menu and choose New > Project.
| | 00:28 | Then I'm going to click
on the language of choice.
| | 00:31 | Today, I'll choose Visual C#,
and then I will click on Windows.
| | 00:36 | There are several templates inside this folder.
| | 00:39 | First is the standard WPF
Application, second is a specialized type of
| | 00:46 | application called a WPF Browser App--
sometimes called an XBAP--that runs
| | 00:51 | within your browser.
| | 00:53 | Then lastly, there are two
Custom Libraries down here:
| | 00:56 | WPF Custom Control Library
and WPF User Control Library.
| | 01:01 | For today, I'm going to choose WPF
Application, and then place that in my Movie folder.
| | 01:07 | I am going to click OK and
wait for about two seconds.
| | 01:12 | Visual Studio opens up the designer surface.
| | 01:16 | It has a split screen.
| | 01:18 | On the top is the area where I can drag
and drop my controls. On the bottom half
| | 01:23 | is the area where I can write my XAML.
| | 01:26 | There is a Zoom to Fit button here, where I can
shrink down the screen size to see everything.
| | 01:32 | Then of course, there is a toolbox to the side.
| | 01:35 | I don't need this bottom window opened,
so I am going to close this document
| | 01:38 | outline, so I can see more of my toolbox.
| | 01:40 | Naturally, I can take one of these controls--
| | 01:43 | Let's say this button--and drag
it over to my designer surface.
| | 01:47 | If you look down in the bottom half of
the screen, you'll see that it wrote this
| | 01:52 | Markup Language for me.
| | 01:54 | I can edit the XAML directly.
| | 01:55 | For instance, I can change the content
of the button by typing in a string here.
| | 02:01 | And I can edit it in either place.
| | 02:04 | I can resize the button, drag to a new
location, place other elements on the
| | 02:10 | screen--for instance a ListBox.
| | 02:12 | When I drag the list box around, I get
snap grid showing me I have the two edges
| | 02:19 | of these two elements aligned.
| | 02:21 | Now I can size this list box a little bit
bigger to make it align with the other side.
| | 02:27 | There are helper numbers here showing me
the size of this control as I resize it.
| | 02:32 | I can make this thing look much nicer
by adding gradients and transparencies.
| | 02:37 | Let me show you on the
background of this window.
| | 02:40 | I'm going to click on the window
itself or this grid--one or the other.
| | 02:44 | I'm going to go to a property called Background.
| | 02:50 | It's called Background and not back color
because you can apply any valid WPF brush.
| | 02:57 | I could paint this with solid colors,
with gradient colors, with images,
| | 03:01 | and many other items.
| | 03:03 | I am going to click on this little
dropdown here, and choose the gradient brush.
| | 03:09 | Notice there's also a solid
color brush and an image brush.
| | 03:13 | I am going to click on the gradient brush.
| | 03:16 | Then at the bottom of the screen,
you'll see that I have two gradient colors.
| | 03:21 | I'm going to flip the gradient
by clicking on this button here.
| | 03:25 | Then I'm going to change the color value of
this black portion of the window to a dark blue--
| | 03:35 | something along those lines--and
maybe make it a little less saturated.
| | 03:40 | So I'll move this mouse
pointer over here to the left edge.
| | 03:45 | It's more gray on the side of the window.
| | 03:48 | I can control how far down the
screen the gradient goes by dragging
| | 03:51 | this gradient stop.
| | 03:52 | I think that looks nice.
| | 03:55 | I can click on the button, and I can
apply an opacity value, so that I could see
| | 04:00 | partially through this button.
| | 04:02 | There's an Opacity property
down here in the O section.
| | 04:05 | I am going to set that to 0.5.
| | 04:12 | Now, you can see that if I bring that
to front, it's covering up the list box,
| | 04:21 | but I can see a portion of
the list box through the button.
| | 04:24 | This is very hard to do with the
other desktop application technology that
| | 04:28 | Microsoft has, which is called Windows Forms.
| | 04:30 | Now, this is all I am going to show
you of the WPF template in this movie.
| | 04:34 | The reason is I have an entire chapter
devoted to this topic, and I encourage
| | 04:38 | you to spend some time watching those movies.
| | Collapse this transcript |
| Creating a WCF service| 00:00 | There is often a requirement when
building an application to communicate with
| | 00:04 | an external service.
| | 00:05 | In today's programming environment,
that service often lives in the cloud.
| | 00:10 | Programmers have been working for
years to come up with the best way of
| | 00:12 | communicating with a remote service.
| | 00:15 | Microsoft has several ideas here too.
| | 00:17 | For example, they have web
services, remoting, and a multitude of
| | 00:22 | networking protocols.
| | 00:23 | A few years back, Microsoft decided to
create a unified architecture for working
| | 00:28 | with these disparate technologies.
| | 00:30 | The outcome of that work is called Windows
Communication Foundation, also known as WCF.
| | 00:36 | The key point about WCF is
that it works in a flexible way.
| | 00:40 | You create a service that
provides data or functions.
| | 00:44 | Through a configuration file, you
specify which technologies this service will
| | 00:48 | use for the communication channel,
and how the data will be transmitted.
| | 00:53 | By changing the configuration files on
the client and the server, you can easily
| | 00:57 | change to another channel.
| | 00:59 | I am inside Visual Studio.
| | 01:00 | It's important that you run Visual
Studio as an administrator if you want
| | 01:05 | to test the project.
| | 01:06 | I have opened a solution
called ProvideWcfServices.
| | 01:10 | This solution already contains a
service project called BookServicesV2.
| | 01:15 | This is the finished version of this service.
| | 01:17 | I am going to show you how to use the
WCF project template to create a project.
| | 01:21 | I am going to right-click on my
solution and choose Add > New Project.
| | 01:27 | I am going to choose my language of choice--
| | 01:30 | today it's going to be Visual C#--and
then I am going to click on the WCF folder.
| | 01:35 | Do you see that I have four templates in here?
| | 01:38 | The one I am going to work with
today is the WCF Service Library.
| | 01:42 | I am going to call this one
BookServicesV1, and then I am going to click OK.
| | 01:52 | This should go in the same
folder as the existing solution.
| | 02:00 | I see I have a typo.
| | 02:02 | I said BookServices, but I typed BookService.
| | 02:05 | Let me fix that and then click OK.
| | 02:11 | Here is my service.
| | 02:12 | Now remember what I am doing here.
| | 02:13 | I am writing some code that's going to
do some work for my clients, or going to
| | 02:18 | provide some data for them.
| | 02:20 | WCF expects you to write an interface,
and they have already stubbed in one
| | 02:24 | interface for you called IService.
| | 02:25 | Then you're supposed to create a
concrete implementation of that service
| | 02:30 | by creating a class.
| | 02:31 | So there is also a class
here called BookServices.
| | 02:34 | There is also a class here called Service1.
| | 02:38 | These are not good names for my services.
| | 02:40 | I am thinking I am going to be
working with a library of books,
| | 02:43 | so I might want to have it called
BookService and IBook and names along those lines.
| | 02:49 | So what I can do is refactor these
by clicking on the IService name here,
| | 02:55 | changing it in the file to IBook.
| | 03:01 | Visual Studio notices that I am
changing the file and is suggesting that I also
| | 03:04 | rename the references in code.
| | 03:07 | I am going to say Yes.
| | 03:09 | Can you see what happened here on line 11?
| | 03:11 | Now it says IBook there.
| | 03:13 | Then I need to rename this
service, the concrete class.
| | 03:17 | I am going to call this one Book,
and it will go through the same process:
| | 03:22 | "Would you like to rename this?"
| | 03:24 | I am going to say yes.
| | 03:26 | Next, I need to go into my interface
and provide the operational contracts.
| | 03:31 | I am going to specify what is okay to
expose to the network by adding these
| | 03:37 | attributes in my code.
| | 03:38 | For instance, you can see I have
got this class down here that is a
| | 03:42 | DataContract, and I've got an interface
up here that is a ServiceContract, and
| | 03:47 | I've got a method here that has an
OperationContract on it. That's saying that
| | 03:51 | this operation or this function
is exposable from the web service.
| | 03:56 | In order to make the application work,
I need to set a configuration.
| | 04:01 | Both the service that's running on a
server somewhere and the client need to
| | 04:04 | have a configuration file saying
how they plan to communicate.
| | 04:07 | To make it easy to create a configuration file,
| | 04:10 | you can right-click on this
App.config and choose Edit WCF Configuration.
| | 04:16 | Now the details of how to set this up
are beyond the scope of this introduction
| | 04:23 | to web services. Just let
me point out this one piece.
| | 04:27 | Here is this service that I am
exposing, BookServicesV1.Book, and underneath
| | 04:32 | this, I can see the Host.
| | 04:33 | I am not sure if this is going
to work correctly on your machine.
| | 04:37 | On my machine, I have localhost set up,
and I have this hardcoded port, 8732.
| | 04:44 | I believe if you open this application on
your machine, it should use the same port.
| | 04:48 | If not, you'll need to investigate
which port is running on your server and
| | 04:52 | change this number to match your machine.
| | 04:55 | Rather than setting up this
configuration, I already have a second version of
| | 04:58 | this application that's set up correctly.
| | 05:00 | So I am going to close this dialog, and
I am going to show you BookServicesV2.
| | 05:10 | Inside BookServicesV2, I have a
class called BookData, which has a BookId,
| | 05:16 | Title, Author, and Price.
| | 05:18 | Inside my BookService, which you
remember is my Concrete class,
| | 05:23 | I have a private field that
contains a list of BookData.
| | 05:30 | I have a constructor that fills that
list full of instances of BookData and
| | 05:35 | then down in this section, I have added some
methods: AddBook, UpdatePrices, and GetBooks.
| | 05:44 | You might remember that I said, in my
IBook services, I need to set up my contract.
| | 05:49 | So if I look over here, you'll see that
I have a contract that says I have an
| | 05:53 | Operational contract for the AddBook
method, the Update books, and the GetBooks.
| | 05:59 | So I've got those all set up correctly.
| | 06:01 | Next, I go to my configuration file,
set that up, and then lastly, I am
| | 06:06 | ready to test this out.
| | 06:07 | So I am going to start the application.
| | 06:15 | Notice down here in the lower right-hand
corner, it says that my service has been hosted--
| | 06:20 | that means it's running on the
server--and click here to view
| | 06:23 | detailed information.
| | 06:28 | That's the one I wanted to see.
| | 06:29 | I actually have two services running right now.
| | 06:32 | I have the new service I just added and
the existing service, and it took them a
| | 06:37 | little while for the second service to show up.
| | 06:39 | This is the one I was showing you
earlier today, and here is the one,
| | 06:42 | the finished version.
| | 06:43 | I am going to close these dialogs
down, and I am going to unload this
| | 06:50 | BookService, so it's not too confusing.
| | 06:52 | I am going to right-click on
it and choose Unload Project.
| | 06:57 | Now, when I run the application, it
won't compile or run that service.
| | 07:01 | It shows my service has started.
| | 07:12 | This is the end point.
| | 07:14 | If I have a client out there somewhere
that wants to call this service, this is
| | 07:17 | the address that you are going to go
to. And because I don't have a client,
| | 07:26 | Visual Studio creates this testing
harness called the WCF Test Client.
| | 07:31 | This shows that my service exposes three
items through the operational contract:
| | 07:35 | AddBook, UpdatePrices, and GetBooks.
| | 07:39 | So I can double-click on this
GetBooks functions, and then come over here
| | 07:43 | and click on Invoke.
| | 07:45 | So I am now taking the role of a
client who is going to call that service.
| | 07:49 | I am getting warned that I am doing
something where the information is going to
| | 07:53 | go over the Internet, and people could
see what's in that call to the server.
| | 07:56 | I am going to go ahead
and say OK to this message.
| | 08:00 | It's contacting the server and then
down here in the bottom, it shows it
| | 08:03 | returned a list of three items.
| | 08:05 | There is a book with an Author called
Walt Ritscher, and another book down here
| | 08:09 | with Sam Pearson, and down here is one for Sarah.
| | 08:12 | All right, I can choose this
UpdatePrices, enter the new increased amount, 1.2
| | 08:22 | and click on Invoke.
| | 08:24 | That method is going to go to the
server and change the prices on the book.
| | 08:28 | So now if I call GetBooks again, and
invoke it, I should see some new prices.
| | 08:35 | I would like to remind you that
you have to run Visual Studio as an
| | 08:38 | administrator if you want to invoke this
test client, like I am doing here, or if
| | 08:43 | you want to try out the
service. It's review time.
| | 08:47 | What have I done in this movie?
| | 08:48 | I created a service, and I
configured it to run on my local server.
| | 08:52 | I exposed a method on the
service through a data contract.
| | 08:55 | In the next movie, I am going to
show you how to use this service from a
| | 08:59 | client-side application.
| | Collapse this transcript |
| Using an existing WCF service| 00:00 | This movie shows how to use an existing
WCF service from a client application.
| | 00:05 | For debugging purposes, I will keep the
service project and the client project
| | 00:09 | in the same solution.
| | 00:11 | Remember, it's important that you run
Visual Studio as an administrator if you
| | 00:15 | want to test the service.
| | 00:16 | I am inside Visual Studio, and I've
opened a solution called UseWcfServices.
| | 00:22 | If you look at the top of my Visual
Studio, you'll see the word "Administrator",
| | 00:25 | which means I am running in Administrator mode.
| | 00:28 | There is a BookService available for
my consumption inside this project.
| | 00:32 | This is similar to the one in the other
movie in this chapter. And I have this
| | 00:37 | WPF application, which is going
to serve as my user interface.
| | 00:40 | Let's review the user interface.
| | 00:42 | I am going to come down here and
look at MainWindow.xaml. Double-click.
| | 00:46 | There is a list box on the left side of
the screen that I wish to populate with
| | 00:53 | the names of the books that
are available from the service.
| | 00:56 | Then when I click on this Button, I
want to add a new book to the collection.
| | 01:00 | The first thing I need to do is tell
Visual Studio where to find my service.
| | 01:05 | I do that by going to my project
and choosing Add Service Reference.
| | 01:12 | Next, I need to provide the
location where the address lives.
| | 01:15 | In my case, it's living on the
same server as my client application.
| | 01:20 | It might also be out there
on the Internet somewhere.
| | 01:22 | I can type a URL here. Or since I
know that my service lives on the same
| | 01:26 | computer, I can click Discover, and
wait for it to define the service.
| | 01:30 | Then when I click on this expand node,
it's going to launch the service--
| | 01:35 | notice down here it says WcfSvcHost--
and then it shows here is the service,
| | 01:41 | there is my interface, and there
are the tree operation contracts.
| | 01:45 | There is a method called AddBook, a
method called GetBooks, and a method
| | 01:49 | called UpdatePrices.
| | 01:50 | I want to be sure that I can call this
service asynchronously, so I am going to
| | 01:55 | click on the Advanced button and
then check this Generate asynchronous
| | 02:00 | operations check box before I continue.
| | 02:05 | I also need to give my Namespace a decent name.
| | 02:09 | I am going to call this one BookService.
| | 02:12 | Everything looks okay, so I will click OK.
| | 02:18 | At this point, I can call a service
as if it were living in a local DLL.
| | 02:22 | So I am going to switch over to my Code
view by clicking F7, and I am going to
| | 02:28 | instantiate an instance
of my service right here.
| | 02:45 | Then inside my Windows Loaded event,
I am going to grab the data from
| | 02:49 | the GetBooks method.
| | 03:02 | Here it is, GetBooks,
open and close parenthesis.
| | 03:05 | Then I need to tell the list box
which property to show in the list.
| | 03:09 | So I'll do that here by saying
DisplayMemberPath= and the name of field or the
| | 03:17 | name of the property.
| | 03:18 | In my case, it's called Title.
| | 03:20 | I am making this call on the same
thread that's running inside this procedure,
| | 03:24 | which in my case is the UI thread.
| | 03:26 | So what's going to happen is when the
application starts up, it's going to go
| | 03:29 | out and call the service. And since I
am doing it on the UI thread, it's going
| | 03:33 | to block and wait until the service
returns before it runs the next line of
| | 03:36 | code, which will cause a
small delay at my start up time.
| | 03:39 | I am going to press F5 to test my application.
| | 03:48 | The service host starts up.
| | 03:50 | Notice that I have no UI for about two
seconds, and then once the service has
| | 03:55 | returned, this list box now contains the data.
| | 03:58 | It would be better if I didn't make
this call on the UI thread, but to make
| | 04:02 | it on a worker thread.
| | 04:03 | So when I click on this button to add a
book to the collection, I'll do that on
| | 04:07 | a background thread.
| | 04:09 | I'll shut this application down.
Return back to my code.
| | 04:13 | On this line of code, I am going to
declare a variable of my BookData type, and
| | 04:35 | then my last one, I am going to
choose a title, since that's what we were
| | 04:38 | looking for in the list box.
Say Title=. Next, I want to add the book,
| | 04:52 | so I am going to call my service.
| | 04:57 | You might remember there was a
method called AddBook. Because I did the
| | 05:01 | asynchronous service, you see that I
have an AddBook method, an AddBookAsync,
| | 05:06 | and I also have this event that fires
when that method returns, AddBookCompleted.
| | 05:12 | So I am going to start by
setting up my event procedure.
| | 05:14 | I'll choose this item and choose a plus, equal.
| | 05:17 | Visual Studio is offering
to stub some code in for me.
| | 05:20 | I am going to press Tab once to
complete that line of code, and then I am going
| | 05:24 | to press Tab a second
time to stub in the method.
| | 05:27 | Now I am going to invoke the service,
AddBookAsync, passing in the instances
| | 05:38 | of my book as a property, and then I
am going to go down here and update my
| | 05:42 | list box when it's done.
| | 05:51 | This is the same code I had up here,
ItemsSource = _service, I gave this.
| | 05:54 | I think it might be faster if I just
copied this and pasted that in down here.
| | 05:59 | If I did everything right, this
service should add a book. Let's try it out.
| | 06:04 | I am going to press F5, and I
will click on the book one time.
| | 06:11 | There is our new book added. And just for
good measure, I'll click on the book a
| | 06:15 | second time to verify that it's still working.
| | 06:17 | As you can see, it is fairly simple
to add a reference to the service.
| | 06:21 | Before I end this movie, I want to
point out that there are other layers built
| | 06:24 | on top of WCF, including WCF RIA
Services and WCF Data Services.
| | 06:31 | Both are beyond the scope of this course,
but I encourage you to check them out
| | 06:35 | as you become more familiar with .NET.
| | Collapse this transcript |
| Navigation UI designs with the Document Outline view| 00:00 | Navigating through a complex tree of
controls in a real world application can be
| | 00:04 | a frustrating ordeal.
| | 00:06 | If you have a design that has hundreds
of nested controls, it can be a lot
| | 00:09 | of work just to find the
control and select it in the designer.
| | 00:13 | To aid in this kind of navigation, Visual
Studio includes a Document Outline window.
| | 00:18 | I am inside a project inside
Solution Explorer called UsingDocOutline.
| | 00:25 | It contains three projects:
| | 00:27 | an ASP.NET application, a
Windows Form application, and a
| | 00:32 | WPF application.
| | 00:33 | In order to navigate through this
HTML page over here in the ASP.NET
| | 00:38 | application, I need to have my
Document Outline window opened.
| | 00:43 | It may not be showing up on your machine.
| | 00:45 | In order to make it visible, you
need to choose View > Other Windows.
| | 00:52 | Then on this sub-menu, choose Document Outline.
| | 00:57 | This window usually lives on
the left edge of the screen.
| | 01:00 | As you can see in my computer,
it's over here, and it's docked at the
| | 01:03 | bottom half of the screen.
| | 01:05 | I have selected the body tag inside my
HTML document, and you can see that it's
| | 01:10 | selected over here in the Document Outline.
| | 01:13 | Naturally, I can navigate to the HTML
element by clicking on it here inside
| | 01:17 | the Document Outline. And notice
that it selects the entire set of
| | 01:21 | sub-elements by doing that.
| | 01:23 | As you might guess, clicking on title
will only select that one child element.
| | 01:27 | There is also a nice little
Navigation bar down here in the bottom in HTML
| | 01:32 | documents where I can see HTML node as well.
| | 01:35 | That's not part of the Document Outline,
but it's another handy navigation tool.
| | 01:40 | Next, I'm going to switch over to the
Windows Forms application and open this Form1.cs.
| | 01:46 | I probably need to make my window a little
bit wider over here to see what's going on.
| | 01:52 | In this UI, I've got the Tab Control that
contains some group boxes and within the
| | 01:59 | group boxes are some
radio buttons and checkboxes.
| | 02:03 | So of course, I can select this radio
button in the group box and see what
| | 02:06 | happens in the Document Outline?
| | 02:08 | It shows up as selected over here.
| | 02:11 | I can then click on this check box here,
and it will automatically select it in
| | 02:15 | the second group box.
| | 02:16 | This is what I mean by when
you have a complex nested UI.
| | 02:19 | Sometimes you can't even get your
mouse to the point where you can click on
| | 02:22 | something in the designer. But with this
Document Outline, I can select it and then see it.
| | 02:28 | I can also move items
from one element to another.
| | 02:31 | For instance, I can take this check box and
drag it and drop it inside this group box.
| | 02:37 | Now, it has become a child of that group box.
| | 02:42 | In WPF, I can do the same thing.
| | 02:44 | Here, I'm in the WPF application.
| | 02:47 | I am going to open the MainWindow.xaml file.
| | 02:52 | Again, this is a complex UI here.
| | 02:55 | WPF has added one extra twist.
| | 02:57 | When you hover over the item in the
Document Outline, you get a Preview
| | 03:01 | window that pops up that's showing
all the children elements of this grid
| | 03:05 | and what they'll look like.
| | 03:06 | It's like a miniature thumbnail.
And as I drill down into my object model,
| | 03:11 | let's say I click on this TreeView here
or hover over it, you see that I'm seeing
| | 03:15 | a subset of that UI.
| | 03:16 | Now, I'm only seeing the
TreeView. I'll click here.
| | 03:20 | It gets selected in my designer
and selected in my XAML view.
| | 03:25 | Also, if I select the items in the
designer, you see that they are selected over
| | 03:31 | here in the Document Outline.
| | 03:32 | So as you can see, the Document
Outline window is a simple concept, but it
| | 03:36 | provides a handy way to navigate
complex UI in many different project types.
| | Collapse this transcript |
|
|
7. Digging Into Your DataCreating a data project with SQL Project| 00:00 | Here is a problem that
torments developer teams everywhere.
| | 00:03 | If you've ever worked with database
changes and multiple developers, you know
| | 00:07 | that keeping your database
schema in sync is a never-ending task.
| | 00:12 | If someone makes a change to the
database schema, you must ensure that that
| | 00:15 | change propagates to each developer's
computer, makes it to the test team, and
| | 00:20 | gets updated on all your production servers.
| | 00:23 | The trouble is compounded when
debugging previous versions of your product.
| | 00:28 | Now you have to ensure that the
database matches the code that is stored in
| | 00:32 | the source control.
| | 00:33 | Some teams attempt to solve this
problem by having a folder full of SQL scripts
| | 00:38 | that contain the
information to update the schema.
| | 00:40 | These scripts can quickly get out of
sync with the real database schema, however.
| | 00:44 | Believe me, I know this
from first-hand experience.
| | 00:48 | To solve these problems, Microsoft created a
special database project known as SQL project.
| | 00:53 | Before I continue, I should add that
these features are only available in Visual
| | 00:56 | Studio Premium edition or Ultimate edition.
| | 01:00 | If you do not have either of these
editions, you will not be able to follow
| | 01:03 | along with my demonstrations.
| | 01:05 | I'm inside Visual Studio, and I've
opened a solution called SqlProjects.
| | 01:11 | This has a project called UseExisting
that I'm going to show you in about two
| | 01:14 | minutes. But first, I want to show you
how to create a brand-new SQL project.
| | 01:18 | I'm going to right-click on this
solution and choose Add > New Project.
| | 01:25 | The projects I'm looking for are down
here near the bottom of the Installed
| | 01:29 | Templates section, in the
Database node, underneath SQL Server.
| | 01:34 | As you can see, there are
several types of projects here.
| | 01:37 | The one that I want to use today is
called the SQL Server 2008 Wizard.
| | 01:43 | I'm going to come down here and have it
generate a new BookSeller database, also
| | 01:52 | in V1, like that, and then click OK.
| | 01:57 | Now the wizard will launch and I get the
Welcome screen, which I'm going to skip.
| | 02:03 | The next type is what kind of
project do I want to create?
| | 02:06 | I'll create a user-defined database
project, and I'm going to choose to store
| | 02:10 | the script files by type of object.
| | 02:12 | Then I'm going to click on Next.
| | 02:16 | These are the standard
settings for the SQL Server database,
| | 02:19 | so I'm going to leave them as is.
| | 02:22 | Then I'll click on Next.
| | 02:24 | At this point, I'm
creating a brand-new databases,
| | 02:28 | so I would be responsible for
creating all of the scripts for creating the
| | 02:31 | tabled, the start procedures,
the triggers, and everything.
| | 02:35 | I bet that you already have some
databases in your organization.
| | 02:38 | If that's the case, you could click on
Import existing schema, go over here and
| | 02:44 | choose your database, make connections
to the server, tell it which database you
| | 02:51 | want, and then when you clicked on Next,
Visual Studio will go out and import all
| | 02:55 | the information from your existing database.
| | 02:57 | Now since we don't have a full-blown
version of SQL Server installed in this
| | 03:02 | machine, I am not going to demonstrate this.
| | 03:04 | Later, I'll show you a schema I
did get from an existing database.
| | 03:09 | At this point though, I'm going to
uncheck this Import existing schema and then
| | 03:12 | click on Next, and then verify that
everything looks the way I want and click
| | 03:18 | Finish and click Finish one more time.
| | 03:24 | At this point, I have created a Visual
Studio project that contains all the
| | 03:28 | information about creating a brand-new database.
| | 03:32 | This can be stored in source
control. Think about this for a second.
| | 03:36 | This becomes very beneficial.
| | 03:37 | Imagine this scenario: Each developer
in your team works with his own local
| | 03:41 | server instance of that database.
| | 03:43 | When one of your developers makes
the schema change--let's say he adds a
| | 03:47 | new column to the table--he'll do this not
in the real database, but inside this project.
| | 03:53 | He'll go over here to Schema Objects > Tables.
| | 04:01 | We're going to add a table here called Books.
| | 04:07 | So the change would be made here.
| | 04:10 | You would write the SQL script here to
modify the name of the column, or change
| | 04:15 | the name of the column.
| | 04:16 | Then I can check this in the source control.
| | 04:19 | All the other developers can check out
this item from source control and then
| | 04:24 | they can run the Update Database
feature that's inside this project.
| | 04:29 | That will update their local copy of
the database with the changes, and every
| | 04:33 | developer can do that.
| | 04:34 | Also, your SQL Server administrators
can do the same thing on your production
| | 04:38 | and your test servers.
| | 04:40 | Rather than walk through creating a
bunch of tables and start procedures, I'm
| | 04:43 | going to show you the second project
that I have here, called UseExisting.
| | 04:48 | I ran this against the NorthWnd
database. So if I come down here and show you
| | 04:53 | the table section here, you'd see that
it contains a number of table schemas.
| | 04:58 | I'll double-click on this
Shippers.table.sql. And this shows how to create the
| | 05:05 | table inside your local copy the database.
| | 05:09 | There are plenty of other interesting tools.
| | 05:11 | There is a Data menu up here that
has something called Schema Compare.
| | 05:16 | What I can do with this is
check two different databases.
| | 05:21 | I can look at this project's schema,
and this UseExisting project's schema, and
| | 05:27 | when I compare the two of them,
what's happening is it's looking at the
| | 05:30 | differences between the tables and all
the other entities in this database and
| | 05:34 | showing me what's missing.
| | 05:35 | For instance, if I look down here
you'll see that I have a Books table in the
| | 05:41 | BookSeller, but I don't have it in the
using existing. So then I can say, create
| | 05:45 | that table and then the next time when
we run this script against the database,
| | 05:48 | it's going to add that table
to the UseExisting database.
| | 05:52 | There is also another tool called Data Compare.
| | 05:57 | What this does is you point it at two
separate databases, and it looks at all
| | 06:01 | the actual data that's in the database
and make sure that they match up.
| | 06:05 | And there is also a Create Test Data
feature, where I can tell it to generate
| | 06:10 | fictitious data that I can
use on our testing applications.
| | 06:14 | Well, now that we've got this project
created, it's time to see how to create
| | 06:18 | design time data and modify database objects.
| | 06:21 | That's the topic for
another movie in this chapter.
| | Collapse this transcript |
| Clarifying the confusion on .NET Data| 00:01 | Microsoft really loves
creating data framework and tools.
| | 00:04 | They love it so much that they create a
new framework every few years. And when
| | 00:08 | they aren't building a new API, they have
teams enhancing and improving existing APIs.
| | 00:13 | As a result of their ongoing efforts,
it can seem overwhelming when choosing a
| | 00:16 | data access strategy.
| | 00:18 | My goal for this movie is to
explain the general layout of the data
| | 00:21 | infrastructures in .NET.
| | 00:24 | There are several areas in .NET
of interest to the data programmer.
| | 00:27 | Here's a list of the
upcoming topics in this movie.
| | 00:31 | Let's start at the beginning.
| | 00:33 | ADO.NET shipped in the original release of
.NET and has seen steady change over the years.
| | 00:39 | It works with data in many sources.
| | 00:41 | This is done by creating a specialized
data provider class that knows how to
| | 00:45 | talk to sources like Oracle,
SQL Server, Access, and ODBC.
| | 00:50 | There are many useful classes in ADO.NET.
| | 00:53 | There are classes that communicate with
the server, like Command and Connection,
| | 00:58 | and there are classes that permit
storing a cached copy of the data on the
| | 01:01 | client, like Dataset,
DataTable, DataColumn, and more.
| | 01:06 | An extension of ADO.NET
is called Typed Datasets.
| | 01:10 | These are generated from a tool and
are based on available data schemas.
| | 01:14 | Because of the generated class
structure, they are easier to use in
| | 01:18 | your application code.
| | 01:19 | Datasets are generated from
the command line by the XSD tool.
| | 01:24 | In Visual Studio, you can use the Dataset
Designer to create and edit the typed datasets.
| | 01:28 | Here you see a class called
Categories, which is a special version of an
| | 01:33 | ADO.NET data table.
| | 01:35 | At the bottom of the Categories
view, there are details about the
| | 01:38 | CategoriesTableAdapter.
| | 01:39 | Table adapters are responsible for
communicating with the data store.
| | 01:44 | Here you can see that we can call the
Fill, or GetData method, to retrieve data
| | 01:49 | from the underlying store.
| | 01:51 | Also, notice the relationships
between the Categories, Products, and
| | 01:55 | Suppliers data tables.
| | 01:58 | In recent years Microsoft has created a new
querying language that is becoming popular.
| | 02:02 | It is known as LINQ, which is an
acronym for Language Integrated Query.
| | 02:06 | It is written directly in your C# or VB code.
| | 02:10 | Here are two examples of a LINQ query.
| | 02:12 | Both are written in C# language.
| | 02:14 | On the top query, we are using LINQ to Objects.
| | 02:17 | The var q represents the potential query
that we're going to run at some future point.
| | 02:23 | As you can see from that first line,
we're querying the String class,
| | 02:27 | calling its GetMethods.
| | 02:29 | Then we're using familiar words, like
orderby and group, to further query the data.
| | 02:34 | If you look at the bottom query, which
is a LINQ to SQL query, you notice the
| | 02:39 | similarity between LINQ
to SQL and LINQ to Objects.
| | 02:43 | There are many different versions of
LINQ, each one optimized for querying a
| | 02:46 | special type of data.
| | 02:47 | Besides the two we've seen, there's
also a LINQ to XML and LINQ to Entity.
| | 02:52 | Finally, there is Entity Framework.
| | 02:54 | This is Microsoft's version of an object
relational mapper, often called an ORM.
| | 03:00 | ORMs are popular because they permit a
developer to think in simple terms like a
| | 03:03 | customer and products, while the ORM maps those
objects to the underlying data in the database.
| | 03:09 | There is more to data access.
| | 03:11 | Be sure and investigate
the services layer in .NET.
| | 03:14 | There you will find web services,
ADO.NET data services and WCF RIA Services.
| | 03:21 | While it may be a struggle to
understand all the nuances of the data layer in
| | 03:24 | .NET, at least you can be confident
that Microsoft is constantly working on
| | 03:28 | improving their data story.
| | Collapse this transcript |
| Using ADO.NET in your application| 00:00 | ADO.NET is the core library for accessing
data from relational and ODBC databases.
| | 00:06 | There are a number of specialized
namespaces, but for today's example, I'm going
| | 00:10 | to use the SQL client classes.
| | 00:12 | I am going to start by
showing you some basic ADO.NET code.
| | 00:14 | I'll show you how to connect to a database.
| | 00:16 | We'll execute queries against the
database using the command object, and then
| | 00:20 | I'll show you how to store data in
a local cache, known as a DataSet.
| | 00:24 | I am inside of Visual Studio, and I've
opened a solution called WorkingWithAdo.
| | 00:29 | Inside that is a project that already
contains a database called northwind.mdf,
| | 00:35 | and it contains a form, Form1.cs.
| | 00:37 | If I open up this user interface, I'll
see there are three buttons and one list box.
| | 00:43 | I am going to start by showing
you how to connect to the database.
| | 00:46 | I'll do that by double-clicking on this
Connect button, which will take me to my C# code.
| | 00:53 | To get a little more screen real estate,
I am going to close this output window
| | 00:56 | on the bottom of the screen.
| | 00:59 | Before you do anything with the
database, you need to create a connection.
| | 01:01 | I am going to use this called SqlConnection.
| | 01:04 | I then need to provide a
ConnectionString that explains all the details about
| | 01:08 | where to find the database.
| | 01:10 | In my case, it's going to look in a
folder called DataDirectory, and it's going
| | 01:14 | to look for a file called northwind.mdf.
| | 01:18 | To open the connection, I'll take my
connection object and call open.
| | 01:25 | To close the connection, I'll call the
Close method. And then to verify that the
| | 01:32 | connection is open, I am
going to type in "MessageBox.Show".
| | 01:37 | Then I'm going to ask the connection
for its current state, like this line
| | 01:47 | of code. And then I'll copy this message
box code and paste it after the conn.Close.
| | 01:55 | I am going to press F5 to run the
application, and then say yes to this dialog.
| | 02:02 | Then if I did everything correctly,
when I click on this Connect To DB,
| | 02:05 | I see the Open dialog and then
immediately after that I see the Closed dialog.
| | 02:16 | Once you've opened the connection,
you can instruct the database to
| | 02:18 | manipulate the data.
| | 02:20 | You're probably familiar with the four
basic operations you can do in a database,
| | 02:24 | sometimes called the CRUD operation:
| | 02:26 | you can create data, read the data,
update the data, or delete the data.
| | 02:32 | In SQL, we use the insert,
select, update, and delete queries.
| | 02:37 | So what I am going to do on this second
button click procedure down here is I am
| | 02:43 | going to open my connection, and
then I am going to declare a cmd object.
| | 02:57 | Next, I specify the CommandText that
I want to send over to the database.
| | 03:04 | This will be my SQL text. So I'll say
Select * from shippers, which is the
| | 03:12 | table I am interested in.
| | 03:17 | Then I am going to tell the command
object to use the connection to talk to
| | 03:22 | the database, like so.
| | 03:26 | Now, the rest of this code, I am
going to uncomment, Ctrl+K, Ctrl+U. This
| | 03:35 | executes the reader against
the database, which is going
| | 03:38 | to return some rows.
| | 03:40 | I checked to see if there
are any rows in the reader.
| | 03:43 | If there are rows, then I call Read,
which gives me the first piece of data,
| | 03:48 | and then I call reader.GetSqlString and I go
get the column with the number 1 from the data--
| | 03:56 | that's the first column of data--and
I am going to add that to the ListBox.
| | 04:01 | Then I have this while loop that
continues to read until it's empty.
| | 04:05 | Then I close the reader,
and then down here I've got another
| | 04:10 | query against a database. I am
going to do an update query.
| | 04:13 | So, I have another command text what I
am going to call as Update Shippers Set
| | 04:17 | CompanyName, or shipper id, and then I
am going to create a random number here,
| | 04:22 | and I'll execute that query.
| | 04:23 | I don't need to iterate over the results,
because I'm inserting data, or updating
| | 04:28 | the data into the database.
| | 04:30 | I'll run this application, press
F5, and there is our shipper data.
| | 04:41 | For my last demo, I want
to talk about the DataSet.
| | 04:43 | Ado contains this class called the DataSet.
| | 04:47 | This acts like an in memory
disconnected representation of your database.
| | 04:52 | Like a database, a DataSet can contain
data tables consisting of rows and columns.
| | 04:58 | You can also define data
relationships, constraints, and other database-like
| | 05:01 | constructs, but remember that the data
set is stored locally and is in memory only.
| | 05:08 | So this last button's code down
here is going to create the DataSet.
| | 05:12 | So here, I declare a DataSet.
| | 05:15 | Think of this as an in memory database.
| | 05:17 | I am declaring a table like this
shipper table here, and then I
| | 05:25 | create what's called a Data Adapter.
| | 05:27 | It's the DataAdapter's job to fill the table
and to push the data back into the database.
| | 05:33 | When I have the shipper data, I can
then create a SelectCommand, which is like
| | 05:37 | the command I created up here, and I
am saying here is the Select statement.
| | 05:42 | Now, unlike up above, this data
adapter can hold the SelectCommand, and it
| | 05:48 | also can hold text for the UpdateCommand, for
the inserting of data, and for the deleting of data.
| | 05:54 | So it can do all four of those operations.
| | 05:56 | I am doing the same thing down
here with the productAdapter.
| | 06:00 | I am creating a product.
| | 06:01 | I am saying select start from product.
| | 06:02 | I'm adding the Shippers table to the DataSet.
| | 06:05 | I am adding the Products table to the
DataSet, and then finally I'm calling Fill
| | 06:11 | and say, "Here's the DataSet.
| | 06:13 | Go out and get the data and
put it inside the Shippers table."
| | 06:17 | My last step is to take the data from
the Products table and put it in the list
| | 06:23 | box via the DataSource property.
| | 06:26 | Let's see if this works.
| | 06:28 | Press F5, click the Fill DataSet, and there
is all our product names from the database.
| | 06:34 | Now, as you can imagine, this is a lot of work.
| | 06:38 | I have to create all of my DataSets by hand.
| | 06:41 | So there is a wizard that is part
of Visual Studio that generates these
| | 06:44 | DataSets on my behalf.
| | 06:46 | That's the topic of the
next movie in this chapter.
| | Collapse this transcript |
| Creating typed datasets| 00:00 | Datasets are the basis of most of the
drag-drop designer tools in Visual Studio.
| | 00:06 | The dataset acts like an in-memory
disconnected representation of a database.
| | 00:10 | As you can imagine, creating a dataset
by hand is a tedious process. Fortunately
| | 00:15 | for us, Visual Studio contains a tool
for generating datasets. These types of
| | 00:19 | Datasets are known as strongly
typed datasets in some circles.
| | 00:23 | The benefit of using the tool is it
quickly creates all of the Dataset parts.
| | 00:28 | The benefit of having a strongly
typed Dataset is that all the columns are
| | 00:31 | correctly named and typed, so a column
in the database that contains a double
| | 00:35 | value will have a column in
the dataset of type double.
| | 00:39 | I'm inside Visual Studio, and I've
opened a Solution called CreatingDataSets,
| | 00:44 | which contains one project.
| | 00:46 | This project contains a Windows
Form with three buttons and a list Box.
| | 00:52 | I'm going to ask Visual Studio to
create a typed dataset for me by going to my
| | 00:56 | project and adding a database.
| | 00:59 | I'm going to choose add an existing item,
and then I'm going to go to my Desktop
| | 01:09 | open my Exercise Files folder and
scroll down to the Assets section.
| | 01:13 | You need to be sure that your filter
inside this dialog is set to All Files, so
| | 01:20 | that you can see the database file.
| | 01:24 | This is the database file
I'm looking for, northwnd.mdf;
| | 01:27 | I'm going to click on Add.
| | 01:32 | Now, Visual Studio recognizes that it
can generate a dataset from this database,
| | 01:37 | so it asks me, would I like to
create a dataset or an Entity Data model.
| | 01:41 | I'm going to create the simpler of the
two, which is the dataset, and then I
| | 01:46 | want to click on Next.
| | 01:47 | It just went out and talked to that
database and has retrieved the list of all
| | 01:54 | of the resources that are
available in that database.
| | 01:56 | Here is the listing of the tables.
| | 01:59 | Within the tables, I can see
things like the different column names.
| | 02:02 | There is also a section that shows the
Stored Procedures inside that database.
| | 02:09 | I'm just going to take a couple of
tables from the database. I'm going to take
| | 02:13 | the Products table and the Shippers table, and
I'm going to leave the dataset name the same.
| | 02:20 | Next, I'm going to click on Finish.
| | 02:26 | Now, inside my app.config file, that
wizard generated this connectionString.
| | 02:32 | I can then use this
connectionString to connect to the database.
| | 02:36 | In fact, all I need to do is talk to
the dataset, and it automatically will
| | 02:41 | connect and talk to the database.
| | 02:42 | Let me show you what I'm talking about.
| | 02:45 | I'm going to go to this Fill
DataSet button and double-click on it.
| | 02:51 | I'm going to declare an instance of the
products table by typing "var products",
| | 02:58 | the name of my dataset, which was
"northwndDataSet.ProductsDataTable".
| | 03:13 | This is the line of code where I'm
going to create the TableAdapter.
| | 03:21 | And as you can see there is a
ProductsTableAdapter and a ShippersTableAdapter,
| | 03:27 | I'm working with the products, so
I am going to choose this one here.
| | 03:30 | The TableAdapters job is to talk to the database.
| | 03:34 | This is where it does the Select
queries, the Delete queries, the Update
| | 03:37 | queries, and the Insert queries.
| | 03:39 | So next, I'm going to ask the
TableAdapter to fill the products table.
| | 03:43 | So I say ta.Fill and then I pass in
the table that I want to fill, which in my
| | 03:50 | case would be that
instance of the products class.
| | 04:00 | And then I'll tell my listBox to use
the ProductName when it's showing the
| | 04:06 | data. And lastly, I need to make sure that I
fill my listBox. My listBox is called listBox1.
| | 04:12 | I choose DataSource =, and then I'm
going to assign the products table.
| | 04:27 | Now, I see, I forgot to put a set
of parentheses after this new line.
| | 04:37 | It's nice to have IntelliSense.
| | 04:38 | I'll press F5 to run the application
and Y to dismiss that dialog, and I'll
| | 04:45 | click on the Fill DataSet, and I'm
now using that strongly typed dataset.
| | 04:53 | You can see down, here in this example,
I can also use the strongly typed access.
| | 04:58 | The product has a UnitPrice property on
it, which I can use to set or retrieve
| | 05:02 | the unit price of a particular product.
But I'd rather show you for the rest
| | 05:06 | of the demo is how to create my
own extra query against the database.
| | 05:10 | So I'm going to open this XSD file that was
created for me when I generated the dataset.
| | 05:15 | This contains a designer surface
for making changes to my dataset.
| | 05:19 | I'll double-click on this file, and here
you can see the Products table and the
| | 05:26 | Shippers table, and then at the
bottom you see the ProductsTableAdapter.
| | 05:30 | Remember, this is what gets the data
out of the database and puts it into the
| | 05:34 | products table, and takes the changes of the
data and puts it back into the data source.
| | 05:38 | So I'm going to come down here on the
bottom, and I'm going to add a new query.
| | 05:43 | I like to choose SQL statements, a
SELECT query. It's already stubbed in the
| | 05:53 | code for a basic query. And I'm just
going to come down here and say, Where
| | 05:59 | UnitPrice > some parameter value @price.
| | 06:09 | So now this is a parameterized query.
| | 06:11 | I'll pass this piece of information in,
and it will return all the products that
| | 06:15 | are greater than this price.
| | 06:17 | I'll click on Next.
| | 06:18 | I'm going to call my method
| | 06:20 | that is going to be on my TableAdapter
GetDataByPrice, and I'll click on Next and Finish.
| | 06:28 | Notice there is a new row here now.
| | 06:32 | There is a GetDataByPrice.
| | 06:34 | So now I'll go back to my form, and
I'll go to this Fill Products by Price, and
| | 06:43 | I just need to write two
lines of code. var ta = new.
| | 06:54 | I need that same TableAdapter, and now
that TableAdapter has my new query on it,
| | 06:59 | so I'm going to say listBox.
DataSource = ta.GetDataByPrice. And watch what
| | 07:08 | happens when I hit the open parenthesis.
| | 07:11 | Over here it says I need a price
parameter to give to the query, so I'm going
| | 07:17 | to use 40M--M for decimal--and then I need to
uncomment this line of code and this line of code.
| | 07:25 | Here is a little trick. I'm going to highlight
all four lines and then do Ctrl+K, Ctrl+U. Press F5 to
| | 07:32 | run the application, choose to save the
data, and there are all the products that
| | 07:39 | are greater than $40.
| | 07:42 | Now you have the general
idea how to create a dataset.
| | 07:44 | You still need to write code
to work with the data, however.
| | 07:47 | Watch the Binding movie in this
chapter to see how to use the drag and drop
| | 07:50 | designer to quickly set up data
bindings in your user interface.
| | Collapse this transcript |
| Using the data binding tools| 00:00 | Showing data from your
business models is a common practice.
| | 00:03 | I'd argue that it is the prime
mission for both business applications.
| | 00:07 | If you are an experienced
programmer, you've probably worked on an
| | 00:09 | application, or you retrieved data from
a source, showed the data to the user in
| | 00:13 | various user controls,
| | 00:15 | allowed the user to change the data and then
saved the data back to the original source.
| | 00:20 | This is such a common scenario that
our industry is constantly building and
| | 00:23 | refining binding frameworks
to simplify these common tasks.
| | 00:27 | Microsoft is helping us in this area too.
| | 00:28 | Visual Studio contains a
number of data binding tools.
| | 00:32 | Some of them work in the
drag-and-drop manner. Let me show you.
| | 00:37 | I am inside a solution called
UsingDataBindingtools that contains a Silverlight
| | 00:42 | application and a WinForms application.
| | 00:44 | I am going to start by using
the WinFormsApp for data binding.
| | 00:49 | Now I have already added a
database and a typed data set.
| | 00:54 | See the "Creating typed datasets"
movie to learn more about how to create
| | 00:57 | these types of sources.
| | 00:59 | There is a special window called the Data
Sources window that permits drag-and-drop.
| | 01:04 | To see the Data Sources window, you need
to go to Data and then Show Data Sources.
| | 01:10 | This window is usually docked to the
left side of the screen, and on my computer
| | 01:14 | it's occupying this lower left-hand corner.
| | 01:17 | As you can see, it's automatically
picked up on all the tables that are
| | 01:20 | inside that data set.
| | 01:21 | I am going to open this form called
AddShipperGrid. And here is where the drag
| | 01:28 | and drop-data binding comes in.
| | 01:29 | I am going to take this Shippers table,
and I am going to drag it over and drop
| | 01:33 | it on the designer surface.
| | 01:36 | And a lot a magic just happened.
| | 01:38 | First of all, it created a data
grid and added the correct columns.
| | 01:42 | It added all of this
stuff down here in the bottom.
| | 01:44 | It added a dataset, and a TableAdapter
for the Shippers and some other items.
| | 01:50 | It added this thing called the
BindingNavigator, which is this toolbar across the
| | 01:54 | top for navigating forward and backward
through my rows, and adding rows and deleting
| | 01:58 | rows and saving them out to the data
Source. And it's automatically wired up
| | 02:04 | all the code to show the data.
| | 02:06 | So we run the application, and it shows
the Add Shippers Grid. You'll see that
| | 02:15 | it's automatically populated, and I can
use these forward and backward buttons
| | 02:20 | to move to new rows, and so on.
| | 02:24 | You can also add more complex grids.
| | 02:27 | Here is a AddParentChild form.
| | 02:29 | I am going to switch over and show you this one.
| | 02:31 | I am going to add some existing controls.
| | 02:34 | I am going to add a list box, and I am
going to add something called the DataGridView.
| | 02:44 | Here it is.
| | 02:46 | So these already exist on the window.
| | 02:48 | I can now take this Categories table
and drag it over and drop it on this
| | 02:53 | list box, and the data binding engine
will add some sources on the bottom,
| | 02:58 | and it also correctly configures my
list box to have DataSource and
| | 03:04 | Display name and so on.
| | 03:06 | I am going to take the Products table
and drag it and drop it on the grid.
| | 03:11 | Now in my DataSource, if I open up the
northwinddataset.xsd, you'll see that
| | 03:19 | there is a relationship between the
Categories table and the Products table.
| | 03:22 | So I am going to exploit that in my
code and make sure that when I click an
| | 03:26 | item in the list box it will show the
related product in the other control.
| | 03:32 | To do that, I am going
to click on this list box.
| | 03:34 | I am going to add an Event procedure by
clicking on this lightening bolt, and I
| | 03:38 | am going to add
SelectedIndexChanged Event procedure.
| | 03:42 | Double-click on this section, and then I
am going to say, productsBindingSource--
| | 03:47 | which is one of those autogenerated items--
| | 03:49 | .Filter =. And I need a semi-colon here, and
then I am going to say, this is a CategoryID=.
| | 04:03 | So what this is going to do is
it's going to create a string
| | 04:22 | that says CategoryID, and then it's
going to use whatever the product name is
| | 04:25 | here. The CategoryID number is going to
put that in here, and it's going to
| | 04:30 | apply that to the filter.
| | 04:31 | I'll run the application. Click on
this button Add Parent Child view.
| | 04:37 | Now again, here you see
the Beverages at the top.
| | 04:41 | These are the beverages that we sell.
| | 04:44 | Here is the condiments that we sell, and so on.
| | 04:46 | There is the dairy products that we sell.
| | 04:52 | In my next demo, I am going to show you
that you don't have to use data grids.
| | 04:56 | I am going to use this form
called AddShippersControl.
| | 04:58 | I am going to go back to that Shippers
table in the Data Sources, and instead of
| | 05:02 | dragging it over and getting a table, I
am going to tell it to use Detail view.
| | 05:06 | So I select this dropdown. You can
see that the symbol changes over here.
| | 05:12 | Now when I drag this over, I still get
bound controls, only this time I get a
| | 05:17 | text box that is bound to the source
and a text box is bound to the Company
| | 05:21 | Name, rather than getting a data grid.
| | 05:23 | For my last example here, I am going to
show that when I am doing this drag and
| | 05:29 | drop--for instance, there is two
dates here for this Categories--
| | 05:34 | when I drag these over, it's
going to use a special control called
| | 05:37 | the DateTimePicker.
| | 05:39 | I can tell that data sources that I
would rather use as an alternative control
| | 05:44 | when I drag it over to the surface.
| | 05:47 | So I am going to choose this dropdown,
and I am going to come down here and say,
| | 05:50 | I don't want the
DateTimePicker anymore. I want a TextBox.
| | 05:53 | So now I will switch this over to
Details view, and I will get one
| | 05:58 | DateTimePicker and one TextBox.
| | 06:01 | There is the DateTimePicker.
| | 06:05 | There is the TextBox.
| | 06:06 | That just shows you that you
can customize the controls.
| | 06:09 | So up to this point, we've been
doing data binding using a database and
| | 06:13 | this northwinddataset.
| | 06:17 | Another source of data is .NET objects.
| | 06:21 | So I am going to switch over
to my Silverlight application.
| | 06:24 | I will make this my Startup here, this
ASP.NET application, and I am going to
| | 06:30 | switch to my SilverlightApp, and I am
going to go to this MainPage. And I want
| | 06:34 | to drag and drop this class that
are right here called CalenderDays.
| | 06:39 | If you look inside this class,
you'll see there is other class called
| | 06:42 | CalenderDay, and then I wrote a
class that returns a list of items.
| | 06:47 | It derives from the list of CalenderDay.
| | 06:50 | So what I am going to do is I am
going to add a new data source to
| | 06:54 | this SilverlightApp.
| | 06:55 | So I will go to Data > Add New Data Source,
| | 07:00 | tell it that the source of the data is
going to be come from an object, tell it
| | 07:04 | I want to use both these objects--
| | 07:06 | CalenderDay and
CalendderDays--and then click Finish.
| | 07:10 | Now I am going to go to my designer surface.
| | 07:14 | Here is my Data Source. I am going to grab that
CalenderDays, and drag it over and drop it on my
| | 07:20 | Silverlight surface.
| | 07:21 | Now it has a slightly different
way of setting up the connection.
| | 07:25 | I need to go to my code behind
window and write a little bit of code.
| | 07:28 | So let me press F7 to do that.
| | 07:31 | Here is all the code that was
automatically written for me.
| | 07:34 | It's really hard to read,
| | 07:35 | so I have created a slightly easier
version of that to read down here.
| | 07:38 | I am going to copy this, go to my
Loaded Event, paste it in, and then Ctrl+K,
| | 07:46 | Ctrl+U to uncomment this.
| | 07:49 | What this is going to do is it's going
to go out to my MainPage.xaml, find this
| | 07:55 | DataContext that was generated
when I did the drag-and-drop--
| | 07:58 | that's called the Calendar View Source--
| | 08:00 | it's going to get a handle on that,
and then it's going to instantiate an
| | 08:06 | instance of CalendarDays
and apply that to the source.
| | 08:09 | Now if I did everything correctly, I
should get a data grid populated with
| | 08:12 | the days of the week. Press F5.
| | 08:16 | I will press Yes. Success!
| | 08:26 | Are all seven days of the
week there? Yes, they are.
| | 08:29 | Well, that's about all I have for drag and drop.
| | 08:32 | It's simple to use,
| | 08:33 | it's customizable, and for simple
interfaces, it's just the right tool.
| | Collapse this transcript |
|
|
8. Debugging Your ApplicationDebugging code| 00:00 | Nobody likes to write code that
contains bugs, but it is a fact of
| | 00:03 | the programmer's life.
| | 00:04 | So in this movie, I would like to show you
the debugging tools that are available
| | 00:08 | inside Visual Studio.
| | 00:09 | I'm inside Visual Studio, and I've
opened a solution called DebuggingCode, which
| | 00:13 | contains one project
called ExploringBreakpoints.
| | 00:15 | Let me show you the main
UI for this application.
| | 00:18 | I'm going to double-click on MainWindow.xaml.
| | 00:21 | It's a simple form that contains seven buttons.
| | 00:24 | Each of these buttons contains one
of the demos I'm going to show today.
| | 00:26 | Before I continue, let's make sure we
have one setting enabled in Tools > Options.
| | 00:30 | I'll go to Tools > Options, and then
you want to go down to the Debugging
| | 00:35 | section, and go to Edit and Continue.
| | 00:39 | Make sure there is a check
mark in Enable Edit and Continue.
| | 00:42 | This allows us to edit the code
while we're sitting at a breakpoint.
| | 00:46 | I don't believe this works on 64-
bit operating systems, however.
| | 00:49 | So now I'm ready to add a breakpoint.
| | 00:53 | I'm going to switch to my Code view by
double-clicking on the Simple Breakpoint button.
| | 00:58 | Here is the code that I want
to examine under a debugger.
| | 01:02 | So I can put my cursor on line 37,
and then I can press the F9 key.
| | 01:07 | Notice that the line turns red, and
on the left margin is a red circle.
| | 01:12 | That signifies that there is a
breakpoint waiting at this line.
| | 01:15 | To turn off the
breakpoint, I can press F9 again.
| | 01:19 | Another way of adding a
breakpoint is to click on the margin.
| | 01:23 | Now that I have this breakpoint, I need
to run the application under a debugger.
| | 01:28 | The easiest way to do that is to
click the Start Debugging or press F5.
| | 01:35 | Now when I click on the button, we
switch over to Visual Studio, and we're
| | 01:39 | sitting here looking at the breakpoint.
| | 01:41 | This line of code has not run yet.
| | 01:43 | To run this line of code, I need
to click on this Step Into button.
| | 01:49 | Now you can see the breakpoint is
still on line 38, but the next line of code
| | 01:52 | that's going to execute is line 39.
| | 01:56 | Another way of putting a breakpoint in
your code is to write a little bit of code.
| | 02:00 | I can come down to this section and
write a System.Diagnostics.Debugger.Break().
| | 02:05 | That means that when the
debugger encounters this line, it'll
| | 02:08 | automatically stop.
| | 02:09 | Now, you probably don't want to
leave this in your production code,
| | 02:12 | so what you can do is wrap it in
an if statement, like I'm doing here.
| | 02:16 | I'm going to uncomment this by doing
a Ctrl+K, Ctrl+U. Now, when I run this
| | 02:24 | breakButton, I'll switch back to my UI,
| | 02:29 | I'll press F5 to continue running the
application, and then I can click on this
| | 02:33 | button, and you see again I have a
yellow marker sitting on that line.
| | 02:36 | Now, there's no red indicator on this
one because that you wrote the code to
| | 02:40 | stop instead of adding a
breakpoint. And then I'll press F5.
| | 02:46 | If you have an exception in your code,
and you don't have an exception handler,
| | 02:50 | which is called an untrapped exception,
then you'll get an opportunity to debug
| | 02:55 | that if you're running under a debugger.
| | 02:56 | Let me click on this button here.
| | 03:00 | So I wrote some code that was
attempting to open a file stream, and I passed it
| | 03:04 | an invalid text file name.
| | 03:07 | So when .NET went out to open this file,
it couldn't find the file, which is
| | 03:10 | what this exception error is I'm getting here,
| | 03:12 | "Could not find file 'C:\bogus.txt'."
| | 03:16 | This is a nice little window that
shows me there's an error at this line.
| | 03:21 | It switched over to a break, so that I
can look at the code and see if I can
| | 03:25 | figure out what's wrong.
| | 03:26 | I can click on this View Detail button
to learn more about the exception, and I
| | 03:34 | can dismiss that dialog and then make
any changes necessary and then try to run
| | 03:39 | this application again.
| | 03:40 | I'm going to stop the application
for a second and rerun the application.
| | 03:50 | Now, we're going to look at
how to step through our code.
| | 03:53 | I'm going to click on this button.
| | 03:55 | I do believe I need a breakpoint on this line.
| | 03:58 | So, I switched over to my code.
| | 04:02 | This is the stepping code.
| | 04:04 | I'm going to add a breakpoint right
here on line 75, F9, Alt+Tab back to my
| | 04:11 | Running application and then
click on that button again.
| | 04:13 | Now, I should say something about breakpoints.
| | 04:16 | When you save your application, it's
going to store your breakpoints in a
| | 04:20 | special user-specific file.
| | 04:22 | So if I open this file again tomorrow,
my breakpoints will still be here.
| | 04:25 | If you were to open this file--say I
sent you a copy of my solution--you
| | 04:29 | wouldn't have my breakpoints, because you
get your own user-specific Solution file.
| | 04:34 | I'm ready to run this line of code.
| | 04:36 | Now, I've showed you earlier,
you can click on this button;
| | 04:38 | you can also press F11 to do a single step.
| | 04:41 | Watch what happens if I do a
single step by pressing F11.
| | 04:44 | There is a function over here called
CalculateMonthlyPayment, and now I've
| | 04:47 | entered that function.
| | 04:49 | Now, I can press F11 to
walk through my code over here.
| | 04:55 | Then I'll return back to the previous code.
| | 04:58 | Another way of walking through your
code is to click on this Step Over.
| | 05:02 | What that does is it looks at
this and says, "We'll there are six or seven lines
| | 05:05 | of code over there, in that method.
I'm going to skip over those.
| | 05:09 | I'm just going to run this as
if it was a single line of code."
| | 05:11 | So, by pressing Step Over, I don't
have to walk through that method that I
| | 05:15 | already have debugged.
| | 05:16 | See, when I clicked on that button,
it immediately went to the next one.
| | 05:19 | There is another setting.
| | 05:22 | Let me click on the Step Into.
| | 05:25 | Now I'm back inside that
CalculateMonthlyPayment.
| | 05:27 | Let's say that I've run a couple of
lines of code in here, and I don't want to
| | 05:32 | step through the rest of the code.
| | 05:33 | I can use this Step Out button to
finish running all the code in here and then
| | 05:38 | stop on the first line of code
back in the calling function.
| | 05:41 | So let me show you how that one works.
| | 05:43 | As you can see, it
returned back to the calling method.
| | 05:45 | You remember that Edit and
Continue feature I turned on earlier?
| | 05:49 | I can now rerun some of these lines of
code by dragging this yellow marker up
| | 05:53 | and rerunning that line of code by pressing F10.
| | 05:56 | Another thing that I can do is I can now edit
this code while I'm sitting at a breakpoint.
| | 06:01 | Let's say I don't like
the name of this variable.
| | 06:02 | I'm going to have to
rename it in several places.
| | 06:13 | But now that I've renamed it successfully, I
can press F10 to successfully run that code.
| | 06:17 | So I'm able to edit the code
while I was sitting at a breakpoint.
| | 06:21 | Let me press F5 and click on the next button.
| | 06:24 | Now, I've got three more
examples I want to show you.
| | 06:27 | So let me shut this application down and
add some breakpoints before I continue.
| | 06:31 | I'll press F9 while I'm on this DataTips.
| | 06:33 | I'll press F9 while I'm down here on the
visualizerButton, and then I'm going to
| | 06:38 | add a special better
breakpoint down in this area.
| | 06:41 | I'll come back and show
you that in just a second.
| | 06:43 | Actually, I think I'll show it to you now.
| | 06:45 | I'm going to add a special breakpoint here.
| | 06:46 | One of the new things in Visual
Studio is that you can add breakpoints that
| | 06:50 | have special counts.
| | 06:51 | So, I'm going to add a breakpoint here,
and then I'm going to right-click on
| | 06:54 | it and set Hit Count.
| | 06:57 | Then I'm going to choose when
the hit count is a multiple of 60.
| | 07:01 | Notice that there's a special
Plus sign now on the breakpoint?
| | 07:09 | Let's run these last three demos.
| | 07:10 | Again, I'm going to go up and
choose Debug > Start Debugging.
| | 07:13 | There is a special tooltip in
Visual Studio called a datatip.
| | 07:21 | I'm going to press F10 a couple of times
to move down into my code, and then I'm
| | 07:25 | going to hover over this variable.
| | 07:27 | You can see the datatip.
| | 07:28 | It tells me that the current
value of that variable is 0.0.
| | 07:32 | And if I come down here and click on
this birthDate, it shows that it's 5/5/1992.
| | 07:37 | Notice there's also a Plus symbol
here, where I can see more specific
| | 07:40 | details about that item.
| | 07:42 | There is also a little stickpin over here.
| | 07:46 | Click on this and then it
adds the tip to my coding window.
| | 07:49 | So now that you notice as I move to my
code, that little datatip is now stuck
| | 07:53 | there for me to keep watch on it.
| | 07:54 | Let me press F10 a few times here.
| | 07:57 | We'll run through this code.
| | 07:58 | This is another feature,
| | 08:02 | I can choose Run To Cursor.
| | 08:03 | I'm going to click down here and
right-click and choose Run To Cursor.
| | 08:07 | Now it'll run down to the line of code
where my cursor is sitting, and then I
| | 08:11 | can hover over this to see that I have
five items inside that SortedDictionary.
| | 08:15 | Now, I'm going to press F5 to return
back to the UI, and I'll show you this
| | 08:20 | other cool thing called the Visualizer.
| | 08:23 | I'll press F10 twice to
run down to this curly brace.
| | 08:26 | I've just loaded an XML file into this variable.
| | 08:30 | If you hover over it, you can see
that there is a lot of raw text in there,
| | 08:33 | but there's this thing called the data
visualizer, where I have the magnifying glass.
| | 08:38 | I can click on this magnifying
glass and then choose a different
| | 08:41 | representation of the data.
| | 08:42 | I like this XML Visualizer
better than the raw Text view.
| | 08:46 | That's what's currently stored in that variable.
| | 08:48 | I'll press F5 one more time, and you
probably remember just a few minutes ago, I
| | 08:57 | added that special breakpoint.
| | 08:59 | Now, I'm going to execute that
breakpoint by clicking here, and I have just run
| | 09:03 | through this loop counter and if you
hover over the counter, you'll see that I'm
| | 09:08 | at 60, because that was my
condition I set up for the counter.
| | 09:11 | If I press F5 again, I'll now have
run through another 60 iterations.
| | 09:17 | So rather than having me step through
this over and over again, I can just skip
| | 09:20 | forward 60 steps in this loop.
| | 09:23 | These debugger tools that I'm showing
you are just the ticket when you have an
| | 09:26 | unexplained problem in your code.
| | 09:28 | In the next movie, I'll show you how to
use the Watch and other debug windows.
| | Collapse this transcript |
| Working with the Watch and other debug windows| 00:00 | In this example, I will look at the watch,
locals, call stack, and thread windows
| | 00:05 | inside Visual Studio.
| | 00:07 | I'm inside a project called UsingWatchWindows.
| | 00:10 | This is a WPF application, and I
have a little sample UI down here
| | 00:13 | in MainWindow.xaml.
| | 00:15 | Let's take a look at it.
| | 00:16 | There are six buttons in here, and each
of these buttons allows me to show you
| | 00:22 | one of the windows inside Visual Studio.
| | 00:25 | And before I show you the actual
windows, I need to build my application, and
| | 00:30 | then I am going to attach the Debugger
by clicking the Debug > Start Debugging.
| | 00:36 | Then I am going to click on
this Watch Windows button.
| | 00:38 | Notice that I have this code in
here that causes the debugger to stop
| | 00:42 | automatically if a debugger is attached.
| | 00:45 | I talked about that in the previous movie.
| | 00:47 | I am going to press F10 to
step through a few lines of code.
| | 00:51 | The idea behind the watch window is
that it's a permanent repository for data
| | 00:54 | that you want to look at.
| | 00:55 | So if I want to keep this s1 around,
instead of having to hover over this to see
| | 00:59 | what's stored in there, I can just
take it and drag it down and drag, drop it
| | 01:04 | into the Watch area.
| | 01:06 | And then when I see the data down
here, and I notice that there's an error,
| | 01:09 | I can fix as while I am at the breakpoint by
double-clicking on the value in the watch window.
| | 01:15 | Typing in the new value, hi, and it
shows it red now that signifies I've
| | 01:21 | just changed that value.
| | 01:23 | Now if I hover over the s1 variable up here,
you see that it now contains the new data.
| | 01:27 | So I can edit the data and watch the data.
| | 01:30 | Of course, I can drag more
sophisticated objects down here, like this book
| | 01:34 | object, and then I can click on this
Plus symbol to expand the details and read
| | 01:39 | more about this class.
| | 01:41 | I don't have to use
variables that I have declared.
| | 01:44 | I can work with the local class.
| | 01:47 | I'm inside this MainWindow.xaml file
right now as I am running. So I could come
| | 01:50 | down here and type something in, like
the word "this," which refers to the current
| | 01:55 | instance, and then I can read
information now about this window I'm inside.
| | 02:02 | Also, I can see information about some
of the buttons in here, and I can also
| | 02:06 | use completely arbitrary
calls out to .NET framework.
| | 02:10 | For instance, I could say System.IO.
Directory.Exists, type in the path to a directory like so,
| | 02:26 | and then it will evaluate that
expression and tell me that no, that
| | 02:29 | directory does not exist.
| | 02:32 | Next I would like to show you
the locals and autos windows.
| | 02:34 | I am going to press F5 to finish running
the code in here and return back to my UI.
| | 02:39 | Click on that Locals and Autos buttons.
| | 02:42 | Now I'm going to take a look at
these two windows here and here.
| | 02:46 | So the autos windows watches your cursor, and as
you step through your code--I am pressing F10--
| | 02:53 | it's keeping an eye on the line of code
where the yellow marker is and several
| | 02:57 | other lines of code around that marker.
| | 03:00 | So right now I can see three variables.
| | 03:02 | If I press the F10 key one more time,
you will see that those variables change.
| | 03:06 | Now compare that to the Locals Window.
| | 03:09 | The Locals Window shows me all
variables that are in scope inside this method,
| | 03:14 | so that would be the sender, which is
a parameter, RoutedEventArgs which is a
| | 03:19 | parameter, plus also these other variables here.
| | 03:22 | Now as I step through my code, I'm
going to get to this line of code right here,
| | 03:25 | 68, and that's going to
move me in to another function.
| | 03:28 | So as I press F11 now, I have now
entered the second function, and you will
| | 03:34 | notice that the Locals Windows changes
and shows me only the variables that are
| | 03:37 | in scope inside that function.
| | 03:40 | Pressing F11 a few more times, I will
leave this function and return back to the
| | 03:44 | previous calling function, and then
again the Locals Window repopulates with the
| | 03:48 | data from that area.
| | 03:49 | I will press F5 to continue.
| | 03:52 | Next, I want to show you the call stack.
| | 03:55 | In this example I have a function
called callStackButton_Click, and it's going
| | 04:00 | to call a function called CreateLoan.
| | 04:01 | So if I go over and click on this
Call Stack button--and if you don't see
| | 04:05 | these windows, you may need to go up to
Debug > Windows and enable the different windows.
| | 04:11 | As you can see, there are a lot of
other windows I am not going to cover, for
| | 04:15 | instance, there is the Register's
window and the Memory window. So here I am.
| | 04:18 | I am ready to make this call.
| | 04:23 | If I look at the Call Stack, I
see that I am inside this function.
| | 04:26 | Now I am going to press F11.
| | 04:27 | Now I have entered the second
function, and the call stack now reflects that
| | 04:31 | and says, "I am in this CreateLoan," and it
was called from this button click procedure.
| | 04:37 | One of the things I can do in the Call
Stack window is double-click on the
| | 04:40 | previous function, and it will take me
over and show me where the code was where
| | 04:45 | I entered this function.
| | 04:46 | Then I can double-click on this one
with the yellow marker, which takes me back
| | 04:49 | to the current line of code is ready to execute.
| | 04:52 | I will press F11 a few times. Now I
have entered another class, and you can
| | 04:57 | see the call stack in now three-deep. And then
when I leave this, it goes back to being two-deep.
| | 05:03 | Press F5 one more time.
| | 05:04 | I am going to click on this button.
| | 05:08 | The Immediate window lives over here,
and what the Immediate window allows me to
| | 05:12 | do is run one single line of code and evaluate it.
| | 05:16 | So right now, I would like to call
this function, Financial CreateLoan.
| | 05:20 | So I come over here, and I will type in "Fin".
| | 05:22 | There is Financial.CreateLoan.
| | 05:29 | I pass the person's name in, and then I
pass in the value of the loan--$5-- and
| | 05:38 | I am going to press Enter, and that
function just ran, and notice nothing
| | 05:43 | happened up here, right?
| | 05:44 | I am running this function independently.
| | 05:46 | It was happening in this window, and then
I get back the results from that function.
| | 05:51 | I can also print any details I want by
typing in the question mark, which is
| | 05:55 | shorthand for print, and then I can
type in something like "this.Width".
| | 06:01 | Here I am referring to the window that
I'm inside, and I can find out that I am
| | 06:05 | 525 pixels wide. Press F5 one more time.
| | 06:10 | Next, I want to look at the Thread windows.
| | 06:12 | So I am going to click
on this button right here.
| | 06:14 | I am inside this click procedure.
| | 06:17 | By going in and choosing Debug >
Windows > Threads, I can examine how many
| | 06:26 | threads I am running.
| | 06:27 | Now, you may not see the same
number of threads that I have here.
| | 06:30 | I have got two threads.
| | 06:31 | It depends on whether you have this
thing called the VsHost running or not,
| | 06:36 | which is a special debugger helper.
| | 06:38 | If VsHost is running, then you'll end
up with four or five more threads than
| | 06:43 | what I currently have.
| | 06:44 | So I have got the main thread,
and I've got one worker thread.
| | 06:49 | I have another example--
| | 06:50 | let me press F5--where I am going to spawn
a worker thread to go do some extra work.
| | 06:56 | Let's make this a little bit smaller.
| | 06:59 | What I am going to do here is I am
going to queue up a thread from the
| | 07:03 | ThreadPool and tell it to go run
the code in this OpenTheFiles method.
| | 07:08 | So currently, I'm on the UI thread,
because I'm running inside this WPF application.
| | 07:13 | When I get down to this line of code, I
will be running on a ThreadPool thread.
| | 07:17 | So I can see the difference in this window.
| | 07:19 | I am going to press F5.
| | 07:22 | Now I have hit this breakpoint that's
inside the OpenTheFiles function. And as
| | 07:26 | I can see in this Thread Window, I'm now
running on this thread down here, 1352.
| | 07:31 | As you saw earlier, there are
other Debug windows available.
| | 07:34 | For example, there is the Register
Window, which shows the data that is stored
| | 07:38 | in the CPU registers.
| | 07:40 | These additional debugging windows are
adapted for special debugging tasks and
| | 07:44 | worth investigating.
| | Collapse this transcript |
| Other debugging techniques| 00:00 | Continuing our quest to master the
debugging tools in Visual Studio, I am going
| | 00:04 | to cover trace points, attaching a debugger to
a running process, and debugging a web site.
| | 00:10 | So let's get started!
| | 00:11 | I am inside the solution called
OtherDebuggingTechniques, and it contains two projects:
| | 00:17 | AttachToConsole and DebugWebApp.
| | 00:19 | I am going to start by showing
you something called a tracepoint.
| | 00:24 | To do that, I am going to
double-click on this Program.cs.
| | 00:27 | If you've watched the other movies in this
chapter, you know that you can add a
| | 00:31 | breakpoint by clicking in
this margin or pressing F9.
| | 00:35 | A breakpoint is hit if you're attached to
it debugger, and you run this line of code.
| | 00:39 | It will then stop and let you look at your code.
| | 00:42 | A tracepoint is similar to a breakpoint.
| | 00:44 | When you establish a tracepoint, you
also specify additional action that can be
| | 00:47 | taken when it is hit. Let me show you.
| | 00:50 | I am going to right-click on
this breakpoint and choose When Hit.
| | 00:55 | Now, it doesn't actually say the word
"tracepoint" in here, but this is where you
| | 00:58 | would enable a tracepoint.
| | 01:00 | I click here, and then I get to
choose what happens when the debugger
| | 01:07 | encounters this tracepoint.
| | 01:08 | First of all, I can have an output
information to the Output window.
| | 01:12 | I can have it do this silently.
| | 01:15 | If this Continue execution check mark
is checked, then it'll print this output
| | 01:20 | to the Output window, but it won't
stop and let me look at the code.
| | 01:23 | Let me add a custom string at the front
of this, so we can find it easily, and
| | 01:28 | then this is going to print the
function name, and this is going to print the
| | 01:31 | thread ID and the thread's name.
| | 01:33 | As you can see, there are other
variables I can inject in there.
| | 01:36 | I can also do other actions like run
macros, and macros are covered in another
| | 01:41 | chapter in this title.
| | 01:42 | Macros are little bits of code that
you can run from within Visual Studio.
| | 01:45 | I am just going to do this basic tracepoint.
| | 01:48 | Make sure I uncheck this Run
a macro first. Click on OK.
| | 01:53 | Notice it has a diamond now instead of a circle?
| | 01:55 | That's how you know it's a tracepoint.
| | 01:58 | Then I am going to do a Debug > Start Debugging.
| | 02:00 | It ask me to enter my name, it says,
"Hello Walt," and now if I come over here to
| | 02:08 | my Output window and scroll down to the end--
you may not see this window in your computer;
| | 02:14 | if you don't, you need to go to View >
Output--and here at the bottom of the
| | 02:19 | window is my custom output string.
| | 02:23 | Notice that it didn't stop my code I am running.
| | 02:30 | So that's the tracepoints.
| | 02:31 | Another common task that you might
want to do when you are debugging is you
| | 02:35 | might need to have an application
already running before you attach a debugger.
| | 02:39 | In other words, you don't want to attach
the debugger at the Startup Process.
| | 02:42 | Or another thing that's happening is
you're running an application and five or six
| | 02:46 | hours after you started running it,
something happens and it doesn't look right,
| | 02:48 | and at that point, you want to start
attaching a debugger and taking a look.
| | 02:51 | So, what I am going to do is I am
going to run this AttachToConsole
| | 02:55 | application directly.
| | 02:57 | To do that, I am going to right-click,
choose Open Folder in Windows Explorer,
| | 03:03 | open my bin folder and my Debug
folder, and run this AttachToConsole.
| | 03:10 | Let me show you how it works.
| | 03:11 | I'll type in my name, and there
is the same code we saw before.
| | 03:16 | Now, it's sitting there waiting for my input.
| | 03:18 | I can go back to Visual Studio, go to
my Debug menu, and say attach to process.
| | 03:28 | This dialog shows me all of the
running processes on my computer.
| | 03:32 | I am going to look for one
called AttachToConsole.exe.
| | 03:38 | That's the one that I am currently
running. And then I am going to click Attach.
| | 03:49 | F9 put their breakpoint here, and then
I will go back here and press the Enter
| | 03:51 | key, which cause the next line of code
to run. And as you can see it now hit my
| | 03:55 | breakpoint. And now at this point, I
can do any of my normal debugging task.
| | 03:59 | I can take this yellow marker
and move it to a previous line,
| | 04:02 | I can examine the data in
the variables, and so on.
| | 04:07 | For my next demonstration, I want to
show you how to debug an ASP.NET web site.
| | 04:11 | So let me close this application by
clicking this Stop button, and then I am
| | 04:17 | going to go over to my solution and
right-click on the DebugWebApp and tell it
| | 04:21 | to make that my StartUp object.
| | 04:23 | Now, this is a ASP.NET application.
| | 04:27 | So, one of the things that you have
to do to make an ASP.NET application
| | 04:30 | debuggable is add a line
of code to your Web.config.
| | 04:36 | By default, Visual Studio does that
automatically for you when you create a web project.
| | 04:41 | If it doesn't have a debugging setting,
then it'll prompt you and say, would you
| | 04:43 | like to modify the Web.config?
| | 04:45 | So, I have already done
that on this application.
| | 04:48 | So all I should have to do is open up
this window here, and I'll show you the
| | 04:57 | bit of code I have here.
| | 04:58 | I have an asp:Button in here that has a
Button1_Click procedure. And if I press
| | 05:03 | F7, you can see the code behind.
| | 05:05 | This is the ASP page.
| | 05:07 | Here is the code behind for that, and
I have added a breakpoint on this line
| | 05:10 | here where I am getting the
DateTime and putting it in this Label.
| | 05:13 | I have also written a
JavaScript button down here.
| | 05:18 | Here is an input button. And it has an
onclick, which says, you need to run this
| | 05:23 | JavaScript called showMessage, which is
up here. And I have added a breakpoint in
| | 05:29 | my HTML for this JavaScript.
| | 05:31 | So, what this is showing is that I
can also debug client-side code in
| | 05:35 | ASP.NET applications.
| | 05:37 | So we have got two breakpoints, right?
| | 05:38 | I need to attach a debugger, so I
will choose Debug > Start Debugging.
| | 05:44 | Visual Studio will launch the ASP.NET
development server, and then it will
| | 05:50 | launch my web browser, and it attaches the
browser to this instance of Internet Explorer.
| | 05:57 | Now, when I click on this button,
you'll see that it switches me over to the
| | 06:01 | code behind, and I can hover over
the variables here. I can press F11.
| | 06:07 | I can rewind my code just like before.
| | 06:12 | Seeing my Output information here.
There is all my locals and watch windows all
| | 06:16 | work just the way you'd expect.
| | 06:17 | Now, I am going to press F5 to continue,
and now I am going to click on this
| | 06:21 | button here which has the JavaScript code.
And again, you'll see it's hitting my
| | 06:25 | breakpoint in my HTML page.
And again, I can hover over these items,
| | 06:32 | press F11 to step through the code, and so on.
| | 06:36 | Very handy and a welcome
addition to web development.
| | 06:39 | All in all, I'd say the Visual Studio
contains a nice selection to debugging tools.
| | 06:44 | In the next movie, I will show you the
IntelliTrace tool, which changes the way
| | 06:48 | you think about debugging.
| | Collapse this transcript |
| IntelliTrace historical debugging in Visual Studio Ultimate| 00:00 | Visual Studio has an amazing new
debugging feature called IntelliTrace.
| | 00:04 | This feature is only available in
the Ultimate edition of Visual Studio.
| | 00:08 | So if you don't have that version,
you will not be able to follow along
| | 00:11 | with this demonstration.
| | 00:13 | Now normally, I wouldn't show you
features from the Ultimate edition as we are
| | 00:17 | targeting the Premium edition for
this series, but this feature is very
| | 00:20 | interesting from a developer's
perspective, and I thought it was worth a look.
| | 00:24 | Also, let me add that testers within
your organization can use this feature if
| | 00:28 | they have the Microsoft
Test and Lab Manager tool.
| | 00:31 | So, what is IntelliTrace?
| | 00:34 | IntelliTrace acts like a debugging recorder.
| | 00:36 | While you are in a debugging
session, it is saving information about the
| | 00:40 | debugger state as you go.
| | 00:42 | Later, you can look back at
this historical data and replay the
| | 00:46 | previous debugging session.
| | 00:48 | To demonstrate this, I'm
going to use this program called
| | 00:50 | DebuggingWithIntellitrace. And I need
to set up a couple of settings inside
| | 00:56 | Visual Studio before I continue.
| | 00:57 | They are over here in Tools > Options,
and then in the IntelliTrace section.
| | 01:04 | First, let's look in the General area.
| | 01:07 | In Visual Studio, by default,
IntelliTrace is enabled.
| | 01:11 | Normally, it is set to this
IntelliTrace events only.
| | 01:13 | I've changed it to this second option down here.
| | 01:16 | If you change to the second option,
you're going to have to restart Visual Studio.
| | 01:19 | If you choose the first option, that
means tracing when you enter break mode and
| | 01:23 | storing individual steps while you're debugging.
| | 01:26 | If you choose the second option, you
will store information about each function
| | 01:30 | call, including the parameter
information and values returned from the call.
| | 01:35 | The second option we want
to look at is in Advanced.
| | 01:39 | This is the location where
my recordings are stored.
| | 01:43 | I'm going to copy this information,
and then I'm going to click OK.
| | 01:47 | I'm going to go over to my Windows Explorer,
and I'm going to navigate to that location.
| | 01:55 | You need to be cautious about storing
files here because they are often deleted
| | 01:59 | when you close Visual Studio.
| | 02:00 | You can see there's already one trace in here.
| | 02:03 | I'm going to come back here and
copy this trace in a few minutes.
| | 02:05 | Now, let's go do some debugging.
| | 02:07 | I'm going to switch to Visual Studio,
I'm going to open this Program.cs file,
| | 02:12 | and you see I have a normal
Visual Studio breakpoint in there.
| | 02:15 | I'm going to press F5 to debug my
application, I hit my breakpoint, and
| | 02:20 | immediately you see there's
something new happening inside Visual Studio.
| | 02:24 | Over here is a IntelliTrace replay toolbar.
| | 02:28 | So, what I can do is press F11 or F10
to step through a few lines of code, and
| | 02:33 | then I can click on this Go To
Previous Call and look at where I was a few
| | 02:39 | seconds ago in my debugging session.
| | 02:42 | And then I can return to the
Live mode by clicking here.
| | 02:44 | Now it's recording these
steps in that IntelliTrace file.
| | 02:47 | So let's record a few more steps.
| | 02:49 | I'm going to spawn another
worker thread to do some work.
| | 02:52 | I'm going to ask for your name,
and we're going to read the line.
| | 02:56 | It's going to prompt me for the name,
I'm going to type in "Joe", and I'll come
| | 03:01 | down here and decide that
I want to rerun that code.
| | 03:03 | So I'm going to grab this yellow marker
and drag it up and rerun those lines of code.
| | 03:08 | Again, I'm pressing F10 to rerun
those lines of code. And then I'm going to
| | 03:14 | press F5 to finish the application.
| | 03:16 | So, at this point, I have
a recording in that folder.
| | 03:19 | I'll go back and look in the folder.
| | 03:21 | Here is the file I just created, and I'm
going to copy this and paste it to my Desktop.
| | 03:28 | As you can see, I have a few copies here.
| | 03:30 | Now, here's where it becomes powerful.
| | 03:33 | Imagine for a second that you've gone
home, had a nice three-day weekend, you
| | 03:38 | come back to work, and there is
a bug that you need to look at.
| | 03:42 | It's in this application we've been
working on, this debugging with IntelliTrace.
| | 03:45 | So you load that in, and then you go
out to your hard drive and you take this
| | 03:50 | recording file and you
load it into Visual Studio.
| | 03:53 | I can just double-click on
this iTrace file to do that.
| | 03:59 | What you see is a summary of all the things
that were happening inside that application.
| | 04:03 | It shows that I had several threads
running, and then I can also get information
| | 04:09 | about the system that was under test.
| | 04:11 | Here is the system
information about this computer.
| | 04:13 | I can see I'm on a quad-core machine.
And then down here, I can find that
| | 04:18 | information about what was
loaded into the memory at the moment.
| | 04:22 | So there's two files down here:
| | 04:23 | mscorlib.dll and DebuggingWithIntellitrace.exe.
| | 04:27 | Now, let me show you where
the power really happens.
| | 04:30 | I can go into this Thread List and pick my
Main Thread and then choose Start Debugging.
| | 04:36 | Now realize, I haven't started a debugger yet.
| | 04:39 | I've just been sitting inside Visual Studio.
| | 04:41 | I'm going to click Start Debugging, and
it pulls my historical data out of that
| | 04:46 | file and shows what happened, the
last step in that debugging session.
| | 04:51 | Now, I'm going to load a new
window over here from the Debug menu.
| | 04:56 | It's called IntelliTrace > IntelliTrace Events.
| | 05:01 | This shows every step that was
recorded during that process.
| | 05:04 | So what I can do is double-click on one
of these steps to take me back to that
| | 05:08 | location, just a single click.
I can look at the Call Stack, the Local
| | 05:12 | variables, a lot of information about
what was happening as the tester or the
| | 05:17 | developer is walking through the code.
| | 05:19 | And you remember what I did earlier?
| | 05:20 | I repeated my steps.
| | 05:22 | I went here, and then I went back up to a
line of code and re-ran that line of code again.
| | 05:28 | So you can see that rewind happening there.
| | 05:31 | I can also hover over these variables
and see the data that was stored in there
| | 05:35 | historically, remember.
| | 05:36 | So, if I go back here, it's null.
| | 05:38 | I'll run this line of code and then
hover over this, and there is the previous
| | 05:43 | version of that variable.
| | 05:45 | This is really an amazing feature.
| | 05:47 | Can you imagine your testers filing a bug and
attaching the IntelliTrace file to that report?
| | 05:52 | Now you can run their trace on your
computer and see the state of every thread
| | 05:57 | running on their
computer at the time of failure.
| | 05:59 | This has to be my favorite debugging
feature, and I think its well worth
| | 06:03 | the investment.
| | Collapse this transcript |
|
|
9. Testing Your ApplicationUnderstanding Visual Studio editions and test tools| 00:01 | In this movie, I want to clarify which
testing tools are available within Visual Studio.
| | 00:06 | There are four versions of Visual
Studio that contain testing tools.
| | 00:09 | Those are 2010 Professional and Premium,
and also the Ultimate edition and the
| | 00:15 | Test Professional 2010.
| | 00:18 | Microsoft also provides an optional
server product, known as Team Foundation Server.
| | 00:22 | This server is the backbone of
many corporate development shops.
| | 00:25 | It serves as a repository for items
considered crucial for your project.
| | 00:29 | For example, it stores
test cases and bug reports,
| | 00:32 | also product work items.
| | 00:34 | It also contains source control repositories,
workflow generators, and progress reports.
| | 00:39 | Testers get their own dedicated
version of Visual Studio, called Test
| | 00:43 | Professional, sometimes called Test Manager.
| | 00:46 | Test Manager has to work in
tandem with Team Foundation Server.
| | 00:50 | It works closely with the items like
test cases, test plans, and environments
| | 00:53 | that are stored inside Team Foundation Server.
| | 00:57 | Test Manager contains a testing c enter
that is meant as a place for testers to
| | 01:00 | execute their tests and activities.
| | 01:02 | For example, team managers can do test
management and testers can execute their tests.
| | 01:09 | Test Manager also contains a Lab Center.
| | 01:11 | The purpose of the Lab Center is
to manage a virtual environment.
| | 01:14 | You can configure
different runtime environments.
| | 01:16 | Individual virtual machines can be used
to execute tests and retrieved later to
| | 01:20 | re-simulate a testing environment.
| | 01:23 | One cool thing about the Lab Center
is that a tester can save a virtual
| | 01:26 | machine with a bug report.
| | 01:28 | When the developer checks out the bug,
they can also check out the virtual machine.
| | 01:33 | I'd like to talk about a few of the tests
that are available inside Visual Studio.
| | 01:36 | First, I want to talk about unit testing.
| | 01:38 | This is available in all four of
the editions I've mentioned earlier.
| | 01:42 | A unit test is a way for a programmer to
verify that the code is performing as expected.
| | 01:47 | I will show examples of unit
testing later in this chapter.
| | 01:51 | In the Premium edition, you get code
coverage tests and a light version of the
| | 01:55 | UI testing framework.
| | 01:58 | And finally, in the Ultimate edition,
you get web performance testing and
| | 02:01 | load testing tools.
| | 02:03 | Also, you get the majority of the
tools available in Test Manager.
| | 02:07 | Overall, I'd say the testing tools provided
in Visual Studio are practical and useful.
| | 02:11 | Personally, I wish there were more testing
tools in the lower-priced versions of Visual Studio.
| | 02:17 | If you are on a budget, then consider
using open source testing tools instead.
| | Collapse this transcript |
| Verifying your code with unit tests| 00:00 | This chapter is about unit testing, which
is a very developer-centric type of test.
| | 00:04 | Let me start with my definition of a unit test.
| | 00:07 | A unit test is an automated chunk of
code that calls a class or a method and
| | 00:11 | verifies that our assumptions about the
behavior of the code under test are correct.
| | 00:16 | Unit tests are commonly written
using a unit testing framework.
| | 00:19 | They should always be
automated and easily accessible.
| | 00:22 | Anyone on your team should be able to
run the unit tests at anytime to verify
| | 00:26 | that the code under test is working,
and that no one has broken the build.
| | 00:30 | Let's look at the unit
testing tools inside Visual Studio.
| | 00:32 | I am going to be working with this
project called WorkingWithUnitTests.
| | 00:35 | First, let me show you
the code I am going to test.
| | 00:38 | It's inside this Book class
| | 00:40 | Double-click on the Book class and
you'll see that this class has one property
| | 00:44 | called Title, one property with a
getter and a setter, and a property called
| | 00:49 | Price, and then I have two methods:
| | 00:51 | UpdatePrizeByPercent and SaveBook.
| | 00:54 | And here is a question for you:
| | 00:55 | how do you know about the text in your code?
| | 00:58 | My general feeling is that you only need
to text code that contains logic statements.
| | 01:03 | So if your method has a loop construct, or an
If statement, or it contains a calculation,
| | 01:08 | it needs to be tested.
| | 01:09 | Not all code qualifies, however.
| | 01:11 | It's a waste of time to
test simple property getters.
| | 01:14 | So looking at this code up here, it
doesn't make any sense to test this because
| | 01:17 | there is no calculation going on.
| | 01:19 | I will right my first unit test by going
to the Test menu and choosing New Test.
| | 01:24 | Within this dialog, I need
to pick my testing language.
| | 01:28 | I will choose C#, and then I will
click on this Unit Test template.
| | 01:32 | Most often, you can write your unit
tests and the code at the same time.
| | 01:37 | There are many in the developer
community that advocate writing your test first
| | 01:40 | before writing any code.
| | 01:42 | This is known as test-driven development.
| | 01:44 | I agree with many of their principles,
but for this movie, I'm adding tests
| | 01:48 | to a prewritten code.
| | 01:49 | I am going to call my unit test
BookUnitTest, and then I am going to click OK.
| | 01:54 | Visual Studio realizes I do
not have a test project yet,
| | 01:57 | so it prompts me for name for my new project.
| | 02:01 | That looks like a great name
for a unit testing project.
| | 02:04 | I will click on Create and then on
OK on this dialog. Several things
| | 02:09 | happened in my project.
| | 02:11 | First of all, a new folder was added called
Solution Items, with a few test items in there.
| | 02:16 | Then a new UnitTest project was
added, including this C# file down
| | 02:21 | here, BookUnitTest.
| | 02:22 | Let's see what's inside here. Double-click.
| | 02:25 | Here is the class that is going to contain my
testing code, and the way we tell the unit testing
| | 02:29 | framework that this is a test class is
by putting this TestClass attribute on
| | 02:33 | the class itself. And then each method
that I want to write that is going to
| | 02:37 | test my code is going to be
marked with this TestMethods.
| | 02:41 | That's so that the unit testing
framework can find this method.
| | 02:44 | Let me show you another
way of adding a unit test.
| | 02:47 | I will use a wizard this time.
| | 02:49 | I'll click on this Unit Test
Wizard, verify it is going to go in a
| | 02:53 | UnitTest project, click OK and
then over here, I am going to save my
| | 02:58 | changes to my project.
| | 02:59 | I'm going to select the
class that I want to test.
| | 03:03 | Test this book class, and I only
need to test the SaveBook book and the
| | 03:07 | UpdatePriceByPercent methods.
| | 03:10 | So I will unselect
everything else and then click OK.
| | 03:14 | This time, it wrote a slightly better test class.
| | 03:17 | If you'll notice, my test methods now
say SaveBook class, and there is a little
| | 03:22 | bit of sample test code in here.
| | 03:23 | Now I am going to delete
these and put my own in here.
| | 03:26 | I have already prewritten
the code for these tests.
| | 03:33 | They are in this Assets folder.
| | 03:35 | I am going to first start by
using this IncreasePrice.txt file.
| | 03:39 | Double-click on this one and copy all of
this code and then paste it in BookTest.cs.
| | 03:46 | I think I just double-clicked on the title.
| | 03:48 | I detached this from the window.
| | 03:51 | The way you put it back is you
Ctrl+double-click on the header.
| | 03:54 | I will paste my code in.
| | 03:57 | Here is my TestMethod attribute, and
the way I like to name my tests is by using
| | 04:01 | the name of the method that I am going
to test, the scenario that I am testing,
| | 04:06 | and the expected behavior.
| | 04:07 | It makes for a long name, but any
developer that's looking at this test knows
| | 04:11 | what it's expected to do.
| | 04:13 | Now, what am I doing in my test case?
| | 04:14 | I am instantiating an
instance of that class, the Book class.
| | 04:17 | I am trying to verify that this
UpdatePriceByPercent function works the way I
| | 04:23 | want, which is I pass in a
value, and it updates the price.
| | 04:26 | So first, I am going to set a price
for the book, then I am going to call my
| | 04:30 | UpdatePriceByPercent, and then I am
going to use one of the test classes, which
| | 04:34 | is called Assert.
| | 04:35 | I am going to use that to verify that
the results are what I expect them to be.
| | 04:39 | There are a number of Assert methods.
| | 04:41 | If you look at the Assert class itself, you
can see there are lots of test methods here:
| | 04:47 | AreSame, there is a method called IsFalse,
another one down here called IsNull, IsNotNull.
| | 04:55 | I have picked one called AreEqual, and
the syntax for this is you pass in the
| | 05:01 | expected value and then you pass in the
actual value, and the Assert will verify
| | 05:06 | whether these are correct or not.
| | 05:08 | So what am I doing?
| | 05:09 | I'm asserting that I should have 10
times 0.05, and then I'm saying, now look at
| | 05:16 | the Book.cs and see if it has that value.
| | 05:18 | Let's run the test.
| | 05:20 | To run the test, I will go to the Test
menu, choose this Windows submenu, and
| | 05:25 | choose Test List Editor. I'll then build
my project, which will build my test and
| | 05:31 | my regular project, and it shows two
test methods. The one that I just wrote
| | 05:35 | down here is the UpdatePriceByPercent.
| | 05:37 | I will put a check mark here, and
then I'll come up and run checked tests.
| | 05:42 | At the bottom-half of the screen
you'll see that there is a Passed result.
| | 05:46 | It says Test run is complete.
| | 05:48 | I had made one test. One test passed.
| | 05:51 | You want to have all green lights down here.
| | 05:53 | Now what happens if a developer comes along
and goes to this book class and make a mistake.
| | 05:58 | They look at this code and say, I am
going to refactor this--maybe I should
| | 06:02 | have been dividing.
| | 06:03 | So they make this change, and
they compile the application.
| | 06:06 | I did a Ctrl+Shift+B to
compile the application this time.
| | 06:10 | Then when you run the unit test the
next time--I will go over here and do
| | 06:13 | Refresh--and then when I run my unit
test, that developer immediately sees that
| | 06:19 | change they made 10 minutes ago is
causing one of our unit tests to fail.
| | 06:23 | So they are going to back and look
at me and say, what did I do to break the build?
| | 06:27 | Let's go back and fix that code because
I want all green lights in a few minutes.
| | 06:31 | Go back over here.
| | 06:31 | Let me switch this back to multiple.
| | 06:34 | And I have got two more text I want to
add to BookTest, and what I am verifying
| | 06:38 | this time is that the SaveBook
method is working the way I expect.
| | 06:42 | So look what happens if I try to save the book.
| | 06:44 | I look at the price that's
currently stored in the Book class and if it's
| | 06:47 | less then zero, then I throw
an ArgumentOutOfRangeExeption.
| | 06:51 | And if the price is larger
than 90, I throw the same ArgumentOutOfRangeExeption.
| | 06:56 | So when I write my test cases, I
need to make sure that if you pass in a
| | 07:00 | negative value, it throws that exception.
| | 07:03 | So let's go to SaveBook1, and copy this code.
| | 07:06 | I will go to my BookTest class and
paste it, and then I might as well bring
| | 07:12 | over the other bit of code while I am at it.
| | 07:14 | I will go over here and get the
SaveBook2 and copy this code and paste it in
| | 07:22 | BookTest. Then I will build the project,
and then I will step through the code and
| | 07:28 | show you what's it doing.
| | 07:31 | In this first test, I am instantiating
the book, I'm setting a negative price,
| | 07:36 | and then I'm attempting to save the book.
| | 07:38 | In the second test, I'm doing exactly
the same thing, only I am setting an $80
| | 07:42 | price and saving the book.
| | 07:44 | Now, I expect this method to throw an
exception, so I put this attribute up here
| | 07:48 | to tell the testing harness,
it's okay if it throws an exception.
| | 07:52 | In fact, I expect it to throw
exception, and this is the exception I
| | 07:54 | expect: ArgumentOutOfRange.
| | 07:56 | Now we will go test our code.
| | 07:58 | Switch over to the test editor.
| | 08:00 | You see there is two new unit tests listed here.
| | 08:03 | I'll check both of those and then run
my tests. And down at the bottom of the
| | 08:07 | screen, you see that the
UpdatePriceByPercent passed, the negative one passed
| | 08:12 | because it threw that exception, but this
one is not throwing any exception, and why not?
| | 08:17 | Let's go and look at my test code.
Because I didn't pass any number that
| | 08:21 | was higher than 90.
| | 08:23 | So if I change this value to 90.01,
rebuild my application, switch over to the Test
| | 08:33 | editor and run my tests
again, I get all green lights.
| | 08:37 | This is what you're looking for.
| | 08:39 | Every time you write code, you write a
unit tests, you write the conditions
| | 08:42 | that you want to test, and then you
run your tests every time you make a
| | 08:46 | change in your code.
| | 08:47 | Make sure you haven't broken
any of your pre-existing tests.
| | 08:50 | Now there are other asserted
conditions they should explore when you have
| | 08:52 | time, and in the next movie, I'll
show you another type of test called
| | 08:56 | performance testing.
| | Collapse this transcript |
| Running performance and load tests| 00:00 | I've found that many developers are
not aware that you can set up and run
| | 00:03 | complex web load tests for an ASP.NET Application.
| | 00:07 | You can do this right from within Visual
Studio, as long as you have the Ultimate edition.
| | 00:12 | If you don't have the Ultimate
version, you won't be able to follow along
| | 00:15 | with this demonstration.
| | 00:16 | I'm going to create two types of tests:
| | 00:18 | a performance test which is intended
to determine how fast some aspect of a
| | 00:23 | system performs under workload,
| | 00:25 | then I'll create a load test which is a
simulated real-world test that exercises
| | 00:30 | the web site by running the
designated performance tests.
| | 00:33 | I'm going to use his project called
WebsiteToLoadTest to write my tests against.
| | 00:38 | This is just a simple ASP.NET site.
| | 00:41 | Now before you run the test, make
sure you are running Visual Studio as an
| | 00:44 | administrator; otherwise you will
not be able to perform the load tests.
| | 00:48 | Next, I'm going to run my application.
| | 00:52 | Since I'm running on a development
server, I need the copy the port number from
| | 00:56 | my local host before I create my web tests.
| | 00:59 | So I'll copy this, and then
I'll close that browser window.
| | 01:03 | Then I'm going to go to my Test
menu and create a brand-new test.
| | 01:07 | I'm starting by creating a web performance test.
| | 01:09 | I'm going to call this one GoToAsp.
| | 01:11 | I'll pick a language, C#--although it
doesn't matter for this example--and
| | 01:19 | then I'll click OK.
| | 01:19 | Visual Studio realizes I
haven't created a test project yet.
| | 01:23 | I'll call this one BasicLoadTests and then
click Create and then OK to this dialog.
| | 01:31 | A number of things has happened.
| | 01:33 | If you look in the Solution Explorer,
you'll see there is a Solution folder has
| | 01:36 | been created, also a new
GoToAsp.webtest file has been created, and then Visual
| | 01:42 | Studio has launched this version of
Internet Explorer with this special plug-in
| | 01:47 | running called the Web Test Recorder.
| | 01:50 | What I'm going to do is I'm going to
paste that URL in that I copied a few
| | 01:53 | seconds ago. And now I'm going to go back and
restart my web server because it's not running.
| | 01:57 | So we will go back over here and choose
Debug > Start Debugging. That looks better.
| | 02:07 | So now what I'll do is I
step through the test steps.
| | 02:10 | The user is going to click on this
link, which takes him to another site, and
| | 02:15 | they're going to click on the Back button.
| | 02:16 | Now, I'm done recording the test,
| | 02:18 | so I'll click Stop.
| | 02:22 | At this point, you'll see a dialog.
| | 02:24 | You might have to wait a few
seconds for this dialog to disappear.
| | 02:27 | Next, I'm going to create a second
web test by going to Test > New Test >
| | 02:33 | Web Performance Tests.
| | 02:34 | I'm going to call these one
FailedLogin, place it in the same test project,
| | 02:41 | verify that my browser is
still running. It's not,
| | 02:44 | so I'm going to have start my browser.
And what the user is going to do this time
| | 02:58 | is they are going to
click on this Log In button.
| | 03:02 | I'll type in their name and a
bad password, and click Log In.
| | 03:07 | It fails and then the user
goes back to the homepage.
| | 03:13 | That's my next test.
| | 03:14 | I'll choose Stop, and save.
| | 03:17 | Now I've got my two performance tests.
| | 03:19 | I'm going to create a load test.
| | 03:20 | Remember that a load test
simulates workload on the site.
| | 03:23 | Now I'm going to go up to
Test > New Test > Load Test.
| | 03:28 | I will go ahead and leave it as the
default name, and then click on OK.
| | 03:32 | Since I'm simulating users coming to
this site, I am going to go through the
| | 03:35 | Scenario system here.
| | 03:36 | First, it's going to ask me to
give the name for this scenario.
| | 03:39 | I'm going to pick the defaults.
| | 03:41 | I'm going to suggest that we
have 25 users exercising the site.
| | 03:45 | This is changeable, naturally.
| | 03:47 | I'm going to skip over this one, and
then I'm going to come in here and add
| | 03:50 | those two tests I just created.
| | 03:52 | I'm going to add the FailedLogin and then
GoToASP, and then I'll click on OK, and then I click on Next.
| | 04:01 | Here I can choose how to simulate
what type of network connections our users
| | 04:05 | are connecting with.
| | 04:07 | So I can come here and choose Add, and
say that some of our users are coming in
| | 04:10 | on a 56K modem--about 10% are using
that--and then I'll click on Add and have
| | 04:17 | other network settings here.
| | 04:18 | Now I don't want a actually keep these
items in here, because if I do, it's
| | 04:22 | going to install some extra network
emulators which I don't want to wait for
| | 04:25 | that process to happen.
| | 04:26 | So I'm going to choose Remove,
and Remove, and then click on Next.
| | 04:31 | Here, I can pick the browser mix.
| | 04:33 | Again, I go to the Add button, and
say that we've got some people coming to
| | 04:37 | you with Firefox 3.
| | 04:39 | 20% of our users are using Firefox 3, and 5%
of our users are using Chrome too, and so on.
| | 04:46 | So I pick these mixes, and I'm going to
remove these for testing purposes today,
| | 04:50 | and then I click on Next.
| | 04:54 | This one allows me to add other
computers to run against the server.
| | 04:58 | I'm just going to use my single computer
today, And then here, I'm going to pick
| | 05:01 | the test duration time.
| | 05:03 | I don't want this to be too long,
| | 05:05 | so I am just going to type in
30 seconds for the test duration.
| | 05:08 | I will click on Finish, and
now I'm ready to run my test.
| | 05:12 | I think before that, I
should build my application.
| | 05:14 | I will choose Build > Build Solution,
and then I'll go to my Test > Window and
| | 05:21 | choose Test List Editor.
| | 05:22 | And notice I have three tests.
| | 05:23 | I am only going to run the LoadTest1.
| | 05:26 | I don't need to run these two
individual ones because they are going to be the
| | 05:28 | part of the LoadTest.
| | 05:29 | I also need to verify that my web site is running.
| | 05:32 | I will switch over here,
| | 05:34 | go to my Test editor and run the test.
| | 05:39 | After waiting 30 seconds, the test completes.
| | 05:41 | I can then click on this
hyperlink here to see my test results.
| | 05:46 | I see that my test passed.
| | 05:47 | I can also right-click here and choose
View Test Results Details and see a more
| | 05:53 | detailed result screen.
| | 05:54 | Here, I can see a summary, when the
test was run, how long it took, what were
| | 05:59 | my slowest pages, and then I can look at a set
of graphs, a set of detailed table information.
| | 06:04 | I see that I had 13 total test run on
FailedLogIn and 28 tests were run on
| | 06:09 | GoToASP and how long
each one of those test took.
| | 06:13 | Quite a bit of details here about
what's happening on your web site.
| | 06:16 | There is more to learn about
performance and load testing. For example, you
| | 06:19 | should never run the load test
from your developer computer.
| | 06:22 | Instead, you should set up a
testing ring and have it test the site.
| | 06:25 | This enables you to scale your test
to simulate a realistic runtime load.
| | 06:29 | You can also create simulated users, or
use data-driven tests to simulate users'
| | 06:33 | search strings and other complex test scenarios.
| | Collapse this transcript |
|
|
10. Deploying Your ApplicationBuilding your application| 00:00 | Before you can deploy your application,
you have to create an executable file.
| | 00:04 | This is done by compiling your application.
| | 00:06 | Simply compiling your code is rarely
enough with modern applications, however.
| | 00:10 | There may be many operations you must
perform to get the source ready for the
| | 00:13 | compile and tasks that need to be
run after the compile is finished.
| | 00:17 | There are tools available to help this process.
| | 00:20 | They are known as automated build tools.
| | 00:22 | Visual Studio contains a build tool.
| | 00:24 | You use it every time you compile,
run, or debug your application from
| | 00:28 | within Visual Studio.
| | 00:29 | It goes by the name of MSBuild.
| | 00:32 | I'll show you how to customize your
build process with Visual Studio, but first,
| | 00:36 | let's look at the basic build tools.
| | 00:38 | I am inside Visual Studio, and
I'm going to create a new project:
| | 00:41 | File > New > Project.
| | 00:43 | I'm going to go to our movie chapter,
which is out on our Desktop, and then I am
| | 00:53 | going to create a C#
console application. There it is.
| | 00:57 | I'll just except the default names and click OK.
| | 01:00 | Every time you press F5 to debug your
application, you are actually compiling
| | 01:04 | the application and then attaching a debugger.
| | 01:07 | You can see the build process here in this menu.
| | 01:10 | There are two parts of this menu:
| | 01:12 | The top part lists the
builds items for this solution.
| | 01:15 | The bottom half selects the build
items for the currently selected project.
| | 01:20 | So right now, since I only have one
project, choosing to build the console
| | 01:23 | application, or build the solution,
would accomplish the same thing.
| | 01:26 | I'll click Build Solution.
| | 01:28 | It goes through the build process, and
when you're done, I now have a compiled
| | 01:31 | executable somewhere in my hard drive.
| | 01:33 | If I add a second project by right-
clicking on the solution and choosing Add >
| | 01:39 | New Project--let's say, for this
example, I want to create a Class Library,
| | 01:44 | I'll call this TextLibrary and then click OK--
| | 01:48 | now when I choose Build > Build Solution is
going to compile two different executables.
| | 01:54 | How do we know what build
order those items will run?
| | 01:58 | You can control the build order by
going to your Project menu and choosing
| | 02:03 | Project Build Order.
| | 02:04 | Here you can see that
ConsoleApplication1 is going to be built first, and then
| | 02:09 | TextLibrary is going to be built second.
| | 02:11 | I'm thinking that's the wrong direction.
| | 02:13 | I'd like to have my library built first,
and then once that's finished, I can
| | 02:17 | then use to code this in that
from the Console Application.
| | 02:20 | So to change the build order, I can
click on this Dependencies tab and say that
| | 02:24 | the Console Application
depends on the Text Library.
| | 02:29 | Now, if I go back to the Build Order
screen, you'll see that it's reversed the order.
| | 02:33 | Now, Text library will be built first, and
then Console Application will be built second.
| | 02:37 | There are two major types of builds
you can do in an application, and they're
| | 02:40 | listed in this configuration dropdown.
| | 02:43 | You can see one called
Debug and one called Release.
| | 02:46 | Right now, I'm creating a Debug build.
| | 02:49 | So the results of that are
going to go in the Debug folder.
| | 02:51 | Let me show you where that's at.
| | 02:52 | I am going to click on this Show All
Files button, I am going to open this
| | 02:56 | bin folder, and right now you can see that my
ConsoleApplication1.exe is in this Debug folder.
| | 03:03 | If I switch over to Release build and
then compile my project, you'll see that
| | 03:12 | it adds a Release folder, and it
makes a copy of the release version of my
| | 03:16 | executable in this folder.
| | 03:18 | In a real application, you may have even
more complex build requirements than this.
| | 03:23 | You may need to have a test build.
| | 03:24 | In that case, you can go to the
Configuration Manager and add other builds in here.
| | 03:30 | For instance, I can come down here and
say that I have a new test configuration.
| | 03:36 | I say that in the Debug build, you're
going to compile the ConsoleApplication1
| | 03:41 | with the Debug configuration. And in the
Release mode, you're going to build the
| | 03:45 | ConsoleApplication1 with
the Release configuration.
| | 03:48 | And in the Test build, you're going
to build the ConsoleApplication1 with
| | 03:53 | the Test configuration.
| | 03:54 | Now, when I come up here and I do a
build, you'll see that I end up with another
| | 04:02 | folder over here, Test, and
there is my ConsoleApplication1.
| | 04:05 | Now this movie is just covering the
basics of building your application.
| | 04:09 | If you watch the build customization
movie next, you'll learn how to adapt the
| | 04:13 | build scripts to your own needs and
how to change which folders the physical
| | 04:17 | files are stored in.
| | Collapse this transcript |
| Customizing the build process with MSBuild| 00:00 | In many circumstances, you can use the
default build options for your project
| | 00:04 | and have a successful build.
| | 00:05 | There are times, however, when you need
to adapt the build script to your needs.
| | 00:09 | I'm going to work with this
project called CustomBuildProcess.
| | 00:12 | It has a simple WPF application, and
I'm going to choose several different
| | 00:17 | compile options for this application.
| | 00:19 | I am going to start by editing a
few of my Tools > Options settings.
| | 00:22 | Let's open this up.
| | 00:23 | We are going to look in Projects and
Solutions and pick the General section.
| | 00:27 | Whenever you do a build inside Visual
Studio, it can output the results of the
| | 00:31 | build process to this output window.
| | 00:33 | So, I make sure that this is checked:
| | 00:35 | Show Output window when build starts.
| | 00:38 | I can also control how much information
is dumped into that window by clicking
| | 00:42 | on Build and Run and
choosing MSBuild output verbosity.
| | 00:47 | By default, I believe at Minimal.
| | 00:49 | You can pick Diagnostic, which gives
you a huge amount of information, or you
| | 00:53 | can pick Quiet, which gives you virtually none.
| | 00:56 | So I'll choose this one and then click OK.
| | 00:59 | Now, let me do a build, Ctrl+
Shift+B, and there is my output.
| | 01:03 | Let's try it again.
| | 01:05 | Choose Tools > Options.
| | 01:07 | This time I'll choose Diagnostic, and do
a Ctrl+Shift+B, and now you see, I have
| | 01:12 | got several thousand lines of code,
including information about the computer I'm
| | 01:16 | running on and the
environment and many, many details.
| | 01:19 | Let me set that back to the
original settings. Minimal.
| | 01:24 | Now I have some control over the build
process by going to Properties and then
| | 01:30 | clicking this Build section.
| | 01:32 | I am going to autohide his
toolbox for a few minutes too.
| | 01:35 | Here I can control what
happens when I do a debug build.
| | 01:39 | I can control all the settings and
even the Output folder down here.
| | 01:43 | Also, this is where I can generate
my XML documentation for that folder.
| | 01:47 | If I switch to the Release build, now
I am controlling what happens when I
| | 01:51 | choose the release build from this
dropdown. And in this case, I'm saying to save
| | 01:56 | the files to a different location.
| | 01:58 | We can also customize the Build Events,
but before I get to this screen, let me
| | 02:01 | show you a few other options.
| | 02:03 | MSBuild.exe is the tool responsible for
building your application, and it relies
| | 02:07 | on a XML script for its instructions.
| | 02:09 | Now here's a little secret:
| | 02:12 | the build script instructions are
stored in an XML file, and they're hiding in
| | 02:15 | plain sight in your project folder.
| | 02:17 | Let me show you where they are.
| | 02:19 | If I were to look at the
project folder for this application by
| | 02:22 | right-clicking on this and choosing
Open Folder in Windows Explorer, there is
| | 02:26 | a file here called csproj.
| | 02:28 | This is the instruction to Visual
Studio of what to load when you open this
| | 02:32 | project within Visual Studio.
| | 02:34 | But also inside this are the
build instructions for MSBuild.
| | 02:38 | So if I right-click on this and
choose Edit with some editor, like Notepad++,
| | 02:42 | which is the free tool we have on our
computer, you'll see that this is nothing
| | 02:46 | more than an XML file.
| | 02:48 | Now I can open this file inside
Visual Studio if I know the secret.
| | 02:53 | Return back to Visual Studio. I right-click
on this CustomBuild project and choose Unload.
| | 02:59 | Now all the files that were part of that project
are no longer usable inside Visual Studio.
| | 03:04 | Next, I right-click and I
choose EditCustomBuildProcess.
| | 03:09 | There is that file, but now we are
looking at it as the text file as opposed to
| | 03:12 | the loading it in the Solution Explorer.
| | 03:15 | If you look through this, you'll see
that it's just settings for the build
| | 03:18 | engine, where to output the
Release build, and it's right here.
| | 03:22 | That's the Output Path.
| | 03:23 | That's the same thing I was
setting just a few minutes ago in
| | 03:26 | the Options dialog.
| | 03:27 | Here is all the references to add
for the DLLs, and then down here are the
| | 03:32 | individual files that need to be
compiled. It needs to compile a file
| | 03:35 | called App.xaml.cs.
| | 03:37 | There are also other areas.
| | 03:40 | There is PreBuildEvent and
PostBuildEvent. And if you want, you can even create
| | 03:44 | your own custom build tasks
and add them to this list.
| | 03:47 | I am going to show you how to do a
PreBuildEvent and a PostBuildEvent.
| | 03:51 | To do that, I'm going to close
this window, then right-click on
| | 03:55 | CustomBuildProject and choose to
reload it back into Visual Studio.
| | 03:59 | Next, I am going to double-click on the
Properties section, and I am going to go
| | 04:03 | to this Build Events tab.
| | 04:05 | So let's show you what
happens when you do a compile.
| | 04:07 | Any instructions that are
inside this section are run first.
| | 04:11 | Virtually anything that you can run at a DOS
command prompt you can run inside this box.
| | 04:16 | Then it'll go through the build
instructions that are part of the csproj file I
| | 04:20 | just showed you, and then when that's
done, it will go out and run any other
| | 04:23 | command you tell it in
the Post-build command line.
| | 04:26 | Now, I am going to start here
by creating a brand-new folder.
| | 04:28 | I am going to do a make directory .exe.
| | 04:31 | I am going to call this directory buildDemo.
| | 04:34 | I would like to make sure
that this is a fresh directory,
| | 04:37 | so I am going up one line above
that and say I would like to remove the
| | 04:39 | directory buildDemo.
| | 04:43 | So I'll remove the directory,
then recreate the directory.
| | 04:45 | Let's see if this is working.
| | 04:46 | I'll do a save, and then I'll do a build,
and then we'll go and look in our hard
| | 04:53 | drive, look in the bin folder.
| | 04:57 | I was in a debug build, so I'll
choose the Debug folder, and there is
| | 05:01 | my buildDemo folder.
| | 05:03 | Next, I am going to copy
a file into that folder.
| | 05:06 | So let me show you how we do that.
| | 05:07 | I go to the PostBuildEvent
and what I want to do is take this
| | 05:10 | App.config.test file, I want to make
a copy of it, rename it and put it in
| | 05:16 | that new folder I just created.
| | 05:18 | I can find that information about my
application and put them in this Post-build
| | 05:22 | event by clicking on this Edit Post-
Build button and then clicking on Macros,
| | 05:26 | and this shows me things like output directory.
| | 05:29 | That's how I put the output directory
into my script, by putting a dollar sign
| | 05:34 | and then this special keyword here.
| | 05:35 | So let me show you the text I'm going to run.
| | 05:37 | I'll go to Assets. I'll open this
AfterBuild text file. I'll copy this and then
| | 05:45 | paste that right in here.
| | 05:47 | So this says, "Go out to the DOS command."
| | 05:50 | We call the command prompt now, right?
| | 05:51 | "We are going to find the project
directory and get this file, App.config.test,
| | 05:57 | and then we are going to make a copy
of it, and we are going to put it in the
| | 05:59 | target directory--the output
directory--and then in the new folder I just
| | 06:02 | created, buildDemo, and then
use this token to rename the file."
| | 06:06 | Let's see if it works.
| | 06:08 | Right-click here and choose Open Folder
in Explorer, open the bin folder, open
| | 06:15 | the Debug folder, drill down into
there, and there, I was successful.
| | 06:19 | I have a CustomBuildProcess.config file.
| | 06:22 | There's more I could do with these events.
| | 06:24 | I could build a batch file, for example.
| | 06:26 | The build engine is customizable too.
| | 06:28 | You can create your own build tasks in .NET
code and then add them to the build script.
| | 06:33 | Be sure and check out more details in MSDN help.
| | Collapse this transcript |
| Setting assembly information| 00:00 | It's important to include certain
information inside your finished assembly.
| | 00:04 | At the minimum, you need to include a
version number of your application and some
| | 00:07 | information about your company.
| | 00:09 | You can embed this information inside
your code, and you can also instruct the
| | 00:12 | build engine to modify the EXE properties.
| | 00:15 | I'm inside Visual Studio, and
I'm going to create a new project.
| | 00:18 | I'll choose File > New > Project,
and then I'm going to pick C# >
| | 00:21 | Console Application.
| | 00:22 | I will put this out in the correct
movie folder, and then I'll click OK.
| | 00:30 | To add assembly information, all I
need to do is go to this property window,
| | 00:34 | double-click on this node right here,
and then in C# projects I click on the
| | 00:38 | Assembly Information button.
| | 00:39 | It's the same in Visual Basic;
the button is just located in a
| | 00:42 | slightly different location--
| | 00:44 | it's down near the bottom of the page.
| | 00:45 | Here I pick the name of my application,
a description, our company name, our
| | 00:56 | product name, which I think is going to
be the same as my SuperFile title, but
| | 01:00 | maybe I might call this one v2, and
then down here I'll say Copyright Lynda.
| | 01:07 | Then over here we'll
change our assembly version.
| | 01:09 | I can also change the
file version in there well.
| | 01:12 | There are some other items added down here.
| | 01:14 | I'm going to leave those at the default,
and then I am going to click on OK.
| | 01:18 | Now I've to choose the name of my output EXE,
| | 01:21 | I need to come up to this
section and type in a new name.
| | 01:24 | So here, I don't want to
call this ConsoleApplication;
| | 01:26 | I want to call this one
SuperFileSaver, or something like that.
| | 01:29 | Now I'm ready to build my
application, and we will go and see if that
| | 01:33 | information is now embedded in my file.
| | 01:35 | I'll right-click here, choose to Open
the Folder in Windows Explorer, find my
| | 01:39 | executable, which is hiding here
in my Debug folder. There it is.
| | 01:42 | See, it's got the correct file name and
when I hover over it, I see that this is
| | 01:47 | Lynda.com, and it's version 2.0.
| | 01:48 | If I want to see more information, I
can right-click choose Properties >
| | 01:54 | Details and here I can see all of that
embedded information, including the File
| | 02:00 | version is also in there.
| | 02:01 | That's about all I have.
| | 02:02 | So I need to close this.
| | 02:03 | Now that you've embedded your
information in the assembly,
| | 02:06 | it's time to think about deploying
the executable to the user's computer.
| | 02:09 | That's the electrifying
topic for the next movie.
| | Collapse this transcript |
| Deploying a basic Windows application| 00:00 | The simplest way to deploy your .NET
Windows application is to copy the files
| | 00:04 | to a folder on the user's computer.
| | 00:07 | Visual Studio plays no role in this
deployment, other than compiling the finished
| | 00:10 | executable. But let me show you this
basic deployment anyway as it sets the
| | 00:14 | stage for other movies in this chapter.
| | 00:15 | For this movie, I'm going to be using
an application called PixelSmithDesktop.
| | 00:19 | This is a simple WPF application.
| | 00:22 | Let me show you what it looks like.
| | 00:23 | I'll run the application.
| | 00:25 | It has a series of buttons on
the left side of the screen.
| | 00:28 | When I click the Add Ellipse button, it
adds an ellipse to the designer surface.
| | 00:32 | Then I can select a different color and
add another ellipse or add a rectangle,
| | 00:37 | and I can even add an image.
| | 00:39 | So it's relatively easy to build this in WPF.
| | 00:42 | The drag and drop behavior comes
from a behavior that's part of the
| | 00:46 | Expression Blend library.
| | 00:48 | So in my applications folder, if you
were to look in the compile folder--
| | 00:53 | for instance, in the debug folder--
you'll see that I have a reference to this
| | 00:56 | Expression Blend and the
System.Windows.Interactivity.dll.
| | 00:59 | Just make sure you have those DLLs in
your folder when you compile the application.
| | 01:04 | The first thing I need to do to make
my application ready to release is to
| | 01:07 | switch over to this release build by
clicking in this dropdown and choosing
| | 01:11 | Release and then building my application.
| | 01:13 | Now here is how easy it is
to deploy the application.
| | 01:16 | I go out to my hard drive and make a
copy of the Release folder, and then I put
| | 01:20 | it wherever I want to install the application.
| | 01:23 | Let's say I want to install it to my desktop.
| | 01:25 | So I'll go over here, I'll
right-click, and choose Paste.
| | 01:28 | Now, the user should be able to go in
to this application and double-click on
| | 01:32 | PixelSmithDesktop, and
there is a running application.
| | 01:36 | They should be able to rename the
folder and re-run the application.
| | 01:43 | They should be able to move that
folder anywhere on their hardware where they
| | 01:46 | have permission to put a copy of a
folder and still run the application.
| | 01:50 | So that's really how simple it
is to deploy a .NET application.
| | 01:53 | The deployment story gets more
complicated if you want to install shared files.
| | 01:57 | Those shared files need to go in
the .NET Global Assembly Cache.
| | 02:01 | For that you need an installer application.
| | 02:03 | If you have a web application, you have
to upload the files to then web server
| | 02:07 | and then configure the server.
| | 02:09 | If you have a Windows application that
needs auto-update, you should consider
| | 02:13 | creating a ClickOnce application.
| | 02:15 | Coincidentally, there are movies
about each of these topics later in
| | 02:18 | this chapter.
| | Collapse this transcript |
| Creating an installer with Visual Studio| 00:00 | Installing a production application
on a local computer is more than just
| | 00:04 | copying files to the Program Files folder.
| | 00:06 | You might need to check for
dependencies and verify that a previous licensed
| | 00:10 | version is available before continuing.
| | 00:12 | You may need to remove certain
files before installing the new version.
| | 00:15 | It is a common practice to create
items in the Start menu and copy help and
| | 00:19 | documentation files to the computer.
| | 00:21 | If you need to perform any of these
tasks, you must create a setup program.
| | 00:25 | The Windows Installer API exists
to assist us in these scenarios.
| | 00:28 | Visual Studio contains an installer
project that creates Windows Installer files
| | 00:33 | for your application.
| | 00:34 | For this demonstration, I'm going to use
a solution called InstallerDeploy that
| | 00:38 | contains the application that I want
to install, which is PixelSmithDesktop.
| | 00:42 | This is that WPF application we've
seen in other movies in this chapter.
| | 00:45 | I'm going to start by
adding an installer project.
| | 00:48 | I'll right-click on my
Solution and choose Add > New Project.
| | 00:53 | Then I'll move down to the Other Project
section and choose Setup and Deployment.
| | 00:58 | There are two setup projects available here:
InstallShield LE and Visual Studio Installer.
| | 01:03 | This is a third-party application
and it's an add-in to Visual Studio.
| | 01:06 | I'm going to choose the Visual
Studio Installer, and then I'm going to
| | 01:10 | choose the Setup Wizard.
| | 01:12 | I'll give this a better name.
| | 01:13 | I'll call this one
PixelSmithInstaller and then click the OK button.
| | 01:20 | Since this is a wizard, it'll
start walking me through these
| | 01:22 | questionnaires. Welcome. Thank you.
| | 01:25 | What kind of application setup do you want?
| | 01:28 | Is this for a Windows or web application?
| | 01:30 | This is for a Windows application.
| | 01:32 | I don't need any of the special packages,
| | 01:34 | so I'll click on Next.
| | 01:35 | Here I'm picking the files that
are coming out of my application.
| | 01:40 | This indicates that I want all of the
files necessary to run the application:
| | 01:44 | the Primary output, EXE, and DLL.
| | 01:47 | This would be the source code, like the C# files.
| | 01:50 | Obviously, I don't need that from my end users.
| | 01:52 | This is for my debugging symbols. I
don't need that, but I do need content files
| | 01:56 | and documentation files if I have those.
| | 01:59 | So I'll click Next.
| | 02:00 | Here I can add additional files. I'll
click on the Add button, and then in this
| | 02:05 | Other Files folder, you'll
find a number of extra files.
| | 02:08 | There is a Windows movie in here.
| | 02:10 | There is a help file, there is a PDF,
and a document file, and an RTF.
| | 02:15 | I'm going to select all of these
and then click Open and then Next.
| | 02:20 | At this point, I'm done.
I'll click on the Finish button.
| | 02:23 | As you can see, a new project
was added to my application.
| | 02:26 | Now let me show you what happens
when you go to build your application.
| | 02:29 | If I go up here and I choose Build
Solution, that normally would build all of
| | 02:33 | the projects that are inside my
solution. But as you can see from my Output
| | 02:37 | window, it only built my main
application; it did not build the installer.
| | 02:42 | I think this is because you don't
create installers until the end of
| | 02:45 | your project cycle.
| | 02:46 | Since installers can take a long time to
build, the default mode is to not build
| | 02:50 | this file when you choose to build the solution.
| | 02:53 | Obviously, I do need to build it, so
the way I can build the application is to
| | 02:57 | right-click and choose Build.
| | 02:59 | At this point, the installer is ready to run.
| | 03:01 | I can right-click here and choose
Install. And later when the application is
| | 03:06 | installed, I can choose
Uninstall from this location.
| | 03:08 | I'll start the install process by clicking here.
| | 03:11 | It's got the wrong name up
here, PixelSmithsInstaller.
| | 03:14 | It should say PixelSmithDesktop.
| | 03:16 | So I need to fix that.
| | 03:17 | So let me cancel out of here.
| | 03:19 | Choose Yes and then choose Close.
| | 03:22 | I need to change a couple of settings.
| | 03:24 | To configure my application, there is
two places I can go. One is I can click on
| | 03:28 | the project and edit
items in the Property window.
| | 03:31 | For instance, I don't want this to
say Microsoft as the manufacturer;
| | 03:35 | I want it to say Lynda.
| | 03:37 | Also, the ProductName is wrong.
| | 03:39 | It should say PixelSmithDesktop version 2,
and I'll also change the Author to Lynda like that.
| | 03:46 | Another place I can change the
configuration is by right-clicking and choosing View.
| | 03:50 | The only one that I want to
change for this demo is User Interface.
| | 03:53 | So let me click here.
| | 03:55 | These are the different screens
that are going to show up during the
| | 03:57 | installation process.
| | 03:58 | What I would like to do is add a
custom Readme to this install.
| | 04:01 | So what I will do is right-click
on Start and choose Add Dialog.
| | 04:05 | Then I'll come over here
and choose Read Me. OK.
| | 04:08 | Next, I'm going to drag this Read Me
up to the top of the list, so that it's
| | 04:12 | second from the Welcome screen.
| | 04:14 | So they'll see the Welcome screen.
| | 04:15 | Then they'll see my Read Me.
| | 04:17 | I also need to tell what text to show.
| | 04:18 | So I'll come over here and click the
ReadmeFile. Browse for that. I'll look in
| | 04:24 | my Application Folder, find
this ReadMe.rtf file and click OK.
| | 04:28 | I'm done with the changes to the user interface.
| | 04:32 | Next, I'm going to change what's
installed on the user's computer.
| | 04:35 | Here is the Application Folder.
| | 04:37 | I would like to add some
extra folders to this location.
| | 04:40 | So I'm going to right-click and choose
Add > Folder, Samples and Add > Folder, Documents.
| | 04:49 | Now I can go back to my Application
folder and drag the files to the location
| | 04:55 | where I want them to be
installed on the user's computer.
| | 04:56 | So this documentation file and this
movie file I think those belong in
| | 05:01 | the Documents folder,
| | 05:02 | so I'll drag those over here.
| | 05:03 | And these two sample files, I'll
drag those over to the Samples folder.
| | 05:07 | Now I'll add a shortcut to the user's desktop.
| | 05:09 | That's accomplished by coming over here and
right-clicking, choosing Create New Shortcut.
| | 05:13 | I want to create a shortcut to the
Primary output, which will be my EXE.
| | 05:19 | I should probably change the name,
because it's a terrible name.
| | 05:21 | I'd also like to add
something to the user's Start menu.
| | 05:24 | So I'll go over here and create a new
folder and call this one PixelSmith.
| | 05:31 | Then I'm going to click here
and choose Create New Shortcut.
| | 05:35 | I would like the Primary output.
| | 05:37 | Again, I'll rename this one.
| | 05:39 | I'd also like a shortcut to my help file.
| | 05:41 | So I'll add one more shortcut and pick
Documents and then Documentation.chm.
| | 05:47 | Now, I'm ready to build the application,
| | 05:49 | rename that, and I'm ready to build.
| | 05:52 | I'll come over here and choose Build.
| | 05:54 | Once I have a successful build, I'll
go to my solution and choose Open Folder
| | 05:59 | in Windows Explorer.
| | 06:00 | It will open my installer application.
| | 06:03 | Here is the files that it will give to my user.
| | 06:06 | They can double-click on these, like
the setup.exe, and now you see the dialog
| | 06:10 | includes the text that I
had, PixelSmith Desktop V2.
| | 06:13 | It has my Readme file.
| | 06:16 | It's suggesting the correct folder
location based on the criteria I put in that
| | 06:21 | Property window, the Lynda folder,
and then the PixelSmithDesktop V2.
| | 06:24 | I'll click on Next to install, and I have
now successfully installed my application.
| | 06:30 | Next, I'll go to the Start menu
and see if my application is here.
| | 06:34 | It was called PixelSmith.
| | 06:36 | There it is, PixelSmithDesktop.
| | 06:38 | It's running successfully.
| | 06:40 | Next, I'll try my Start folder, and I'll
look for the PixelSmith folder, and both
| | 06:45 | my shortcuts are available there,
| | 06:47 | so I'll click on the Documentation.chm file to
see what my help file looks like. That's working.
| | 06:52 | If you were to go look in the Program
Files folder on your hard drive, you'd
| | 06:55 | also see those subfolders that I generated,
like for instance the Samples folder is there.
| | 07:00 | I would like to show you how
to uninstall the application.
| | 07:03 | On Windows 7 you would go to Programs
and Features, and then you're going to
| | 07:08 | scroll down and find the
PixelSmith area. There it is.
| | 07:11 | Click on this and choose Uninstall.
| | 07:14 | Click Yes to this dialog, and
now the application is uninstalled.
| | 07:18 | The Visual Studio Installer creates a
basic setup that works for most developers.
| | 07:22 | If you want more power, I suggest
you look at the InstallShield add-in.
| | 07:26 | There is one feature that's
missing from this installer.
| | 07:29 | I think it would be nice if the
application would automatically update when you
| | 07:32 | create a new version.
| | 07:33 | For that, you need to look at creating
a ClickOnce installer, and that's the
| | 07:37 | topic of the next movie in this chapter.
| | Collapse this transcript |
| Creating a ClickOnce application| 00:00 | ClickOnce is an alternative to
the standard Windows installer.
| | 00:04 | The name refers to the fact that the
user is prompted once to install the app.
| | 00:08 | The more compelling reason to
use ClickOnce, however, is for its
| | 00:11 | automatic update feature. Here is the idea:
| | 00:14 | When you deploy your application
you embed a URL in the application.
| | 00:18 | Then you deploy a special
manifest file to that URL.
| | 00:21 | The manifest contains information
about the application itself and a list of
| | 00:25 | application files and prerequisites.
| | 00:27 | Whenever your application is started,
the ClickOnce engine checks that URL to
| | 00:31 | see if the manifest has changed.
| | 00:33 | If so, the application is updated
based on the contents of the manifest file.
| | 00:38 | I've used ClickOnce successfully on
several projects, and I have to say that
| | 00:42 | it's easy to use and works as advertised.
| | 00:45 | I'm opening a solution called
ClickOnceDeploy, which contains our
| | 00:49 | PixelSmithDesktop WPF application.
| | 00:52 | To do a ClickOnce deployment, you just
need to open this Properties node and go
| | 00:57 | to the Publish section.
| | 00:59 | I would like to talk about security keys first.
| | 01:02 | You have to sign your manifest. It's easy
to sign by going to the Signing tab and
| | 01:07 | using this Sign the ClickOnce manifests.
| | 01:09 | I have already created a
temporary test certificate key.
| | 01:13 | If you want to create your own, just
click on this Create Test Certificate key
| | 01:16 | and it will replace the existing one.
| | 01:18 | Next, I'll go to Publish, and
we'll talk about these two locations.
| | 01:22 | There is the Publishing Folder
Location and the Installation Folder location.
| | 01:26 | The Publishing Folder Location is
where Visual Studio copies the files during
| | 01:30 | a publish operation.
| | 01:32 | The Installation Folder URL is where
the user goes to install the application.
| | 01:36 | These may be the same location,
and they might be different.
| | 01:40 | Let me tell you a reason
why they might be different.
| | 01:42 | Let's say we're deploying off of a web server.
| | 01:45 | I don't have permissions to upload files
to that server. I will have to give them
| | 01:48 | to my IT administrator.
| | 01:50 | So I'll publish to a local location.
| | 01:52 | Then when I'm done, I'll zip those up
and send them to my administrator and say,
| | 01:56 | "These need to be installed on our web server."
| | 01:58 | I do need to embed the correct HTTP
location here though, because that's where
| | 02:04 | your application is going
to go look for the manifest.
| | 02:06 | For today, we're going to use the
same location to make our lives easier.
| | 02:10 | I'm going to go out to our Desktop, and
I'm going to create a new folder on the
| | 02:14 | desktop called Deploy.
| | 02:15 | Then I'm going to open that Deploy folder, and
then I'll click up here and copy the location.
| | 02:22 | Then I return back to Visual Studio,
and I'm going to paste that location in
| | 02:27 | this Publishing Location.
| | 02:29 | Now, I'm going to have to change this
to localhost, and then I'll take and copy
| | 02:34 | this and paste it down here in
the Installation Folder location.
| | 02:38 | Next, I'm ready to publish.
| | 02:40 | I will walk you through the
wizard, so you can see how that works.
| | 02:42 | I'll click on the Publish Wizard.
| | 02:44 | This just asks where the publishing folder is.
| | 02:46 | I'll go ahead and click the Next button.
| | 02:48 | Here, I can specify where the user will
install the application: from a web site,
| | 02:53 | or from a local file, or from a CD-ROM.
| | 02:55 | Naturally, we're choosing this local location.
| | 02:57 | Next, I choose whether I want to
have the application run offline or not.
| | 03:01 | In other words, if the web site is not
available, will this application is still run?
| | 03:04 | I'll click Next and then Finish.
| | 03:07 | It opens this folder, which is my Desktop\Deploy
location, and let's look at what we have here.
| | 03:12 | This is the file that the user
will run to install the application.
| | 03:15 | This is the manifest file.
| | 03:17 | It contains XML data, pointing out what
the current version of the application is.
| | 03:22 | This folder contains all the
physical files that need to be installed on
| | 03:25 | the user's machine.
| | 03:26 | So if I open up the Application Files
folder, here is our first version of our
| | 03:29 | application, and inside there are the
files that need to be copied to the
| | 03:32 | user's install folder.
| | 03:33 | I'm going to go back here and double-
click on setup.exe, step through the prompts.
| | 03:38 | The reason I get this is because I'm
not using a real security certificate; I'm
| | 03:43 | using a test certificate.
| | 03:44 | Click on Install, and now
the application is running.
| | 03:47 | We'll check out and make sure it's working.
| | 03:50 | Looks good, and then I'll close the app.
| | 03:54 | Now the next day your user comes into
work, they go down here, and they choose
| | 03:57 | to run PixelSmithDesktop.
| | 03:59 | I don't know if you saw that, but very
briefly there was a Checking for Updates
| | 04:04 | dialog that popped up.
| | 04:05 | It didn't find any new updates, so it
just went ahead and ran the application.
| | 04:09 | I'm going to make some changes to the
application over inside Visual Studio.
| | 04:12 | So I'm going to go to my UI, and I'm
going to change some text on this ToolBox.
| | 04:18 | I'm going to come over to the Text
property and put an extra x in there and then
| | 04:23 | compile my application.
| | 04:24 | So now I have a new version.
| | 04:26 | Let's publish that new version.
| | 04:28 | We'll go back to the Publish location.
| | 04:31 | Rather than click on the Publish
Wizard, I can just click on the Publish Now.
| | 04:35 | Let's go look in this folder.
| | 04:38 | You see how there is two versions
here now? And this manifest file is now
| | 04:41 | pointing to the second version.
| | 04:43 | So, the user now goes to their
shortcut on their desktop, and they see this
| | 04:48 | dialog, "A new version of
PixelSmithDesktop is available.
| | 04:52 | Do you want to download
it now?" Of course we do.
| | 04:54 | We'll click on OK, and now there is our
new improved version of the application.
| | 04:59 | There is more to explore in ClickOnce.
| | 05:01 | For example, it exposes a programming model so
that you can write code to check for updates.
| | 05:06 | I'm a firm believer in the power of
this tool, and I love how ClickOnce
| | 05:09 | streamlines the update
processes for my application.
| | Collapse this transcript |
| Setting up IIS for deploy| 00:00 | Before watching the "Deploying a
Silverlight or ASP.Net application" movie in this
| | 00:04 | chapter, you need to have IIS
running and configured correctly.
| | 00:08 | This movie shows you the bare minimum
necessary to deploy the application to
| | 00:12 | your local web site.
| | 00:13 | If you're going to have IIS running on
your computer, it has to be enabled.
| | 00:16 | To do that, you need to go to
Programs and Features, and click on this Turn
| | 00:22 | Windows features on or off.
| | 00:24 | Next, you're going to want to come down
here and click on this section where it
| | 00:27 | says Internet Information
Services, and then click OK.
| | 00:30 | After about two minutes, IIS will
be up and running on your computer.
| | 00:34 | I've already got it running, so I'm
going to click Cancel and then Close.
| | 00:38 | Also for this movie, I'm going to
create a separate folder on my desktop to
| | 00:42 | contain my web site.
| | 00:43 | Now that I have those two
things ready, I'm going to open IIS.
| | 00:46 | I'll go back to my Start menu and
type in "IIS", and then I pick this item up
| | 00:50 | here: Internet Information Services Manager.
| | 00:53 | I'll maximize my window, and then we'll
take a look at this preview over here.
| | 00:58 | Currently, I have one site on my web server.
| | 01:01 | I'm going to add a second site
for my demonstration purposes.
| | 01:05 | Right-click, choose Add Web Site.
| | 01:07 | I'm going to call this site LyndaWeb,
and I'm going to point it to that
| | 01:12 | folder on my desktop.
| | 01:13 | So I'll go to my desktop, find the
LyndaWeb folder, and then click OK.
| | 01:19 | Now because this default web site is
already using port 80, I need to assign a
| | 01:24 | different port number.
| | 01:25 | I'm going to choose an arbitrary number.
I'll just pick the number 90 and then click OK.
| | 01:30 | Notice it says, "Start Web site immediately."
| | 01:33 | So now the second web site is up and running.
| | 01:36 | I'm going to click on Application
Pools and verify that LyndaWeb is using the
| | 01:41 | .NET 4.0 framework, which it is not.
| | 01:43 | So I'm going to double-click on
this item and then choose .NET 4.0.
| | 01:47 | That's because I'm going to be using a
Silverlight application in a later demo.
| | 01:52 | My web site is now configured. I can close IIS.
| | 01:55 | For a more detailed look at ASP.NET
deployment, check out the ASP.NET Essential
| | 02:00 | Training course here on lynda.com.
| | 02:02 | Be sure and watch the IIS chapter in
that title to learn more about setup and
| | 02:06 | configuration of ASP.NET and IIS.
| | Collapse this transcript |
| Deploying a Silverlight or ASP.NET application| 00:00 | After you create a Silverlight or
ASP.NET web application, you typically deploy
| | 00:04 | it to a web server where
others can access your application.
| | 00:08 | At a bare minimum, a web deployment
copies files from one server to another.
| | 00:12 | In some situations, you might need to
deploy it to multiple servers in a web farm.
| | 00:16 | I've seen many ways to deploy
applications to web servers--some of
| | 00:20 | them downright clunky.
| | 00:21 | Fortunately, for us, Microsoft
created web deploy extensions for their
| | 00:25 | enterprise web server,
| | 00:26 | IIS, and Visual Studio can use these
extensions to deploy your application.
| | 00:31 | For today's demo, I'm going
to be using the solution
| | 00:35 | UsingBingMapControl, which contains
a Silverlight application and an
| | 00:39 | ASP.NET application.
| | 00:41 | The Silverlight pages are going to be
rendered inside the ASP.NET application.
| | 00:45 | Let me show you what the application will look like.
| | 00:47 | I'm going to press F5 to run the application.
| | 00:50 | This showcases the Bing Map control,
which I can also programmatically access.
| | 00:56 | For instance, I can click on this New
York button to center the map on New
| | 01:00 | York, or I can click here to zoom
in on an area of the Grand Canyon.
| | 01:05 | So this is our ASP.NET application.
| | 01:07 | Now I'm ready to deploy.
| | 01:09 | I need to push it out to our web servers.
| | 01:12 | For our demonstration today, we're going
to be using a local server in IIS, but
| | 01:16 | in reality we probably would be
publishing this to a production server
| | 01:19 | somewhere on a host.
| | 01:20 | I can go to Build > Publish.
| | 01:24 | In this dialog, I have several
ways to connect to the web server.
| | 01:28 | I can use the local file system on my
machine, because IIS is running locally.
| | 01:32 | If it was running elsewhere, I could
use FTP, or File Transfer Protocol, or
| | 01:37 | FrontPage Server Extensions to deploy.
| | 01:39 | I can also use Web Deploy, which is
Microsoft's latest mechanism for deployment.
| | 01:44 | I'll show you that dialog in just one minute.
| | 01:46 | I'm going to delete all the existing
files, and I'm going to save out to my
| | 01:51 | desktop, which is where IIS
is looking for the web files.
| | 01:55 | Before I publish, I need to
configure IIS for the correct authentication.
| | 02:01 | I'm going to go run IIS, and here's the site
we set up in the previous movie: the LyndaWeb.
| | 02:09 | Now, I'm going to click on
Basic Settings and then Connect As.
| | 02:14 | If we connect as application user, and
we run off our desktop, I'm going to get
| | 02:18 | denied access to the web config file.
| | 02:21 | So I'm going to switch over to a different user.
| | 02:23 | I'm going to use my user
account on this machine to connect.
| | 02:27 | I'm going to test my settings. And if
I get green authorizations, I know that
| | 02:34 | I'm correctly configured.
| | 02:36 | Naturally on your machine, you're
going to have to use account that makes
| | 02:39 | sense for your computer.
| | 02:40 | I'll close all these dialogs and close
IIS, and then I'm going to click Publish.
| | 02:46 | Now I'm going to go look in my Desktop,
in the LyndaWeb folder, and there are all
| | 02:51 | the files necessary for my web application.
| | 02:54 | Next, I'm going to test this in a web browser.
| | 02:56 | I'll try Mozilla Firefox.
| | 02:58 | I'll use localhost:
| | 03:02 | and then my port number, which is 90, and we
have successfully published our web application.
| | 03:07 | Let me show you one more detail.
| | 03:09 | I'm going to go back to Visual
Studio and then choose Build > Publish.
| | 03:15 | I can also deploy with this Web Deploy model.
| | 03:18 | As I showed you earlier,
I'm doing a local deploy.
| | 03:21 | But you can also publish using
this web deploy model, which is a much
| | 03:24 | more powerful system.
| | 03:25 | This a great option if you're
deploying to production servers.
| | 03:28 | With web deploy, I can perform other
tasks, like changing web.config files
| | 03:32 | for different environments, configure
IIS settings, and even install security
| | 03:37 | certificates.
| | Collapse this transcript |
|
|
11. Working with Source ControlUnderstanding source control| 00:01 | Version control systems have been
protecting our code for decades.
| | 00:04 | In this movie, I want to give a short
overview of version control and how it
| | 00:08 | relates to Visual Studio.
| | 00:10 | Version control, also called source control,
is a way to track versions in your software.
| | 00:15 | More precisely, it permits you or
your team members to track every single
| | 00:19 | revision made to a project.
| | 00:20 | The change track can be as small as
changing one character inside a single text
| | 00:25 | file, up to reorganizing entire directory
structures for complex multi-project applications.
| | 00:31 | All files and changes
are stored in a repository.
| | 00:34 | Most often this
repository is stored on a server.
| | 00:37 | In some of the newer control systems,
like Mercurial, the changes are distributed
| | 00:41 | across multiple developer desktops.
| | 00:44 | Version control takes a snapshot
of your project at a moment in time.
| | 00:48 | Anyone with access to your repository
can regress to a previous snapshot and
| | 00:52 | investigate the files as they were at that time.
| | 00:55 | Naturally, after working with an older
version, you can retrieve the most recent set.
| | 01:01 | Each change is stored
separately in the repository.
| | 01:04 | You never have to worry
about overwriting the files.
| | 01:06 | Of course, you'd better have a
reliable backup plan for your repository.
| | 01:11 | There are two common
metaphors used in source control.
| | 01:14 | The first is Lock-Modify-Unlock model.
| | 01:17 | One person checks out a file, or files.
At that moment, the source engine locks the files.
| | 01:23 | All your other team members cannot
edit the file while it is checked out.
| | 01:27 | If you have ever worked with someone who
forgot to check in a file and then went
| | 01:30 | on sick leave, you know the pain
that this type of system can cause.
| | 01:34 | The second metaphor is
the Copy-Modify-Merge model.
| | 01:37 | There is no locking in this model, and
anyone is free to get the latest version
| | 01:41 | and change the file.
| | 01:42 | The trouble, if there is any, comes
when two or more people edit the file and
| | 01:46 | then check in. The second person gets
a warning when checking in their copy.
| | 01:51 | It's their responsibility to merge
these changes with the prior check-in.
| | 01:55 | Visual Studio works with both of these models.
| | 01:58 | It really depends on which
source control engine you connect to.
| | 02:01 | If you're using Microsoft Team
Foundation Server, it supports both models.
| | 02:05 | As you can see, there are many
providers available to use.
| | Collapse this transcript |
| Setting up Team Foundation Server source control| 00:00 | In this movie, I want to show you how
to connect with Team Foundation Server.
| | 00:03 | A word of caution before continuing:
| | 00:05 | I am using my open source project
on CodePlex site for this movie.
| | 00:09 | You can see the screen here.
| | 00:10 | I'm logged into CodePlex,
and I have two projects that
| | 00:13 | I am a coordinator on:
| | 00:14 | my open source project called Shazzam,
which is a pixel shaded editor, and this
| | 00:18 | one I created for this
week's class called TruffleSoup.
| | 00:21 | Now I can connect to these sites,
because I'm the coordinator on them, but you
| | 00:24 | do not have permission to
connect to the CodePlex projects.
| | 00:27 | In order to follow along, you need
to talk to your administrator and get
| | 00:30 | information to connect to your
copy of Team Foundation Server.
| | 00:33 | Of course, if you don't have a Team
Foundation Server of your own, you can
| | 00:37 | always create a project on CodePlex.
| | 00:39 | It's free, after all.
| | 00:40 | I'm going to start by clicking this
TruffleSoup project. I will click here.
| | 00:45 | This is a temporary project. It's only here for
30 days, and I need to go to the Source Control tab, and
| | 00:53 | then over here, I can find the
information necessary to connect with different
| | 00:57 | source control engines.
| | 00:58 | As you can see, CodePlex allows you to
connect with Subversion, Visual Studio
| | 01:02 | Team Explorer, and Team Explorer Everywhere.
| | 01:05 | I am going to choose this one here.
| | 01:08 | Next I am going to copy this piece of
information, because I am going to need it
| | 01:12 | to connect to the server.
| | 01:13 | Now, I am going to open up Visual Studio,
and I am going to go to File > Source
| | 01:25 | Control > Open from Source Control.
| | 01:29 | Over here, you can see the Servers tab.
| | 01:32 | If I open this up, this is where I can
choose to servers that I want to connect to.
| | 01:36 | I am going to remove this
one and re-add it. Choose Add.
| | 01:41 | Here, I am going to paste in the
text I've got from the web site.
| | 01:45 | I am supposed to connect using HTTPS,
so I'll select this radio button here,
| | 01:50 | and then I'll click OK.
| | 01:53 | The next thing is it is
going to ask me for my credentials.
| | 01:57 | My credentials are listed down
here--at least some of them are.
| | 02:00 | My username is listed here, so I'll copy
that, and then my password is the one I
| | 02:05 | used to log on to CodePlex.
| | 02:07 | I will come over here and type
in the name and then my password.
| | 02:11 | I have successfully connected to the
server. Now, I can close this dialog.
| | 02:25 | Now, it shows that it is talking to TFS
with my credentials, and it shows that
| | 02:30 | there are two team project collections:
one is for Shazzam, my open source
| | 02:35 | project, and the other one is for this
fictitious site I created called TruffleSoup.
| | 02:39 | And what I can do next is
create workspaces for these.
| | 02:42 | I will choose trufflesoup and click on Connect.
| | 02:48 | It says, "Would you like to
open from Source Control?"
| | 02:50 | I already have an existing project up there.
| | 02:52 | I can download it, but this movie was
just about connecting to the source control.
| | 02:56 | In the next movie, I'll show you how
to set up your projects, create new
| | 02:59 | projects, upload them to source
control, and work with the files in
| | 03:04 | version control.
| | Collapse this transcript |
| Using Team Foundation Server source control| 00:00 | In this movie, I will show you how
to connect to a Team Foundation Server
| | 00:03 | repository, and work with the source control
tools inside Visual Studio. A word of caution:
| | 00:09 | I've already set up the connection to
TFS in an earlier movie in this chapter,
| | 00:13 | so if you need a refresher on how to create
a connection, you can watch the Setup movie.
| | 00:17 | I'm inside Visual Studio, and I am
going to show you how to open a project
| | 00:21 | from source control.
| | 00:22 | I am going to go to File > Source
Control, and then Open from Source Control.
| | 00:28 | I already have our project set up on CodePlex.
| | 00:32 | This is the sample project
I created for this class.
| | 00:34 | I am going to drill down into the
folders on the server by double-clicking.
| | 00:40 | At this point, I've reached
the solution files on the server.
| | 00:43 | Notice that's in a
folder called TruffleSoupMain.
| | 00:46 | I am going to select the solution file,
and then I am going to go down here and
| | 00:49 | map to a Local Path.
| | 00:50 | So I am going to type in this
folder that I created earlier this week,
| | 00:54 | called localSourceFiles, and then
I'm going to create another folder
| | 00:57 | called TruffleSoup.
| | 00:58 | Then I am going to click on OK.
| | 01:03 | We just connected to the server.
| | 01:05 | It's downloading the files to my computer.
| | 01:10 | Now if I right-click on this
solution and choose Open Folder in Windows
| | 01:14 | Explorer, you'll see that inside my
localSourceFiles folder is the TruffleSoup,
| | 01:19 | and then I've downloaded this Readme.txt,
| | 01:22 | this solution file.
| | 01:24 | If I look in here, you'll
see a number of files in here.
| | 01:27 | Notice there are several files
that end with this SCC extension.
| | 01:31 | Those are used by the source control engine.
| | 01:34 | There is another one up here:
| | 01:35 | TruffleSoupMain.vssscc.
| | 01:39 | Hard to say, but that's
the name of the extension.
| | 01:41 | I am going to return back to Visual
Studio and show you the Source Control.
| | 01:45 | If you can see, there is a little
padlock next to each of the icons in here.
| | 01:49 | That means it is under source control, and I
don't have an editable copy yet of that file.
| | 01:54 | So if I go over here to this main window,
double-click on it--we need to unpin
| | 02:01 | this for a while, this Pending
Changes down at the bottom of the screen--
| | 02:04 | here is the designer
surface for this WPF application.
| | 02:08 | If I go over to the Toolbox, and
drag another Button out here, watch what
| | 02:15 | happens to the XAML file over here.
| | 02:16 | There is a red check mark over
it, and when I hover over it, it
| | 02:20 | says, checked-out to me.
| | 02:22 | Now if other people check out files, I
can get notification on icons here that
| | 02:25 | when I hover over, it'll say
it's checked-out to someone else.
| | 02:28 | So that way, I know I can't check out that file.
| | 02:30 | This is what's called a silent checkout.
| | 02:33 | You can change whether it's a silent
checkout or whether it prompts you by going
| | 02:38 | to Tools > Options, and then choosing
Source Control, and then Environment.
| | 02:50 | Here's what happens: checked in items.
| | 02:54 | If they're checked in, and I am
editing it, it'll check it out automatically.
| | 02:58 | If I'd rather be prompted, I can choose
this Prompt for check out.
| | 03:01 | I'll close this. Now, I am going to make my changes.
| | 03:08 | Make this button the same size as the
other one, and now I'm ready to check in.
| | 03:13 | So what I do is I go over here, and I
say, I have a pending check in here.
| | 03:18 | I want to check this in.
| | 03:19 | It asks me if I want to change
my solution file. I say Yes.
| | 03:25 | Then I'm prompted to check in my source files.
| | 03:27 | Here is where I make my comment.
| | 03:29 | There are also some
affiliated items you can see over here:
| | 03:37 | there is Check-in Notes, there is something
called the Work Items, and there is Source Files.
| | 03:42 | Now, work items are part
of Team Foundation Server.
| | 03:44 | They're like notes of what needs to be done.
| | 03:47 | They can be bug tracking items.
| | 03:49 | I don't have them set up on
CodePlex, so I can't add a work item.
| | 03:52 | I am ready to check this code
in, so I'll click on Check In.
| | 03:56 | Now, the other developers can check
out this file and start working on it.
| | 04:00 | If I ever want to see the history of
what I've been doing, I can right-click
| | 04:03 | here and choose View History.
| | 04:06 | This shows that there has been
several change sets on this file.
| | 04:11 | I can see the timestamps on here.
| | 04:13 | So I can see I was working on these last night.
| | 04:17 | I can close this window.
| | 04:18 | Another thing you can do is you can
right-click and choose this Annotate item.
| | 04:25 | What this does is it shows you your
source code, and then along the edge it shows
| | 04:29 | you where the changes have occurred.
| | 04:30 | So something got changed
up here on change set 1014.
| | 04:34 | This is the latest change set, 1193.
| | 04:37 | This is where I added that second button.
| | 04:38 | So I can see that this is where I changed it.
| | 04:41 | Now, in a real project when you're
working with multiple developers, you'll be
| | 04:43 | able to see each person's information
over here and which sections they've been
| | 04:47 | working on and what they have changed,
which is really handy when you don't
| | 04:50 | understand a piece of code and you
need to know who wrote that section.
| | 04:52 | You can go look this up and then go, talk
to them and say, "What were you doing here?
| | 04:55 | I don't understand what this
code is doing at the moment."
| | 04:58 | So I'll close the annotated version of that.
| | 05:01 | I am going to make two changes.
| | 05:02 | I am going to double-click on
this to write some code, like that.
| | 05:21 | I'm thinking I want to check
this in--let me save this--
| | 05:24 | but I'm not sure what is
currently saved in the database.
| | 05:28 | I want to know what the differences
are between what I currently have on my
| | 05:31 | machine and what's checked into source control.
| | 05:33 | So what I can do is right-click on this,
and choose Compare, and I'll click on OK.
| | 05:43 | It brings up a Difference tool, and
it shows me the two files. And I can
| | 05:47 | obviously see that I've added four or
five lines of code here on the local copy.
| | 05:52 | See, it local up here at the
top? Here is the server. Here is the
| | 05:55 | timestamp of when this was last saved in
the server. Here is the timestamp on the
| | 05:59 | current file when I last saved it locally.
| | 06:01 | Now naturally, I can also edit these files.
| | 06:06 | I can copy pieces of data back
and forth between these two sites.
| | 06:09 | I'm happy with that change.
| | 06:11 | I am ready to check it in,
| | 06:12 | so I'll choose Check In again, and
we'll check that in one more time.
| | 06:24 | There is another window you should be aware of.
| | 06:28 | It's available in the View menu, under Other
Windows, right here Source Control Explorer.
| | 06:36 | This is another way of looking
at your source control engine.
| | 06:39 | This shows my server and my
TruffleSoup sub-item, and then I can click on this
| | 06:46 | and see all of the files
as they are on the server.
| | 06:50 | I can do some of the same
operations I was doing over here.
| | 06:53 | I can right-click on this
and choose Check Out for Edit.
| | 06:55 | I do want to point out
that this is a binary file.
| | 06:58 | These are text files. These are
binary files, this sample document.
| | 07:03 | So Team Foundation Server
creates those slightly differently.
| | 07:06 | If I attempt to edit this, if I double-
click on the SampleDocument.docx, it'll
| | 07:12 | load it into Word 2010,
| | 07:17 | but because it's a binary file, it
doesn't automatically check it out.
| | 07:21 | I have to go in and specifically check it out:
| | 07:25 | Check Out for Edit.
| | 07:28 | Now, I can make the
changes in the Word doc. Save.
| | 07:35 | Now, this is a common error.
| | 07:41 | The trouble stems from the fact that
the Sample Document is considered a binary
| | 07:45 | file by TFS, and it doesn't
automatically check those out when you open them.
| | 07:50 | All you need to do is close this
document, and check out from Source Control.
| | 07:55 | Now, it's checked out. Now, we double-click on it
to open it, and it will no longer be Read Only. Save.
| | 08:11 | Then I can check it back in.
| | 08:20 | I can't imagine working on a
project without source control.
| | 08:23 | I've used Team Foundation Server on many
projects, and it just keeps getting better.
| | 08:27 | Don't forget that Visual Studio also
supports other source control servers.
| | 08:31 | I've also worked on several
projects that use the subversion server.
| | 08:35 | There are several Visual Studio
extensions that allow you to perform the same
| | 08:38 | source control task
against the subversion system.
| | 08:41 | Regardless of which one you choose,
Visual Studio can bind to the repository.
| | Collapse this transcript |
|
|
12. Integrating with Microsoft Office ApplicationsUnderstanding the .NET Office integration| 00:00 | Microsoft Office is the world's
dominant corporate application suite.
| | 00:04 | A good portion of the business world uses
Microsoft Excel, Outlook, and Word applications.
| | 00:09 | Over the years, Microsoft has
incorporated many extensibility features in
| | 00:13 | Office applications.
| | 00:14 | With Office, you can create add-ins,
write macros and even access to core
| | 00:19 | features of Office via Visual Basic
for Applications, sometimes called VBA.
| | 00:24 | The Visual Studio team is also
part of this integration toolset.
| | 00:27 | In version 2003, they published a
special copy of Visual Studio, called Visual
| | 00:32 | Studio Tools for Office.
| | 00:34 | In Visual Studio 2010, Office
integration is built right in.
| | 00:38 | Let me show you some of the types of
applications you can build with Visual Studio.
| | 00:41 | I am using my File > New menu,
and I am clicking New > Project.
| | 00:47 | I have to choose a language.
| | 00:48 | I can choose either Visual Basic or C#.
| | 00:49 | The underlying programming model for
inside Office applications is Visual
| | 00:54 | Basic for Applications.
| | 00:56 | Up until this release of Visual Studio,
programming Office applications in C#
| | 01:00 | has been a frustrating experience.
| | 01:02 | The object model in VBA doesn't
mesh very well with the C# mechanics.
| | 01:07 | Now that C$ 4.0 is out,
it's much easier to write Office
| | 01:10 | automation applications.
| | 01:11 | For my demos, I'm going to use Visual Basic.
| | 01:13 | So I am going to open up the Visual
Basic node and choose Office and then filter
| | 01:19 | it down to only the Office 2010 applications.
| | 01:22 | As you can see, nearly every
Office product is represented in here.
| | 01:27 | Some of the Office products have
multiple items. For instance, Excel has an
| | 01:30 | add-in, a template, and a workbook.
| | 01:34 | Let's talk about the difference
between a workbook and an add-in.
| | 01:37 | A workbook, when you create an Excel
workbook, all of the code that you're going
| | 01:41 | to write is embedded in that one document.
| | 01:44 | If you open that document, you can use
the code. If you open any other documents,
| | 01:48 | that code is not available.
| | 01:50 | When you create an add-in, you're creating an
application that is independent of any document.
| | 01:56 | Most of the Office applications I've
seen have a separate add-in manager, and
| | 02:00 | this allows the user to remove an
add-in when it's no longer needed.
| | 02:04 | As you can see, there is an add-in for
Excel and one for Outlook, and so on.
| | 02:09 | Let me just demonstrate what
you can do with an Excel Workbook.
| | 02:12 | I am going to click here and then click on OK.
| | 02:14 | Visual Studio asks me if I'd like to create
a new document or use an existing document.
| | 02:20 | I am going to use a new document.
| | 02:28 | As you can see, in the designer area
of Visual Studio is a copy of Excel.
| | 02:32 | Anything that you can do in Excel
you can do inside Visual Studio.
| | 02:35 | I can add text to a cell. I can take that
cell and format it. I can apply Bold text.
| | 02:44 | If I have numbers I can put the numbers
in here and then add a auto-sum formula.
| | 02:52 | If I choose to run the application, I
can press F5 and debug the application.
| | 02:58 | Here I am pressing F5. Excel is going to launch.
| | 03:03 | And the important thing is I'm now debugging
my code that's inside my Excel code behind.
| | 03:08 | Let me show you that code behind.
| | 03:10 | I am going to close Excel and then
switch over to this Sheet1.vb. And then I am
| | 03:17 | going to right-click on it and choose View Code.
| | 03:19 | And as you can see, it's just Visual
Basic code with some event procedures
| | 03:24 | here that run when the sheet starts up and
other one that runs when the sheet shuts down.
| | 03:30 | I can also work with Word
and the other Office products.
| | 03:33 | Let me show you an example of that.
| | 03:35 | I am going to choose New > Project,
discard my existing one, scroll down,
| | 03:42 | and pick Word Add-in.
| | 03:46 | Remember, add-ins are
independent of any document.
| | 03:48 | Now, of course, I don't have a
designer because it's not part of a document.
| | 03:53 | This is typically added to the
Add-in menu in your application.
| | 03:57 | So if I were to run this and go look
in the Add-in menu inside Word, I would
| | 04:02 | see by custom add-in.
| | 04:04 | I'll show you an example
of that in a later movie.
| | 04:07 | As you see, this movie is just a quick
overview of what you can do with Office
| | 04:10 | in Visual Studio. The rest of the
movies in this chapter dig into each topic
| | 04:14 | in more detail.
| | Collapse this transcript |
| Making a Word 2010 application| 00:00 | Microsoft Word is a
remarkable tool for building documents.
| | 00:03 | Think about this for a minute.
| | 00:05 | Word supports headers and footers,
table of contents, and indexes.
| | 00:09 | You can embed charts and images.
| | 00:12 | It has a huge set of formatting tools,
and the list just goes on and on.
| | 00:16 | I've used Word automation successfully
on many projects as a reporting engine,
| | 00:20 | including creating hundreds of report
templates for a large insurance company.
| | 00:24 | Let me show you what it can do.
| | 00:26 | In this demo, I'm going to create
a Word 2010 document-based project.
| | 00:31 | I'm going to go to File > New > Project.
| | 00:34 | I'm going to choose
Office 2010 and Word Document.
| | 00:41 | Then I'm going to use a copy of an
existing document, which is out on our
| | 00:47 | Desktop, in our Exercise Files folder,
and then over here in our Assets folder.
| | 00:54 | I'm going to use this Shipping
Bill of Record and then click OK.
| | 01:02 | So as you can see, it's opened up
the Word UI inside Visual Studio.
| | 01:07 | I can see the content of my document.
And over here, you can see that I have a
| | 01:13 | copy of the docx file.
| | 01:16 | I also have a code behind
file called ThisDocument.vb.
| | 01:20 | Now here is the goal.
| | 01:22 | I want to be able to take these
bold sections of the document--
| | 01:26 | Date, and Customer, and Shipper name--and I
want to be able to replace those at runtime.
| | 01:32 | So the user would open up this document,
and I would calculate today's date and
| | 01:37 | put it at this location.
| | 01:39 | Then I would want to pull the
customer name out and store it here, and down
| | 01:44 | here, I want to have the shipper name.
| | 01:46 | So, what I can do is insert
something called a bookmark.
| | 01:49 | To do that, I'm going to go over to my
Toolbox, and I'm going to go up to the
| | 01:56 | Word Controls section right up here.
| | 01:58 | I'm going to find this Bookmark, and I'm
going to drag it over and drop it on Date.
| | 02:04 | It says, "Make a selection or place the
insertion point where you want to add
| | 02:09 | a Bookmark control. Then click OK."
| | 02:11 | So again, I can come over here and select that,
if it wasn't selected already, and then click OK.
| | 02:16 | Now, you notice that there are some
gray square brackets around the word "Date".
| | 02:21 | I'm going to give this a name that
makes sense, dateBookmark, and I'll do the
| | 02:31 | same for Customer--Bookmark.
| | 02:44 | Lastly, I'll do one for the shipper. Double-
click, drag, and then click OK, and I'm
| | 02:53 | going to call this one shipperBookmark.
| | 02:54 | Now next, I want to add some UI that'll
go on the right edge of my Word document.
| | 03:07 | These are called action panes.
| | 03:10 | If I go to my project and right-click
and say Add > New Item, you can see that
| | 03:18 | there is a list of common elements here,
and there is one up here at the top
| | 03:22 | called Action Pane Control.
| | 03:24 | You can also see that there's some Office
things like the Ribbon, Ribbon (XML),
| | 03:29 | Style Sheets, and other items.
| | 03:31 | I'm going to click on Action Pane
Control and then give it a new name.
| | 03:35 | I'm going to call this one
ShipperPane and then click Add.
| | 03:42 | It has a designer surface where I can
add any valid Windows Forms controls.
| | 03:47 | So, I'm going to put a label over here.
| | 03:49 | Now, I'm going to sort my properties in
alphabetical order to make things easier
| | 03:58 | to find, and then I'm going to type in
"Choose Shipper", and then below that, I'm
| | 04:04 | going to put a list box.
| | 04:11 | Then I'm going to go click on the Items
(Collection), click on this button that
| | 04:13 | has the three dots on it, and I'm
going to type in the names of my shippers
| | 04:17 | "FedEx", "UPS", "USPS", and "Clairs
Couriers", and then click OK.
| | 04:30 | Now let's save this application.
| | 04:35 | I'm going to save this out into my
Desktop folder > Exercise Files > chapter 12 >
| | 04:44 | movie number 02, and then click Save.
| | 04:50 | Now, I'm going to write a little bit of
code on this ShipperPane, and then now
| | 04:57 | I'm going to test the application.
| | 04:58 | So I'm going to choose the
ShipperPane again, and I'm going to go to the
| | 05:04 | SelectedIndexChanged property.
| | 05:06 | I double-click on that item, and what I'm
going to do is I'm going to find those bookmarks.
| | 05:13 | To do that, I'm going to use the
special keyword called "Globals." and then my
| | 05:18 | bookmark names, ThisDocument--that
refers to the current document--dot.
| | 05:26 | There is my shipperBookmark.
| | 05:27 | I'm going to replace the text that's in
that, and I'm going to put in the text
| | 05:32 | that's in my list box.
| | 05:34 | So I'll choose ListBox1.SelectedItem. Let's review.
| | 05:43 | The user is going to click
on something in the pane.
| | 05:45 | This event's going to fire.
| | 05:46 | We're going to figure out
which item they selected.
| | 05:49 | We're going to get its text and
apply it to the shipperBookmark.
| | 05:52 | Let me save my application one
more time, and let's test this out.
| | 05:56 | I'm going to press F5 to run the
application, and there is our document.
| | 06:03 | I do not see the Action pane yet,
however, and that's because I need to tell
| | 06:07 | Word to load that Action pane.
| | 06:09 | So, I'm going to shut down Word.
| | 06:11 | I'm going to return back to my code
over here, and I'm going to go to the
| | 06:16 | document Startup event.
| | 06:17 | That's over here in ThisDocument.vb file.
| | 06:19 | Then I'm going to look at the code.
| | 06:24 | Here is ThisDocument_Startup.
| | 06:26 | Down here, I'm going to say Me dot ship date.
| | 06:30 | I forgot to type in "Text" here.
| | 06:40 | There is my customerBookmark.
| | 06:46 | I'm going to set its Text equal
to some fictitious customer name.
| | 06:53 | Then finally, we're going
to come down here and say,
| | 06:55 | Me.ActionsPanes.Controls.Add, and I'm
going to add an instance of my new Action
| | 07:07 | pane control, which I called ShipperPane.
| | 07:08 | So let's review what we did here.
| | 07:15 | When the document starts up, I'm going
to populate the two bookmarks with data,
| | 07:19 | and then I'm going to load the ShipperPane in.
| | 07:21 | Save my application. Press F5 to debug the app.
| | 07:28 | The date has been correctly entered, the
Acme Dental has been correctly entered,
| | 07:32 | and if I go over here and click on one
of these Shippers, you'll notice that
| | 07:36 | it's changing it in the body of the document.
| | 07:39 | Now of course I can turn off the
bookmarks by going to my setting in Word and
| | 07:42 | say, I don't want to see
the bookmarkers in my text.
| | 07:45 | We've just skimmed the possibilities
of what you can do with Word automation.
| | 07:49 | I'm sure you can think of many
reporting scenarios where using Word in this
| | 07:52 | fashion would be helpful.
| | Collapse this transcript |
| Making an Excel 2010 add-in| 00:00 | Excel has found a permanent
niche in most corporate environments.
| | 00:03 | Its usage is not limited
to accounting tasks either.
| | 00:07 | In this movie, I'm going to show
you how to extend it even further by
| | 00:10 | creating your own add-in.
| | 00:11 | Remember that an add-in is an
application that is installed once and then loaded
| | 00:15 | each time Excel runs.
| | 00:17 | It's not tied to a specific document.
| | 00:20 | For this demonstration, I'm going to
create a custom ribbon inside Excel 2010.
| | 00:24 | I'm inside a project called ProcessAnalyzer.
| | 00:27 | I've already written the
demonstration code for today.
| | 00:29 | I'm going to start by showing you how to add
your own custom ribbon to an Excel project.
| | 00:35 | You right-click on the
project, and choose Add > New Item.
| | 00:42 | Here is the ribbon designer that I want to use.
| | 00:45 | Now, I already have a ribbon, so I'm not
going to go ahead and click on the Add button.
| | 00:48 | Instead, I'm going to cancel
this one and show you the existing
| | 00:52 | ProcessMemoryRibbon I've written.
| | 00:55 | As you can see, it has a designer surface.
| | 00:58 | There's also a special section on the
Toolbox that contains Office Ribbon Controls.
| | 01:03 | So if I wanted to, I
could take this CheckBox here and
| | 01:05 | then drag it over and drop
it on my designer surface.
| | 01:09 | Of course, I can go to the code behind
and manipulate this item, or read this
| | 01:13 | information and push it into the Excel document.
| | 01:16 | Now let me show you what I mean.
| | 01:18 | I have this button here that I've
added an icon and some pictures to.
| | 01:23 | You can see it's a Button though.
| | 01:24 | If I switch over to the code behind
view by pressing F7, and scroll down here,
| | 01:30 | you can see I've written a Button1_Click procedure.
| | 01:33 | I start by creating a
reference to an Excel worksheet.
| | 01:36 | Then I go to the Globals area,
and I find the current application.
| | 01:41 | I get its Worksheet collection, and I
tell it to add a brand-new worksheet.
| | 01:45 | Once I've done that, I go into the sheet
and I print some text into the A1 cell.
| | 01:51 | I then print some other text into the
B1 cell. And then I have a small link
| | 01:57 | query here that goes out and talks to .NET and
gets all the running processes on the computer.
| | 02:04 | I then take those, apply a link query to
that, and I say, I want to order them By
| | 02:09 | their WorkingSet, how much memory
are they using, in a descending order.
| | 02:14 | Then I select out the processes.
| | 02:18 | Then down here, I take the number of
items--five, ten, fifteen, or twenty--and I
| | 02:27 | convert this to Array and
assign it to their process.
| | 02:29 | And then down here, I'm going to
generate some cells in my spreadsheet.
| | 02:32 | This number is coming from my Ribbon value.
| | 02:36 | If I go back to my Ribbon,
| | 02:37 | you'll see that I have a Count dropdown
here, and I've populated this with words
| | 02:42 | like "Five, Ten, Fifteen."
| | 02:43 | So that's why I'm getting that Count value.
| | 02:45 | As you can see up here on line 20,
I'm getting that from the Gallery, my
| | 02:50 | Gallery, SelectedItem.Tag, and then
I'm just returning an integer value.
| | 02:55 | So down here, I am iterating over the
processes.Length times one--the number of
| | 03:02 | items inside this array--and I'm
printing out a cell with the ProcessName,
| | 03:08 | and its WorkingSet.
| | 03:09 | Then I'm changing the NumberFormat for the cell.
| | 03:13 | Once that's done, I select that
range, and I create a brand-new chart.
| | 03:19 | The name of the chart's going to be Memory Used.
| | 03:21 | So, what's going to happen, when you
click on my add-in, I'm going to look at
| | 03:24 | the operating system, tell you the
number of processes that are using the most
| | 03:28 | WorkingSet memory, and then
generate a chart out of that.
| | 03:29 | Does that sound good?
Let's see if it works.
| | 03:33 | I'm going to press F5 to run the application,
which will launch Excel, load my add-in.
| | 03:39 | My add-in is living in this Add-Ins section.
| | 03:46 | Office 2010 has a separate area for
all custom Add-Ins. Here is my item.
| | 03:51 | Here is my Count.
| | 03:52 | There is Five, Ten, Fifteen, right?
| | 03:56 | So I'll choose Ten.
| | 03:59 | Now, I'm going to click in this
button, and it generated this sheet with
| | 04:06 | the names of the processes and the
amount of memory used, and it generated
| | 04:10 | this chart, which shows that Visual Studio is
using the most memory of all the applications.
| | 04:16 | Now, your user can uninstall this add-
in by going to their Add-Ins menu. Or if
| | 04:23 | you're a programmer, you can go to
the Developer toolset to remove this.
| | 04:26 | Let me show you how to do that.
| | 04:28 | I'm going to go to File > Options,
click on the Customize Ribbon, and then I'm
| | 04:36 | going to check the
Developer tab here and then OK.
| | 04:41 | Now, I have a new tab right here, and I can go
into the COM Add-Ins and find out my add-ins.
| | 04:47 | Here is the Load Test Report
add-in, and I can uninstall that.
| | 04:52 | The other way to uninstall this is to
go back to Visual Studio and choose this
| | 04:58 | Build > Clean Solution.
| | 05:01 | This will go out and remove it from the list
of add-ins to run the next time Excel runs.
| | 05:07 | So, I'll choose Clean
Solution for my final demo.
| | 05:10 | This concludes the section on Office automation.
| | 05:13 | Many of the other Office
products have similar add-in templates.
| | 05:17 | I encourage you to explore your own
ideas in your favorite Office tool.
| | Collapse this transcript |
|
|
13. Extending Visual StudioUnderstanding the extensibility model in Visual Studio| 00:00 | Visual Studio contains an amazing
assortment of tools to help in writing,
| | 00:04 | testing, compiling, and
deploying your applications.
| | 00:07 | Microsoft had the foresight to make
Visual Studio extendable and customizable.
| | 00:11 | In fact, a lot of that tools you work with
everyday in Visual Studio are really extensions.
| | 00:15 | There are two main ways to extend Visual Studio.
| | 00:18 | The simplest is to buy or
download a third-party tool and configure
| | 00:22 | Visual Studio to use it.
| | 00:23 | This requires no code writing on your behalf.
| | 00:26 | The second approach requires you to
write some code and instruct Visual Studio
| | 00:30 | to use this custom extension.
| | 00:31 | If you want to write these extensions,
you need to install the Visual Studio SDK.
| | 00:36 | Hidden in the depths of Visual Studio
are several extensibility frameworks.
| | 00:41 | Most of the functionality of Visual
Studio can be accessed via these APIs.
| | 00:45 | One of these APIs is based on
the ancient COM specification.
| | 00:49 | You can build either add-ins or
Visual Studio Packages that run on COM.
| | 00:53 | In Visual Studio 2010, Microsoft
introduces their new extensibility framework
| | 00:57 | called Managed Extensibility Framework, or MEF.
| | 01:02 | With this, you can build
add-ins and component parts.
| | 01:05 | Macros provide a quick and
easy way to write plug-in code.
| | 01:09 | They're written in Visual Basic.
| | 01:10 | They are stored in a special Visual
Studio macro files and can be imported into
| | 01:15 | your Visual Studio using
the Macro Explorer tool.
| | 01:18 | COM add-ins have a higher
potential for complexity.
| | 01:21 | For example you can create walkthrough
wizards or build new windows to add to the IDE.
| | 01:26 | These are compiled into executable files
and then deployed like any other application.
| | 01:31 | Plus, you get to write them
in your favorite .NET language.
| | 01:34 | COM VSPackages are even
more powerful than add-ins.
| | 01:38 | These programs have been used
by Microsoft partners for years.
| | 01:41 | You have to be an accepted partner
before you can acquire the SDK bits.
| | 01:45 | With VSPackages, you get full
access to the backend of Visual Studio.
| | 01:49 | You can create your own visual
designers, like the Windows Form Designer or the
| | 01:53 | Silverlight Designer.
| | 01:55 | If you've written your own custom
languages, you can use Visual Studio Packages
| | 01:58 | to create special code
editors and integrate the compilers.
| | 02:01 | MEF add-ins provide access to
the fancy new code editor in 2010.
| | 02:06 | With MEF, you are writing a WPF
application, so you can overlay special
| | 02:10 | graphics in the code editor.
| | 02:12 | The rest of this chapter will
show you how to use some of these
| | 02:15 | extensibility tools.
| | Collapse this transcript |
| Adding external tools to the Tools menu| 00:00 | Visual Studio contains a lot of
tools for working with your code,
| | 00:03 | but it's possible that it
doesn't contain the tool that you want.
| | 00:07 | After all, there are lots of
companies out there creating developer tools.
| | 00:10 | One of the easiest way to extend Visual
Studio is by adding these third-party tools.
| | 00:15 | I'm inside Visual Studio, and I have
opened a solution called ExternalTools that
| | 00:20 | contains two projects: one is a sample
application we are going to add to the
| | 00:24 | Tools menu, and another one is
PixelSmithDesktop, which I am going to use for my
| | 00:28 | disassembly demonstration.
| | 00:30 | The External tools live
up here in the Tools menu.
| | 00:33 | They are in this section of the menu.
| | 00:36 | For example, here is an external tool,
a very simple one called Create GUID.
| | 00:41 | This was added by Microsoft.
| | 00:43 | When I click on this link, it will
launch an independent application.
| | 00:46 | And then all this does is allow me to
generate a unique identifier and then
| | 00:51 | copy it to the Clipboard.
| | 00:53 | For a more exciting demo, I am going to
add one of my favorite third-party tools,
| | 00:57 | which is called Reflector.
| | 00:58 | This is a free tool that lets
you disassemble your .NET code.
| | 01:01 | So I am going to go to Tools > External Tools,
and then I am going to click the Add button.
| | 01:09 | This is the title for the menu, so I
am going to type in the word "Reflector".
| | 01:15 | And then here, I choose the executable location.
| | 01:17 | I click on this button.
| | 01:20 | And the executable, I've
made a copy of it on my desktop.
| | 01:25 | Here it is, Reflector.exe.
| | 01:27 | I've also created a copy of the
executable from this solution file here
| | 01:32 | OurExternalTool project,
| | 01:33 | I've copied that over here because I
am going to use it in a later demo.
| | 01:37 | So I'll click on
Reflector.exe and then click on Open.
| | 01:43 | On this line are the arguments that I
want to pass from Visual Studio to the
| | 01:48 | Reflector application.
| | 01:50 | In my case, I want to pass this
argument called Target Path. I am done,
| | 01:56 | so I am going to click on OK.
| | 01:57 | Now what's going to happen is I can
select one of these two projects, and when I
| | 02:02 | run Reflector it's going to define the
executable for this application and open it
| | 02:07 | up and disassemble the code, like this.
| | 02:13 | So there is PixelSmithDesktop.
| | 02:15 | If I close this down and click on
OurExternalTool and choose Reflector, I can
| | 02:21 | see that it has added another executable.
| | 02:25 | But this assembly tool is easy to use.
| | 02:27 | I just click here, press the Spacebar,
and it starts showing the disassembly
| | 02:35 | information over here.
| | 02:36 | Then I can drill down and find the
class that I wanted to disassemble.
| | 02:40 | And here's more information about it.
| | 02:42 | I can look at the Visual Basic version of
the code or disassemble it back into C#.
| | 02:47 | Your choice.
| | 02:48 | As I said, this is one my
favorite third-party tools.
| | 02:52 | For my next demonstration, I thought
I would show you one of our tools.
| | 02:55 | So I have created this application,
which has a simple Windows form on it.
| | 03:00 | And when you click on this button,
I'm going to load the command arguments
| | 03:05 | into this list box.
| | 03:06 | I am going to double-click on
this button to look at the code.
| | 03:08 | Now what I'm doing is when the form is
loaded in the constructor, I'm grabbing
| | 03:12 | an array of arguments.
| | 03:14 | Those are being passed in
from the static void main.
| | 03:18 | And then down here, I'm looking in the
argument array, and I am looking for this
| | 03:23 | delimiter, this semicolon.
| | 03:25 | I am going to split the string based on
that semicolon, and I am going to add an
| | 03:28 | individual row to the list
box based on that split string.
| | 03:31 | So let me add this to the Tools menu.
| | 03:34 | Click here. Go out to ExternalTools.
Click the Add button again. Put my title in.
| | 03:46 | I am going to find the executable.
| | 03:49 | Remember, on your machine you have
to find the correct location for this
| | 03:51 | executable. For convenience, I
put it on my desktop. There it is.
| | 03:56 | And then I am going to put some
arguments in here, like the Item Path, and then
| | 04:01 | that delimiter--the semicolon--and then
some other argument like Target Path,
| | 04:07 | semicolon, and I'll choose one
more, Solution Directory, semicolon.
| | 04:13 | I will click on OK, and then I
am going to run the application.
| | 04:19 | And then I will click the Get Arguments.
| | 04:21 | And you can see I have successfully
passed three pieces of information from Visual
| | 04:24 | Studio to my application. So let's review.
| | 04:28 | You can easily add external
applications to Visual Studio.
| | 04:31 | If the third-party application
supports command line parameters, you can pass
| | 04:35 | information to them from Visual Studio.
| | 04:37 | And that means you can customize
the IDE even more to your liking.
| | Collapse this transcript |
| Creating macros| 00:00 | A Visual Studio macro is code
that can be run by the Visual Studio IDE.
| | 00:04 | A macro is usually used to
automate some task in Visual Studio--
| | 00:08 | for instance, inserting Boilerplate
comments, formatting or cleaning up
| | 00:12 | portions of your code.
| | 00:13 | Macros are written in a special version
of Visual Basic. Plus, they are written
| | 00:17 | and edited in a special
version of Visual Studio.
| | 00:20 | For today's demo, I'm in a solution
called UsingMacros, and I've got the
| | 00:24 | PixelSmithDesktop application open
so that I can look at the source code.
| | 00:28 | I'm going to double-click on MainWindow.xaml.cs.
| | 00:31 | So to work with macros, you need to go to
the Tools menu and then the Macros section.
| | 00:39 | There are a number of menu items in here.
| | 00:42 | The one we're interested in
right now is the Macro Explorer.
| | 00:44 | We're going to enable the Macro
Explorer by clicking on this menu item.
| | 00:49 | The Macro Explorer lives over here with
the Solution Explorer and Class View,
| | 00:54 | and it shows that we
currently have two areas of macros.
| | 00:58 | The top one is my RecordingModule--
| | 00:59 | we'll look at that in a minute--and at
the bottom we have this Samples module.
| | 01:05 | Microsoft includes a number of samples for
you to look at and learn from their examples.
| | 01:09 | So, what I can do is click in my
code and then come over here to this
| | 01:14 | VSEditor Samples section and look at
things like this BeginningOfFunction
| | 01:19 | method, which I would guess--if I put
my cursor here on line 30 and ran the
| | 01:24 | BeginningOfFunction function, Run--it moves
my cursor to the first line of the function.
| | 01:31 | I'll put a comment in here in
C#, and then I'll choose InsertDate.
| | 01:38 | It put the current date in there.
| | 01:43 | Now, if you want to learn how these
macros work, you can right-click and choose
| | 01:47 | Edit, which will then launch the
special version of Visual Studio, and here's
| | 01:53 | the code that Microsoft
wrote to insert the date.
| | 01:57 | A good tool, if you're brand-new to
macros, is using the Macro Recorder.
| | 02:02 | Recorded macros go in this area.
| | 02:04 | Let me show you how that's done.
| | 02:07 | I'll come over here to my code,
and I'll turn on macro recording:
| | 02:11 | Tools > Macros > Record TemporaryMacro.
| | 02:16 | It's hard to see, but Microsoft has added
a special macro recording toolbar up here.
| | 02:23 | When I'm done, I'm going to
click on the Stop Recording.
| | 02:26 | Now, everything that I do in my code is
being recorded and written as Visual Basic code.
| | 02:31 | So I'll put a comment in, and then
I'll draw a line across the screen.
| | 02:45 | When I'm finished writing the
macro, I can click on Stop Recording.
| | 02:50 | Now, if you'll notice over here in
MyMacros, there is a TemporaryMacro now.
| | 02:55 | And I can right-click on that and go learn
how to write comments and text inside my code.
| | 03:00 | So, over here, I can see that I've
got this DTE.ActiveDocument, and so on.
| | 03:07 | DTE stands for Design-Time Environment,
and ActiveDocument obviously is where
| | 03:12 | my cursor was, and the Selection.Text
is meaning I'm writing some text into
| | 03:17 | that selected document.
| | 03:20 | Then I'm writing a blank line and then
some more text and a blank line and so on.
| | 03:24 | So this is a great tool for learning
how to write the Visual Basic code,
| | 03:28 | especially if it's not your native language.
| | 03:31 | You can also bring in your own macros.
| | 03:33 | I have a series of macros I use
all the time in Visual Studio.
| | 03:37 | To bring in your own macros, you right
-click and choose Load Macro Project.
| | 03:42 | For today's demo, we're going to be
using a VSMACROS file that's included in the
| | 03:47 | Exercise Files folder
and then the Assets folder.
| | 03:49 | So, I'm going to click on this and then
click on Add. And then up here, you'll
| | 03:55 | see that I have two macros.
| | 03:59 | We're going to look at
the Search for Google macro.
| | 04:02 | To show you how it works, I'm going
to come down here and click on this
| | 04:06 | BorderBrush property, and then
I'm going to invoke my macro.
| | 04:13 | As you can see, Visual Studio loaded
a copy of Internet Explorer within the
| | 04:17 | body of Visual Studio.
| | 04:19 | It passed my string to the Google search engine.
| | 04:23 | So you can see it's says search
question mark q=BorderBrush.
| | 04:26 | Let's look at the code.
| | 04:29 | I'm going to right-click here. Choose Edit.
| | 04:32 | I'm also going to show you that you can put
a breakpoint in your code and run this later.
| | 04:37 | So here, I'm getting the current
selected text--that was my word BorderBrush.
| | 04:42 | I'm trimming off any excess
space and storing it in the string.
| | 04:46 | I'm checking the length of the string to make
sure that something is selected in the designer.
| | 04:51 | Then I'm encoding that string, using the
UrlEncode, so that it's safe to pass to
| | 04:56 | a domain, and then I'm going to
concatenate that on the end of this string here
| | 05:01 | which is passing the string
to the Google search engine.
| | 05:04 | And then Navigate, that's telling Visual
Studio to load the browser in the designer.
| | 05:08 | Now, you notice I have a breakpoint in here.
| | 05:11 | So I'm going to return to my code, I'll
choose a different word, and then I'll
| | 05:18 | right-click and choose Run.
| | 05:20 | Notice that I switched immediately
over to Visual Studio, even though I didn't
| | 05:23 | have it running before.
| | 05:24 | This is that special copy of Visual
Studio. And at this point, I can step
| | 05:28 | through my code by using F11, hover
over the variables to find out what's
| | 05:33 | contained in them, rewind my
code by grabbing the yellow arrow.
| | 05:37 | All the tools that work in Visual Studio's
normal Debugger work in this special copy.
| | 05:41 | I'm going to go ahead and remove my
breakpoint and then press F5. And again, you
| | 05:46 | see my query string pass to Google.
| | 05:49 | So, a macro is just some Visual Basic
code that can be executed from within the
| | 05:53 | Visual Studio IDE, and I use
it to automate common tasks.
| | 05:56 | Let me show you one more thing.
| | 05:58 | I'd like to attach this macro to a keystroke.
| | 06:01 | I'm going to click on this Close button,
and then I'm going to go to my Tools
| | 06:05 | menu and choose Options.
| | 06:09 | Next, I'm going to look in my
Environment section and find the Keyboard section.
| | 06:14 | The name of my macro is called SearchGoogle.
| | 06:16 | So, I'm going to come over here and type in "goo".
| | 06:19 | There is my macro, and now I'm going
to come down here and press the desired
| | 06:23 | keystroke that I would like
to use to invoke this macro.
| | 06:26 | I think Ctrl+Shift+G would be good.
| | 06:30 | When I choose that, I see that
that's already assigned to a pre-existing
| | 06:34 | function inside Visual Studio.
| | 06:35 | I have to decide if I want to erase
this shortcut and replace it with my own, or
| | 06:40 | pick a different keystroke.
| | 06:42 | I never use this keystroke,
| | 06:44 | so I'm going to go ahead and
reassign it to my Google macro.
| | 06:47 | Then I'll click OK, and then I'll
come down here, and we'll try this one
| | 06:52 | here, Canvas.SetTop.
| | 06:54 | So, I'll click here, and then
I'll press Ctrl+Shift+G, and it
| | 07:00 | worked beautifully.
| | 07:01 | So, let's think about this.
| | 07:03 | A macro is just some VB code that can
be executed from within the Visual Studio
| | 07:07 | IDE, and I use it to automate common tasks.
| | 07:10 | For more complex scenarios, we need
to turn to other tools, but that's a
| | 07:13 | topic for the next movie.
| | Collapse this transcript |
| Using the Extension Manager| 00:00 | Visual Studio has supported an
extensibility model since day one.
| | 00:04 | Unfortunately, creating an add-in for
Visual Studio has been a task for geniuses.
| | 00:09 | You had to understand the
Visual Studio SDK, the COM model, and a
| | 00:13 | complex deployment mode.
| | 00:15 | Most of that changes with Visual Studio 2010.
| | 00:18 | By using the Managed Extensibility
Framework, or MEF, as a replacement for COM,
| | 00:23 | Microsoft has made it super simple
for us to build and deploy add-ins.
| | 00:27 | In this movie, I will show you the
Extension Manager, which is the first step in
| | 00:31 | understanding extensions.
| | 00:33 | I am inside a solution called
Extension Manager, which contains two projects: a
| | 00:38 | Console application and our
old friend PixelSmithDesktop.
| | 00:42 | To work with Extension Manager,
you go to the Tools menu and choose
| | 00:46 | Extension Manager.
| | 00:49 | On this tab, you can see any
installed extensions in Visual Studio.
| | 00:54 | I have two here because I've installed
them in earlier movies: the DevExpress
| | 00:58 | tools and the Snippet Designer.
| | 01:00 | If I click on the Updates node, the
tool will contact Microsoft web site to see
| | 01:07 | if there are any updates
for my installed extensions.
| | 01:10 | As you can see, there are none.
| | 01:12 | Here is where it becomes really powerful.
| | 01:15 | I can click on the Online Gallery.
| | 01:17 | The tool will go out to the Microsoft
designated web site and give me a list of
| | 01:22 | all the available extensions.
| | 01:25 | Currently, they're sorted by
the highest rank extensions.
| | 01:27 | Now if I decide that I want this
Productivity Power Tools application, I can
| | 01:32 | click on the Download button.
| | 01:34 | I will do that in just a second.
| | 01:35 | Let me show you a couple of these tools.
| | 01:38 | Here's one called the VS10x Code Map.
| | 01:42 | As you can see, when you install this
extension, it adds a special colorized
| | 01:45 | navigation bar to the regular code window.
| | 01:49 | That PowerCommands is another favorite of mine.
| | 01:52 | This one gives you all sorts of useful
commands, especially when you right-click
| | 01:55 | items in this Solution Explorer.
| | 01:57 | For example, here you can see I can
open the containing folder or launch a
| | 02:01 | command prompt directly from this app.xaml file.
| | 02:05 | The one I'm going to install for
today's demo is the Productivity Power Tools.
| | 02:09 | All you need to do is click
here and then click Download.
| | 02:15 | Next, I need to click the Install button,
| | 02:18 | and it takes me over to the web site,
so I can read more about that extension.
| | 02:23 | I will have to restart Visual Studio here.
| | 02:27 | Now, I can read about this extension.
| | 02:28 | Let's look at a couple of features.
| | 02:31 | There is the Solution Navigator, which
gives you an alternate view to the Solution
| | 02:36 | Explorer, and then another one that's a
favorite of mine is the new Tab UI.
| | 02:43 | Also, some people like this automatic
C# completion of your brace structure.
| | 02:47 | So if you type in a beginning curly brace,
it can type ending curly brace for you.
| | 02:51 | I am going to close the browser, and then
I am going to switch back to Visual Studio.
| | 02:58 | Notice at the bottom of the screen it
says you must restart Visual Studio,
| | 03:00 | so I am going to choose Restart now.
| | 03:05 | After a few seconds, Visual Studio
restarts and reopens my solution.
| | 03:10 | And now I can go to my
Tools menu and go to Options.
| | 03:19 | And there's a new node in
the Tools Option section.
| | 03:22 | Here I can turn on the different
extensions. Like if I want to have the
| | 03:27 | Automatic Brace Completion, I can click here.
| | 03:29 | And if I wanted to disable that, I can turn it off.
| | 03:32 | Extensions are written using WPF.
| | 03:35 | So this is using a WPF UI.
| | 03:37 | Here is the Document Well Tab.
| | 03:40 | This is where I can change the way
the tabs look inside Visual Studio.
| | 03:43 | You can see I can do things like tab
sorting by project in alphabetical
| | 03:49 | order and then choose OK.
| | 03:52 | Now, I am going to open up a few code files.
| | 03:54 | I am going to open this
Program.cs and make a change.
| | 03:59 | Here is one of the additions.
| | 04:01 | So there is now a red dot on that tab to
signify that I have made a change to that file.
| | 04:05 | I am going to go here to this MainWindow.
xaml, and notice that the tabs are now colorized.
| | 04:13 | This blue tab is for all the files
that are inside the console demo.
| | 04:17 | And I open multiple tabs here
in the PixelSmithDesktop demo,
| | 04:20 | you see they show this kind of brown theme
and it's sorting them alphabetical by project.
| | 04:27 | I don't have enough files to show you
that in great detail, but if you have 20
| | 04:30 | or 30 files opened it is really handy
to see them in alphabetical order, when
| | 04:33 | you're looking for that particular tab.
| | 04:36 | I have to say I'm completely sold
on this version of Visual Studio.
| | 04:39 | This is the way extensibility should
be, from the consumer's perspective.
| | 04:43 | Extensions are easy to find, simple to
install, and they automatically update if
| | 04:48 | there's a newer version of the extension.
| | 04:51 | They integrate easily with
the Visual Studio IDE, too.
| | 04:54 | The reason they integrate so
well is because of the managed
| | 04:56 | extensibility framework.
| | 04:58 | And that's the topic for the
next movie in this chapter.
| | Collapse this transcript |
| Creating an MEF add-in| 00:00 | There were some major changes made to the
architecture of Visual Studio in this release.
| | 00:05 | The old Visual Studio interface,
including the code editor, was replaced with a
| | 00:09 | new UI written in WPF.
| | 00:11 | Then the extensibility framework
was enhanced with the new managed
| | 00:14 | extensibility code bits.
| | 00:17 | What this means for .NET
programmers is that it is now easier to write a
| | 00:20 | custom editor plug-in.
| | 00:21 | You no longer have to learn the ugly COM
object model to be a successful add-in author.
| | 00:27 | I'm inside Visual Studio, and I've
opened the CreateMefAddin Solution.
| | 00:32 | This contains two extensibility projects.
| | 00:36 | The first one is enabled. The second one
I have unloaded temporarily. That's
| | 00:40 | why it says unavailable.
| | 00:42 | Before you attempt to create an
extensibility item, you need to install the
| | 00:46 | Visual Studio SDK, which I've done.
| | 00:49 | Now I can go to File > New > Project
and choose my language and then this
| | 00:57 | Extensibility note. And here are some
other new items that were added for the SDK.
| | 01:03 | This is a syntax highlighter.
| | 01:06 | This adds graphics to the editor margin.
| | 01:09 | This applies adornments to the text,
and the one we are going to look at today
| | 01:14 | is called the Editor Viewport Adornment.
| | 01:16 | This is going to draw a purple
rectangle in a floating section of the UI.
| | 01:21 | That's what the adornment layer is.
| | 01:24 | I have already added a project of this
type, so I am going to click Cancel, and
| | 01:27 | then we are going to take
a look at this code here.
| | 01:31 | Most of the code that's inside this
Floating Code Counter Factory is managed
| | 01:35 | extensibility framework code.
| | 01:36 | I suggest you spend some time looking at
the code in there to learn how it works.
| | 01:40 | I don't have time to give you a
tutorial on managed extensibility framework.
| | 01:45 | I am going to look in this class
here through, FloatingCodeCounter.cs.
| | 01:53 | We are going to draw something on
the adornment layer, which as I said is
| | 01:56 | a floating area of the window, and then
here's the important parts for this application.
| | 02:02 | I am using these bits of code to
generate a WPF brush, and then this section
| | 02:07 | is creating a rectangle, a 30x30 rectangle,
and using the brushes to draw that rectangle.
| | 02:12 | And then at some point, I'm going to get
the adornment layer and add my floating
| | 02:18 | code counter to the adornment layer.
| | 02:20 | Then this wires up a few
events to listen. If you resize the Visual
| | 02:24 | Studio window, then it will move
the adorner window to a new section.
| | 02:28 | I'm ready to test out this
application, so let me save the application and
| | 02:35 | rebuild the application.
| | 02:37 | Now lets think what needs to happen if
I am going to test out my extension.
| | 02:42 | I need to install the tool in Visual
Studio. Extension manager is one of the
| | 02:50 | ways that I can do that.
| | 02:51 | There is also a special type of
installer, called a VSIX installer.
| | 02:55 | You'll notice that in this project there
is a file that ends with vsix.manifest.
| | 03:01 | When I compile my code in this bin
folder, it will create a file with
| | 03:08 | the extension vsix.
| | 03:10 | So when I'm ready to test the
application, I need to launch another version of
| | 03:14 | Visual Studio, especially if
I want to debug my add-in.
| | 03:20 | I need to launch this second version
and then attach the debugger to it, and
| | 03:23 | then when you work in that second
version of Visual Studio, you can walk through
| | 03:27 | the code in this first one.
| | 03:28 | To do that, you would click
on Debug > Start Debugging.
| | 03:32 | Using this method, however, is very slow because
it's attaching the debugger to Visual Studio.
| | 03:36 | So I am going to choose Start Without
Debugging, which will launch the second
| | 03:40 | version of Visual Studio
but wont attach the debugger.
| | 03:42 | I will show you what it looks like.
| | 03:49 | Notice that at the top of my
window it says Experimental Instance.
| | 03:52 | That's to tell me that I'm in
the second copy of Visual Studio.
| | 03:55 | Now before I go any further, let me
make one change. Tools, Options, and then
| | 04:03 | I'm going to go to Projects and
Solutions, and uncheck this Save New
| | 04:06 | Projects when created.
| | 04:08 | I am going to be creating
a lots of sample projects.
| | 04:10 | I don't want to cut up my folder
with applications I am not going to use.
| | 04:13 | So I'll click on OK, and then I'll
create a brand-new project, create a console
| | 04:20 | application and here's my Code Editor window.
| | 04:26 | There is the first instance of Visual
Studio in the background, and then you can
| | 04:30 | see when I resize this window, this
rectangle moves to a new position.
| | 04:33 | That's the floating window.
| | 04:34 | When I close this second instance of
Visual Studio and choose Discard, I get this
| | 04:41 | dialog because of that Tools Option
settings I just changed. And now I am going
| | 04:45 | to show you the code in this second project.
| | 04:49 | So I am going to reload the second
project and unload the first project.
| | 04:57 | Then I am going to save my changes, and
we are going to rebuild my application.
| | 05:04 | This application is slightly more
interesting because it has a better UI.
| | 05:07 | I have written a WPF user control, and
I've added some text blocks, and in this
| | 05:13 | case, I've added a text block
called Total Lines Text block.
| | 05:16 | I'm going to read the line count inside
the code window and update this number.
| | 05:20 | The way I am doing that is I am going to
exposing some properties on this window and
| | 05:24 | then in the FloatingCodeCounterV3.cs
file, down here in this area, I am using
| | 05:32 | this to talk to the Visual Studio
editor and get the line counts again.
| | 05:37 | Look at this code to see how it works.
I am ready to try it out, so I am going to
| | 05:42 | go and choose Debug > Start Without Debugging.
| | 05:50 | The Experimental Instance of Visual
Studio runs, I click on New Project, choose
| | 05:55 | a console application, and
there is my new floating window.
| | 06:01 | You can see the old
floating window is still there.
| | 06:03 | This is interesting.
| | 06:04 | It was installed inside this
experimental instance of Visual Studio. So I
| | 06:08 | can uninstall this purple rectangle
by going to Tools, Extension Manager,
| | 06:14 | and it shows that I have the
FloatingCodeCounter and the
| | 06:17 | FloatingCodeCounterV3 installed.
| | 06:20 | So if I click here and choose
Uninstall and then restart Visual Studio, that
| | 06:28 | purple rectangle disappears.
| | 06:29 | I am going ahead and leave with it like it is.
| | 06:32 | Now, watch what happens when I come
over here and type in some lines of
| | 06:35 | code. The total lines updates to 15
and if I put a few blank lines in and show
| | 06:41 | have 4 blank lines of code, and if I
put comment in here, that updates the
| | 06:49 | comments section. Wow.
| | 06:52 | The ease of creating these
extensions is incredible.
| | 06:55 | I know I've created a few
extensions in the old days, and this is much
| | 06:58 | easier. Plus, the ability to use the
extension manager to add and remove
| | 07:02 | extensions is very powerful.
| | 07:04 | In the next movie, I'll show you how to
install extensions and add them to the
| | 07:07 | public extension store.
| | Collapse this transcript |
| Deploying and installing an add-in with VSIX| 00:00 | If you've been following along in this
chapter, you probably have ideas for
| | 00:03 | building your own add-ins and
custom tools for Visual Studio.
| | 00:07 | How do you distribute those to other developers?
| | 00:10 | The best way is to use a special
installer, known as V-S-I-X, or VSIX.
| | 00:15 | You can use a VSIX installer to install
many Visual Studio editions, including
| | 00:18 | projects and item templates,
ToolBox controls, .NET assemblies, and even
| | 00:23 | custom start pages.
| | 00:25 | A VSIX is nothing more than a
compressed file that contains your compiled code
| | 00:30 | and some special files: a content
type XML file and a VSIX manifest.
| | 00:36 | Let me show you how to create a VSIX installer.
| | 00:40 | I have a Solution called
CustomVSIXInstaller opened, which has a project in it
| | 00:45 | called LyndaCustomBizTemplate.
| | 00:47 | So this thing that I'm going to
install today is a project template.
| | 00:51 | You might remember from earlier movies
that you can export your own templates by
| | 00:56 | going to File > Export.
| | 00:58 | You're going to choose to export a
Project template. And then here, we're going
| | 01:07 | to give this template a little bit of a
more interesting description, like that.
| | 01:22 | I don't want to automatically
import this template into Visual Studio,
| | 01:25 | so I'm going to uncheck that check box.
And I will want it to show the output
| | 01:29 | files folder when I'm done.
| | 01:31 | Now, you can see I have a file
from my previous runthrough.
| | 01:37 | So I'll click on Yes to delete it,
and here is that custom ZIP file.
| | 01:42 | This contains the ZIP
information for the custom template.
| | 01:46 | Now I'm going to create
the custom VSIX installer.
| | 01:48 | I'll go over to my solution, right-
click, and choose Add > New Project.
| | 01:57 | I'm going to go to the C# section,
then to Extensibility, and then to
| | 02:03 | the VSIX Project. And I'm going to call mine
lyndaBizTemplateInstaller and then click OK.
| | 02:17 | At this point, it's a simple matter of
filling out some details--whatever I
| | 02:21 | want to call my Product.
| | 02:24 | And then I'm going to come down here, and
this is the content that I'm going to install.
| | 02:28 | So, I'm going to click on the Add Content.
| | 02:30 | Here, I get to pick the
kind of installer I'm making.
| | 02:34 | Project Template's what I'm working
with, but you can see there's also VS
| | 02:37 | Packages, there's Custom Extension
Types, there is the MEF Components from an
| | 02:42 | earlier demo, and here is the Toolbox Control.
| | 02:45 | Today, I'm going to Project Template,
and then I'll come down here and choose
| | 02:50 | that ZIP file that I just saved out.
| | 02:53 | So, I'm going to go over here and copy
this file path, return back to Visual
| | 02:56 | Studio and paste it in
here. I'll paste it in here.
| | 03:04 | It'll be more useful if I paste it in here.
And there is my ZIP file and then I click on Open.
| | 03:09 | One more thing:
I need to type in a subfolder here.
| | 03:11 | I don't need to, but I'm going to have
this customize the Open Project window by
| | 03:17 | doing this, and I'll click on OK.
| | 03:20 | Next, I need to save my project
and build or rebuild my solution.
| | 03:26 | At this point, I can go out to my
installer, right-click, and choose Open Folder
| | 03:31 | in Windows Explorer, and then drill down
into the bin > Debug folder, and here is
| | 03:38 | the custom installer.
| | 03:39 | Now, I will give this
installer to other developers.
| | 03:43 | They will take it, and
they'll double-click on it.
| | 03:47 | It shows information
about what they're installing,
| | 03:49 | the Lynda Business template.
| | 03:51 | Now, I didn't put in a licensing term.
| | 03:54 | Now I'm going to click the
Install button and then Close.
| | 04:00 | It's told me I needed to restart
Visual Studio, but I could relaunch a second
| | 04:04 | copy, which is what I'm going to do.
| | 04:10 | And then I'm going to go to File > New
Project, and there is the lynda folder,
| | 04:16 | and there's our lyndaCustomBizTemplate.
| | 04:17 | Now, I'm going to choose this one and
click OK, and now I have my custom files
| | 04:24 | all loaded in from that CustomBizTemplate.
| | 04:26 | There's one more thing you can do with
this template, is you can go out to this
| | 04:32 | site, the Visual Studio Gallery, and
click on Upload, tell them what kind of
| | 04:40 | item you're uploading, give them the
path to your template, and then it'll be
| | 04:44 | shared with all the other Visual Studio 2010
users when they go to the Tools > Extension Manager.
| | 04:53 | It'll show up as an item they
can install from this location.
| | 04:57 | Now you know how to create a custom add-
in and a custom installer to share your
| | 05:01 | new tool with the .NET community.
| | 05:03 | I can tell you from past experience
that this way of doing extensions is far
| | 05:06 | superior to the old model.
| | Collapse this transcript |
|
|
14. Configuring Your ApplicationWorking with configuration files| 00:00 | .NET applications use configuration files
to change the way they behave at runtime.
| | 00:05 | These configuration files are
located in the same folder as the
| | 00:07 | application executable.
| | 00:09 | Let me show you a few
configuration files inside Visual Studio.
| | 00:13 | I've opened a Solution
called ConfigurationFiles.
| | 00:15 | It contains a ConsoleApplication, a
FinancialLibrary DLL, and a WebApplication.
| | 00:21 | I'm going to start by
looking at the WebApplication.
| | 00:24 | They commonly have a
configuration file called the Web.config.
| | 00:28 | It's added automatically
in the project template.
| | 00:31 | And this contains settings in an XML
format that might be used by this web
| | 00:35 | application. Now, in web applications,
you typically have at least one Web.config
| | 00:40 | in the root directory of the web site,
but you can also put other Web.configs in
| | 00:45 | children directories.
| | 00:46 | So here, you can see it's just XML.
| | 00:49 | There's a section called configuration,
and below that is a subsection called
| | 00:53 | connectionStrings. And inside this
connectionString is something with the name
| | 00:58 | ApplicationServices, and then below
that it has some details of how to connect
| | 01:02 | to this particular SQLEXPRESS database.
| | 01:05 | This implies that in my
C# or VB code, I could open this
| | 01:10 | connectionString through some code.
| | 01:12 | Other applications that you might create, like
console applications, don't have config files.
| | 01:19 | So let me look at this
ConsoleApplication and see what's going on here.
| | 01:22 | This ConsoleApplication has a
Reference to the FinanceLib.
| | 01:26 | That means when I deploy the
application, I have to deploy the Console's EXE,
| | 01:31 | and I also need to deploy the FinancialLib DLL.
| | 01:35 | Luckily for me, in Visual Studio, when I do
a compile--I'm going to come here and choose a
| | 01:41 | Build Solution, I do a Show All
Files and then look in the bin directory--
| | 01:47 | you'll see that it created the EXE, and
then also after it's uncompiling the DLL,
| | 01:51 | it made a copy here.
| | 01:53 | What I want to do is write a
configuration file that allows me to change the
| | 01:56 | location of this DLL, to put it in
another location on my hard drive.
| | 01:59 | I need a config file to tell this Common
Language Runtime where to go probe for that file.
| | 02:06 | So, the first thing I'm going to do is
add a configuration file to this ConsoleApp.
| | 02:09 | I'll right-click and choose Add > New Item.
| | 02:13 | Then I'll come over here to the
Search text box and type in "config".
| | 02:20 | Here is the one I'm looking
for: Application Config File.
| | 02:25 | Leave the name at the default,
App.config, and click Add.
| | 02:29 | Now, the CLR, when it's looking for
a configuration file for a Windows
| | 02:33 | application, expects the file to
have exactly the same name as your
| | 02:36 | finished executable.
| | 02:37 | Now, my finished
executable's name is ConsoleApp.exe.
| | 02:41 | That means my config file should
be called ConsoleApp.exe.config.
| | 02:48 | The good news is if I compile the
application right now, you'll see that there's
| | 02:55 | now two copies of the config:
| | 02:57 | this is one which I edit inside Visual
Studio and then every time I compile it
| | 03:01 | makes a copy of this and
gives it the correct name.
| | 03:04 | Therefore, if I ever change the
compiled version of the name later, Visual
| | 03:07 | Studio will change the name
of the configuration file.
| | 03:10 | I need to do one more thing.
| | 03:11 | I need to write some code in this
configuration file that will probe in new locations.
| | 03:16 | I've already got the code written.
| | 03:19 | It's up here in this Assets folder.
| | 03:20 | So here's what I'm going to use.
| | 03:24 | I'm going to copy this piece here and then
switch back to the App.config and paste that in.
| | 03:34 | So, this is telling the Common Language
Runtime: at runtime when you go to find
| | 03:39 | an assembly, I want you to
search these two extra locations:
| | 03:44 | otherfiles and the bin
folder. So now I'm ready.
| | 03:48 | I'll save and I'll compile.
| | 03:50 | Now, I'm going to open this Folder in
Windows Explorer, and what I'm going to do
| | 03:58 | now is I'm going to take this Debug
folder and copy it and move it somewhere
| | 04:01 | else on my hardware.
| | 04:02 | And so I think I'm going to move it to
move it to my Desktop and paste that in.
| | 04:05 | And then I'll go in here, and
I'll see if this application runs.
| | 04:16 | I'll double-click on this and the
application runs and gets my payment value.
| | 04:23 | Okay now, let's put a folder in here,
and call this one "DLLs" like that.
| | 04:32 | Now, I'm going to take this FinancialLib file.
| | 04:34 | I'm going to move it in there.
| | 04:36 | Now when I try to run this application,
it throws an unhandled exception because
| | 04:41 | the Common Language Runtime is
looking for that DLL and cannot find it.
| | 04:44 | So, what I can do is come into this
configuration file--this is just a text
| | 04:50 | file--and I can create another probe.
| | 04:54 | I can say instead of looking in the bin
folder, let's look in that DLLs folder.
| | 04:59 | I'll save that file, and now I'm going
to double-click on this ConsoleApp.exe
| | 05:06 | again, and it runs successfully.
| | 05:09 | So that's how I can change the runtime
probe for that file. So let's review.
| | 05:15 | A configuration file is loaded by the
.NET runtime when the application starts up.
| | 05:20 | The application reads sections of the
config file to determine what settings and
| | 05:23 | data to use at runtime.
| | 05:24 | You can write your own code to read
the sections of the config file too.
| | 05:27 | Visual Studio includes a clever tool
to simplify creation of application and
| | 05:32 | user-specific settings.
| | 05:33 | I'll show you how this
settings tool works in the next movie.
| | Collapse this transcript |
| Using the Settings Editor| 00:00 | Your application may need global settings.
| | 00:02 | These are typically stored in the
configuration file and loaded at startup time.
| | 00:07 | These settings affect the entire
application, regardless of who is using the
| | 00:10 | application on the local computer.
| | 00:13 | You may also want to
store user-specific settings.
| | 00:15 | Each user on the local computer
would get their own copy of the
| | 00:19 | configuration value.
| | 00:20 | .NET has a |
|
|