Silverlight 3 Essential Training

Silverlight 3 Essential Training

with Walt Ritscher

 


Silverlight 3 and its associated development tools, Expression Blend and Visual Studio, are used to reproduce the look and feel of a desktop application in a browser deployed, cross-platform framework. Silverlight 3 Essential Training is designed to teach programmers how to create dynamic, interactive, rich Internet applications that run on Silverlight 3. Instructor Walt Ritscher covers the basic framework architecture to creating an engaging user interface. This course requires a working knowledge of C#, but includes lessons on XAML, a language designed to make coding in Silverlight easier. Exercise files accompany the course.
Topics include:
  • Hosting Silverlight
  • Choosing a Silverlight programming language
  • Understanding XAML basics
  • Interacting with the user through events, dialogs, and controls
  • Adding animations to the interface
  • Managing application assets in Expression Blend
  • Compiling and deploying a Silverlight application
  • Customizing UI with Silverlight templates

show more

author
Walt Ritscher
subject
Developer, Web
software
Silverlight 3
level
Beginner
duration
6h 50m
released
Mar 02, 2010

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(Music playing)
00:03Hello!
00:04I am Walt Ritscher and I'd like to welcome you to Silverlight 3
00:07Essential Training.
00:08Silverlight is a powerful framework for building line of business programs.
00:12It does an amazing job of reproducing the world of Desktop applications and a
00:16browser deployed cross-platform framework.
00:19In this course, I'll start with an overview of the Silverlight platform.
00:23Then take a close look at the new Extensible Application Markup Language,
00:27more commonly known as XAML, and see how it simplifies writing Silverlight interfaces.
00:32I'll cover user controls, UI templates, adding media to your interface,
00:38energizing your application with animations and plenty more.
00:42Along the way, you'll discover lots of interesting tips on how you use Visual
00:44Studio and Expression Blend to make Silverlight development a snap.
00:49This course is targeted at developers, so a working knowledge of the C#
00:52programming language will be required for most of the course.
00:55I've been working with Silverlight since the first version was released back
00:58in 2007, and I am delighted to have this opportunity to introduce you to Silverlight.
01:03So, let's get started with Silverlight 3 Essential Training.
Collapse this transcript
Using the exercise files
00:00If you're a premium member of the lynda.com Online Training Library or if you
00:05are watching this tutorial on a DVD-ROM, you have access to the exercise files
00:10used throughout this title.
00:11I have copied the Exercise Files to my Desktop. Let's take a look.
00:13I'm going to open the folder.
00:16The Exercise Files are arranged by chapter.
00:19This is the Controls chapter, and inside each chapter are the individual movie files.
00:25Let's take a look at movie number 07.
00:27This is the starter project for the ExploringSilverLightToolkit project.
00:31If you open this one up, you'll see that there is a SLN file.
00:35This is the file that you open in either Expression Blend or Visual Studio.
00:40Since I've Visual Studio installed in this computer, I can double-click on the
00:43SLN file and it will launch Visual Studio.
00:45Within Visual Studio, each of the files that are used by Silverlight are arranged
00:49over here in the Solution Explorer.
00:51This closely mimics the file system on your hard drive.
00:53Let me return back to the Exercise Files and I will open this
00:57ExploringSilverLightToolkit folder and in here, you see matching folders.
01:02There is Properties. There is App.xaml.
01:05There is MainPage.xmal.
01:07You'll learn more as the course goes along.
01:09This is a mirror image of what you see in Visual Studio.
01:12The key to remember is once you've opened the Solution, you do not need to
01:15return to the file system.
01:16We are also using Expression Blend during this course.
01:19Let me show you how that works.
01:21I've switch to Expression Blend. To open the Solution file here, I would go to
01:25File>Open Project/Solution, find my Exercise Files folder on my desktop.
01:31Go to the same chapter, Controls, Movie number 07, double-click on this folder
01:37and then double-click on this Solution file.
01:39Expression Blend looks very different from Visual Studio, but it still uses a
01:43similar Tree view to viewing your files in your project.
01:46If you are a monthly subscriber or annual subscriber to lynda.com, you don't
01:50have access to the Exercise Files, but you can follow along from scratch
01:54with your own assets.
01:56Let's get started.
Collapse this transcript
Exploring the finished project
00:00I'm going to show you the finished application for this course.
00:03It's called PixelSmith and it demonstrates many of the features of Silverlight 3.
00:08We'll be working on this application repeatedly throughout this course, as we
00:12build it up using tools like Visual Studio and Expression Blend.
00:16I have it opened inside Visual Studio.
00:18If you're following along with the Exercise Files, you're going to want to open
00:21the PixelSmith solution.
00:23I'm going to press F5 to run the application.
00:26Visual Studio launches Internet Explorer as the default browser, but PixelSmith
00:32works equally well in Firefox.
00:34I'll copy the URL, switch to Firefox and paste it in the URL, and then press Enter.
00:40And as you can see, PixelSmith is running over here too.
00:44I'm going to close Firefox to return to Internet Explorer.
00:47As I said, this is a sample application so it demonstrates many of the new features.
00:52For one, it demonstrates some of the new controls like the Accordion control.
00:56You can click here to snap open different parts of the UI.
00:58It shows dynamic controls.
01:02I'm going to place the control in this area of the screen by clicking on the Add
01:05Ellipse button, not only that, I'll add an ellipse to my work area.
01:11I can interact with this ellipse by clicking on these buttons.
01:15I can also add a rectangle.
01:17There is one of the new tooltips available in Silverlight 3.
01:19I'm going to click on the Add Rectangle and note what happens to the Toolbar,
01:24the Toolbar changes.
01:26I can switch back and forth between the Ellipse and the Rectangle and the
01:30Toolbar knows about the two items in the artboard.
01:33I can also add fancier items like the Octopod.
01:38I can work with several Popup type windows.
01:40You've already seen the tooltip, but if I go back to the Ellipse menu there's
01:44the Print button, which brings up something called a child window.
01:48I can choose items in here, like a dialog, and then when I click OK, the
01:51information is passed back to the main UI, as you can see right here.
01:56Another kind of Floater is what's called the Popup.
01:58If I click on this button, you can see I have a little mini window pop up down
02:02here, which I can interact with and then close.
02:04We can have some fun with Physics Objects.
02:07You can drag these around on the screen and see if I can knock the rest of
02:14them off the screen.
02:17You'll also work with images throughout the course.
02:20For this first example, I'm going to choose From File.
02:24I'll go out and choose my Pictures folder, then the Sample Pictures folder.
02:28Choose a photograph from this collection, and then once you have the image in
02:33here, we can add special effects: Blurs, Drop Shadows.
02:38You're even going to learn to write your own Custom Shaders, like this ripple effect.
02:41We spend some time with the file system.
02:43You'll learn how to write to Isolated Storage.
02:46This will analyze the number of items on the work area and save it out to a
02:49file and then I can read out the value.
02:51I can save to my local hard drives.
02:52You'll learn how to do that.
02:53There is a section on Animations.
02:55If I click on this logo at the bottom, it will start a storyboard and
02:59animate the item into View.
03:01And lastly, you'll learn how to use Media.
03:03I'm going to click on the video help link, which will launch another child window.
03:08This is my alter ego.
03:09He is going to talk to you about PixelSmith.
03:12Walt in video: Hello. My name is Arturo Diega and I'm the founder and CEO of PixelSmith labs. You can call me cra-
03:20You'll learn how to control media, change the Volume, find out your current
03:24location in the video, and much more.
03:27Let's get started building PixelSmith.
Collapse this transcript
What you should know before taking this course
00:00I want to talk to you for a minute about the training we offer for Silverlight
00:043 and Silverlight 4.
00:05You may have noticed that we offer both courses here at lynda.com.
00:09The Silverlight 3 course is where you should start if you are new to Silverlight.
00:12In that course, I cover all the essentials to get you started as a
00:15Silverlight developer.
00:17The best way to think about our Silverlight 4 course is that it is an addendum
00:21to the Silverlight 3 title.
00:23It is in this title that I concentrate on the new features.
00:27I'm particularly happy with the improvements that make Silverlight better for
00:30creating desktop-style applications, but I encourage you to watch and see which
00:34new feature is your favorite.
00:36Here is the important point that I want to make if you're learning about
00:39Silverlight development:
00:41the core technologies in Silverlight 4 are unchanged from Silverlight 3.
00:46There are literally hundreds of features shared between the two versions.
00:50That's good for us, as that means we can leverage our skills from the previous release.
00:55These courses are targeted at developers, and I assume that you know how to
00:58write code and are familiar with C# and the .NET framework.
01:02So let me summarize.
01:04If you are new to Silverlight development, you should check out the Silverlight
01:073 Essential Training Series, here at lynda.com, or pick up a copy of the DVD.
01:12Every movie in that title applies to Silverlight 4 development.
01:16If you want to learn about the improvements available in Silverlight 4, watch
01:19the Silverlight 4 New Features title.
01:21Regardless of which title you watch, I know you're going to love what you can
01:24do with Silverlight.
Collapse this transcript
1. Getting Started
Silverlight 3 explained
00:00From a modest start less than three years ago, Silverlight has grown into a serious
00:04competitor in the Rich Internet Client space.
00:07Currently, it is installed on 47% of all internet connected devices.
00:12In some areas of the world, most notably Europe, the number climbs to 60%.
00:17For another important indicator in the growing influence of Silverlight
00:21just look around the internet and tally up the companies betting on this new technology,
00:25over 350 partners in all.
00:27These companies are placing a strategic bet on Silverlight for several reasons.
00:31One, Microsoft is a dominant player in many technologies and it seems likely
00:36that the Silverlight platform will be a success.
00:38Two, Silverlight is similar to other Microsoft frameworks, like WPF and is
00:45programmed in familiar Microsoft languages like Visual Basic and C#.
00:49There is a website that shows statistics on market penetration that you can use
00:52to learn more about the competing RIA frameworks.
00:55It is called www.riastats.com.
00:58At the time I'm recording this movie, it looks like they are tracking 17 million
01:01daily visits and Silverlight 3 has slightly more than 47% installed base.
01:07Microsoft has expended considerable time and effort creating new UI Platforms.
01:11Windows Presentation Foundation, WPF, was the first of the new generation UI
01:16frameworks to emerge from Microsoft.
01:18WPF leverages the powerful GPU processor available on your local computer.
01:22WPF is based on a time tested UI engine, known as DirectX.
01:28One common trait cropping up in modern UI generation tools is reliance on an
01:32XML based markup language.
01:34In WPF and Silverlight, that language is called XAML. While powerful, there is one
01:38major drawback to WPF.
01:40It only runs on Windows operating systems.
01:44Silverlight is a few years newer than WPF and as such, it benefits from the work
01:48done by the WPF team.
01:50Silverlight is based on WPF, and follows familiar and comforting
01:54programming models.
01:55Don't be fooled. Silverlight has undergone some serious reworking before being
01:59released to the web.
02:01For one, the size of the framework has been drastically reduced to speed up
02:04the install process.
02:07Silverlight underwent a long security rewrite too, just to make doubly sure that
02:11a rouge Silverlight application can not damage the client computer.
02:15Silverlight runs on many operating systems including Mac OS X, Windows XP,
02:21Windows Vista, and Windows 7.
02:24It's also available on several of Microsoft's server products, including Windows
02:292000, Windows 2008 and surprisingly, it's also available on Linux, though not
02:35through Microsoft, through the Mono/Moonlight framework.
02:38It also works in many browsers.
02:41It works in IE7 and IE8.
02:44It works in Firefox for Windows and Firefox for Mac.
02:47It's also available on Safari and there are plans to have it work in Google Chrome.
02:53Silverlight 3 is deployed to the client by way of a browser plug-in.
02:56The reason for this choice is obvious.
02:57It provides a mechanism for reaching a wider audience for your application.
03:00This is not a new idea.
03:02Microsoft tried it years ago with Active X controls.
03:05Many competing frameworks also use this model, including Flash, Flex and Java.
03:11Another huge benefit of using a proprietary runtime on a client is that
03:14Silverlight applications are not HTML-based.
03:18You can interact with the HTML on the page, but Silverlight is its own critter.
03:22You get better graphic rendering and avoid all those browser compatibility
03:26issues at the same time.
03:27For me, that is a compelling package.
03:31Another benefit, especially if you are currently using .NET, is that Silverlight
03:35can be programmed in familiar languages like C# and Visual Basic. Not content to
03:39incorporate classic .NET languages, Microsoft created several new dynamic
03:43languages like IronRuby and IronPython.
03:46Finally, the Silverlight framework is nearly the same as the .NET framework,
03:49which is awesome if your programs have .NET knowledge.
03:52So, I just like to say that Silverlight is filled with compelling features.
03:55They have really simplified building modern graphic rich applications and
03:58I think it's time we start showing you how to make your own Silverlight
04:01applications.
Collapse this transcript
Showcasing amazing Silverlight applications
00:00I know you're ready to start creating your own Silverlight application, but
00:03stay with me for a moment because I want to show you some great examples of what
00:06other people are doing with Silverlight.
00:08Silverlight has already proven capable of streaming media applications.
00:11Look at the Sunday night football site or the upcoming Winter Olympic site to
00:14see some terrific use of live streams and multimedia channels.
00:18In this movie, I would like to show you three examples.
00:20In the exercise file folder, in the Getting Started chapter, there is a folder
00:24called Amazing Sites.
00:25There are number of sites in here. The ones we are going to look at in this
00:28movie are in the Today folder.
00:29I'm going to start by looking at the Behind Bing, New Features.
00:34Bing has a new visual search feature.
00:36I'm right-clicking in the browser to show you that it's a
00:41Silverlight application.
00:42It's got some nice hover effects as you move over the items in the screen.
00:47I'm going to click down here in VISUAL SEARCH, a little animation there and then I'm going to
00:53click on to see THIS FEATURE IN ACTION ON BING.COM, maximize my browser and then
00:59I can choose to Sort. The difference here is I'm sorting and getting a visual
01:03representation of the data I'm sorting on.
01:05So, I'm going to look at New Cars, 336 New Cars. Today, I'm interested in the
01:11Safest car. You can see that Bing rearranges those in a nice, animated fashion.
01:18Let's look at by Make.
01:21For the second site I'd like to look at Today, I'm going to look at the
01:24Microsoft-Center des Usage, which is a very Flash-like site,
01:32(Speaker talking in Spanish)
01:36I'm going to mute that so that I can talk over what's going on here.
01:39This has got a nice animation between pages in the book.
01:44Go back to previous page. I love watching these things fold up. It's one of my
01:50new favorite things to do and one more page before we move on to the next application.
01:56And finally, I'd like to look at a game.
01:59Games drive a lot of the industry and it's no different in the Silverlight world.
02:03Here is a Silverlight shock game. I click on Play, choose my experience and then click Play.
02:25I'm going to press Escape and then close the browser window.
02:32You can continue playing on your own. These are just a tiny subset of the
02:36interesting applications currently released in the Silverlight space.
02:39I've included links to more sites in the Exercise Folder.
02:41Now it's time to start making your own Silverlight masterpieces.
Collapse this transcript
Setting up your developer computer
00:00All right! You've heard about Silverlight and are ready to start learning this new UI
00:03platform, but first you need to install some tools on your developer computer.
00:08In this movie, I will show you how to set up your computer so that you can create
00:11Silverlight 3 applications.
00:13The premiere developer tool available from Microsoft is Visual Studio.
00:18There are many versions of Visual Studio available. There is the Pro
00:21version, the Standard and my favorite, Team, and each is suited for
00:25Silverlight 3 development.
00:27Each version requires a paid license.
00:30Note that you'll need an hour or more to install Visual Studio.
00:33I'd also like to point out that Visual Studio 2010 will include the Silverlight tools.
00:38By the way, you can pause and copy down the link if you want to check it out.
00:42After installing Visual Studio, you should install the Silverlight tools for Visual Studio.
00:47These tools configure Visual Studio to work with Silverlight applications.
00:51The reason that you have to install this additional piece is that Silverlight 3
00:54shipped nearly a year after Visual Studio 2008 did and therefore, Visual Studio
00:59does not include any Silverlight editors.
01:02If you don't have a license for Visual Studio, you can install Visual Web
01:06Developer Express 2008.
01:08This is a free version of Visual Studio that can create Silverlight 3 applications.
01:12It is installed via the new Web Platform Installer, which is the latest
01:16installer technology to emerge from Microsoft.
01:19Web Platform Installer is a single location for installing any tools necessary
01:22for web development.
01:24Web Platform Installer installs the Silverlight SDK, the Web Express ID and many more tools.
01:30You won't get a database server with Web Express, but there is also the free SQL
01:34Web Express for data base development.
01:36Another thing you should consider installing is the Microsoft Silverlight
01:41toolkit. Don't be fooled by the name.
01:43The Silverlight toolkit is a Vital install for this Silverlight 3 developer.
01:47The toolkit is a Microsoft product that ships out of the normal
01:50Silverlight timeline.
01:51Here you'll find tools that are slated for future releases of the Silverlight
01:55API but are available now.
01:57I thought I would like to go take a look at the site for a minute and see some
02:00of the features in the Silverlight toolkit.
02:01Let me switch over to my browser. Here is the site.
02:04It's Silverlight.codeplex.com and if you scroll down the page, you'll see
02:08there are some new controls, like the TreeView control, a series of charts, the
02:13new AutoCompleteBox.
02:17Here is a more complete list, further down in the page.
02:20I highly recommend that you install this toolkit.
02:23I'll be using it throughout the series.
02:27Microsoft also has a set of Designer tools.
02:30I recommend that you install Expression Blend 3.
02:33Blend is the best Visual Design tool for crafting Silverlight 3 interfaces.
02:36Blend is marketed more towards UX designers, but I strongly believe that it is an
02:40essential tool for developers.
02:42It's available through your MSDN subscription or other Microsoft channels.
02:46Here is where you can find a free evaluation version.
02:49There are additional Silverlight tools available that are not used in this
02:52series but are worth looking at. Deep Zoom Composer allows you to take a series
02:56of photos and arrange them in a constantly zoom-able photo montage.
03:02Another intriguing tool is .NET RIA services.
03:06This tool set simplifies connecting to services on the server by creating
03:09client proxies on your behalf.
03:11As you have seen in this movie, you've got some work ahead to you to get ready
03:13for Silverlight development.
03:15I'd say you're looking at spending a few hours of your day downloading and
03:18installing these applications before you're ready to go.
Collapse this transcript
Contrasting the Silverlight versions
00:00Microsoft has released three versions of Silverlight and there is another
00:03version looming on the horizon.
00:05In this movie, I want to comment on these versions and help clarify the benefit
00:08of using Silverlight 3.
00:10Silverlight 1 was released in September of 2007.
00:14The primary reason for releasing this version was to get the product on the market
00:18and the number one feature was high-definition video.
00:21It had a very limited programming support.
00:23It only supported one language, JavaScript.
00:26A year later, Microsoft released Silverlight 2.
00:28That was a huge jump in features in this version, and probably the most
00:33important for me was the inclusion of the managed language and a managed runtime.
00:38This means, of course, that you can program in your favorite .NET language,
00:41like C# or Visual Basic.
00:42They also added input controls.
00:46There were no controls in Silverlight 1, much better networking, added support
00:51for Microsoft's new LINQ Integrated Query.
00:54The ability to work with the storage system on the local computer and other benefits.
00:59About nine months later, Microsoft released Silverlight 3.
01:04By the way, I'd like to talk about the versioning. Microsoft calls it
01:06Silverlight 3 and not Silverlight 3.0.
01:08That's right. Microsoft decided not to use the dot release name for Silverlight.
01:12There is a huge set of features in Silverlight 3.
01:16We will cover a large number of these in this title. And finally, the newest
01:22version is Silverlight 4.
01:23That's not released yet, but it's expected sometime in the summer of 2010.
01:28As usual, it continues the upgrade path with interesting things like
01:32drag-and-drop support, web cam support and other interesting pieces.
01:37As you can see, the Silverlight team and Microsoft is moving quickly to make
01:39Silverlight as full-featured as possible.
Collapse this transcript
Using Visual Studio to create a Silverlight project
00:00If you're a .NET Developer, you're probably familiar with Visual Studio.
00:03It excels as a developer IDE for many types of projects.
00:06Remember, however, that Visual Studio 2008 does not understand Silverlight
00:10projects unless you have installed the Silverlight 3 tools for Visual Studio 2008 SP1.
00:16This is due to the offset and shipping schedules, because Silverlight 3 shipped a
00:20year after the release of Visual Studio 2008.
00:22In this movie, I'd like to show you how to create a Silverlight application or two.
00:26To demo this, I'm going to launch Visual Studio 2008 and I'm going to go up to
00:30the File menu and choose New>Project.
00:35There are two languages prevalent in Visual Studio: Visual Basic, and Visual C#.
00:40For this series, I'm using Visual C#.
00:42So, I'm going to come down to Visual C# and then I'm going to look to see if
00:45there are some Silverlight apps.
00:47There is a Filter button available, where I can come down here and click
00:50the Silverlight node in the tree to shrink this down to only the templates
00:54that have Silverlight.
00:55And I'm going to start by creating a Silverlight application.
00:58If you are following along, you can pick any location you want.
01:00By default, Visual Studio starts in Documents folder and then under a folder
01:05called Visual Studio 2008\Projects.
01:08I'm going to leave it there, and then I'm going to choose a nice new name for
01:11my SilverApp and then click OK.
01:18The next thing that Visual Studio is going to do is ask me if I want to create a companion site.
01:23For this first demo, I don't want to create the companion site.
01:25So, I'm going to uncheck the checkbox and click OK.
01:29And here's our first screen.
01:31Over on the right side of the screen is the Solution Explorer and it shows all
01:35of the files who were are added by this template.
01:36There is my project name.
01:39Here are two files.
01:40There is an App.xaml file and a MainPage.xaml file.
01:43And inside the MainPage.xaml is this Grid, which is going to be used to contain
01:47all our elements if we want to show to the end-user.
01:50Right now, the only thing I'm going to do is change the background color of the Grid.
01:55I'm doing this by typing in the IDE and Visual Studio has an IntelliSense
01:59engine, which prompts me for names like Background and the colors that I want to choose.
02:03Okay, I'm done. I can save this.
02:07Next thing I want to do is run it to demonstrate how it works.
02:09We'll go up and choose Build, which will compile my application, and then
02:13secondly, I'm going to go choose Debug, Start Debugging.
02:16Now what I've just done is I've launched the Browser.
02:23In this case, it's my default Browser that is IE 8.
02:26It's loaded our test page in.
02:28It's colored the entire surface area of the Browser with the
02:31Silverlight application.
02:32If I right-click anywhere inside the Browser, you can see that it's Silverlight.
02:36I don't get the HTML menu you usually do, and lastly, it has attached the
02:41debugger in Visual Studio to this, so I can put breakpoints in my code and walk
02:45through the code if necessary.
02:47There's another movie in this series detailing more about debugging.
02:50Okay, I think I've seen enough.
02:51We'll close down the Browser, which will also detach the Debugger and return me
02:55back to Visual Studio.
02:57Another way of creating a project is to go to File>New>Project.
03:03I'll call this one Silevrapp2. I click OK.
03:09This time, I am going to allow Visual Studio to create this Host the Silverlight
03:12application and a new website.
03:14I have two possibilities here for the type of project that I'll create.
03:18I can have it create a web application or an ASP.NET website.
03:23I don't want to go into the differences right now between the two.
03:25Those are almost identical.
03:26For this one, I'm going to choose website, and then I get to pick a name if I
03:30want, but I'm going to leave it at the default and then I'm going to click OK. What do you think?
03:37Does it look any different?
03:38I have a XAML page here.
03:41But if I look in my Solution Explorer on the right edge of Visual Studio, it
03:44seems like there is a lot more items over here.
03:47If you look closely, you can see that Silverapp2 is down here, and there is my
03:50App.xaml, there is my MainPage, but up here is a brand-new website that
03:54contains regular ASP.NET items, like here's an ASPX page, here's an HTML page,
04:00here's a default page.
04:01If you're already an ASP.NET Developer, this should be very familiar to you.
04:06When I go up and I choose Debug start this time - by the way you don't have to do Build first.
04:11If you choose Debug start, it'll automatically run the Compiler first and then
04:15launch the Debugger.
04:16So, you could save yourself a step by going up here, Debug > Start Debugging.
04:21Visual Studio has to decide which of these two projects to run.
04:25It can only attach a Debugger to one process at a time.
04:27It determines that by looking at what you called your startup project.
04:31Since Silverlight2.Web is in bold, that tells me that that's the startup project,
04:36but you can change that by right- clicking, and say Set as Startup Project.
04:41Now you see Silverapp2 is in bold.
04:43If I run this application by pressing F5, another way to launch the Debugger,
04:52you'll see that I get the page.
04:54It's kind of hard to tell because it's white, but it's the Silverlight page.
04:57You see that it's loading the TestPage.html.
05:01But if I come back to Visual Studio and make this my startup project and run, by
05:08pressing F5, a couple of different things happen.
05:11First of all, ASP.NET is not set up for debugging automatically.
05:15There has to be a change in the configuration file, and Visual Studio is
05:18prompting me and asking me if I want to go ahead and do that.
05:20Is it okay to modify the Web.config to enable debugging? Yes.
05:27And then another thing that's different is now it says a local host up here,
05:30which means it's running off my local web development server, whereas before it
05:34was just running up the hard drive and it's now learning that TestPage.aspx and
05:37if I right-click, you see it says Silverlight in here.
05:39What you have just seen is a lot closer to the real world.
05:44Most of the time when you create a Silverlight application, you're going to put
05:47your Silverlight item on a webpage, because that's how your users are going to
05:52find your application.
05:54There are other movies in this series that are go into a lot more details on
05:57how you add the Silverlight control to a page - what's actually in here.
06:02We'll take a very brief look, but on this HTML page, I just double-clicked on it,
06:06in Solution Explorer.
06:07There is a lot of stuff going on here, which will make more sense when you look
06:11at some of the other movies in the series.
06:13Down here, of course, it's where it's actually adding the Silverlight
06:16application to the webpage.
06:18That's about all I have for this movie, but I hope you saw was that it's very
06:21easy to create Silverlight applications using the Visual Studio IDE.
Collapse this transcript
Using Expression Blend to create a Silverlight project
00:00Expression Blend is a great editor for working with Silverlight applications.
00:03In this movie, I'd like to show you how to create a Silverlight project in Blend.
00:07I've already got the Expression Blend 3 interface open and I'm going to create a
00:11brand-new Silverlight application.
00:13Naturally, you go to the File menu and look for something like this File>New Project.
00:19When you click on the menu item, it opens this New Project dialog.
00:24You can see that Expression Blend works with both Silverlight and WPF.
00:27In Silverlight, there are several possible projects. The two we're interested
00:31in today are the Silverlight Application and the Silverlight Applications + Website.
00:36This creates a standalone app and it creates a default webpage to host it on.
00:40If you choose this option, it creates the application plus the companion website
00:44an ASP.NET website.
00:45Let's choose this first option.
00:47I'm going to just leave it at the default name, SilverlightApplication2, and it's
00:52going to go into the default location
00:54on this machine. You can choose to put the application wherever you like to go on
00:57your machine and click OK.
00:58Now in Expression Blend, you have a Project Explorer over on the left edge of the screen.
01:05At the top is your Solution, below that is your Project, and here are our two
01:09XAML files: App.xaml and MainPage.xaml.
01:13I'm going to edit the background of this page and blend the way you edit the
01:17background is you would choose the Route Canvas and you'd go to the Property
01:22window on the right edge and pick your color.
01:24In the previous movie, I picked an orange color.
01:26Now I'm ready to run the application.
01:29In Visual Studio, you press F5 to build and run the application and attach a Debugger.
01:35Expression Blend doesn't have a Debugger, but you'll launch the Silverlight
01:39project the same way.
01:40You go to Project and you can choose Build, or you can choose Run, which is the
01:46same shortcut key, F5.
01:47So, let's just try that. F5, and there's our Silverlight test page.
01:54There's our Silverlight user control.
01:56With Expression Blend and Visual Studio, you use the same project file format
02:04and the same solution file format.
02:06So, that means that I should be able to open this brand-new project I created
02:09inside Visual Studio.
02:10Let's test that out.
02:11I'm going to launch Visual Studio.
02:12I'm going to go to File>Open>Project.
02:18We'll go to the default location, which is Documents/Expression/Blend 3/Projects,
02:28and here's the new application that it's created.
02:31Here's that SLN file, which both Blend and Visual Studio understand, and now I'm
02:38editing the project in Visual Studio.
02:40And now, of course, I can do things that will make sense for a developer.
02:43I can put breakpoints in here.
02:44I can edit my code.
02:46I can watch the code as it's running.
02:48Let's just verify that it works.
02:49Press F5, and it looks exactly the same as the website we had in
02:54Expression Blend.
Collapse this transcript
Touring the Visual Studio interface
00:00When you become a Silverlight Developer, you're going to spend a lot
00:02of time in two tools.
00:04Visual Studio is a great tool for writing code and editing your XAML.
00:08The second tool is called Expression Blend.
00:11It has the designer that Visual Studio is missing.
00:13Visual Studio does not have a visual designer for Silverlight 3 applications.
00:18I have Visual Studio open and I'm going to go up to the File menu and
00:21choose File>Open>Project.
00:26And then I'm going to go to the Exercise Files folder, the Getting Started
00:29folder and then open this PixelSmith Tour folder, and then double-click on this sln file.
00:35Sln stands for Solution.
00:38Solutions can contain multiple projects.
00:40I'll click on the Solution.
00:43You'll note over in the Solution Explorer on the right edge of the screen, a
00:46number of files and folders have appeared.
00:49I'll explain more about this in a couple of minutes.
00:50We also have details about the Solution Explorer in another movie in this course.
00:54I am going to open this MainPage.xaml file, and what you see here is the main
01:00programming language for Silverlight. It's called XAML.
01:03XAML is XML.
01:04There is also a code version of this.
01:07I can press F7 while I'm in this window to launch the code version of the same control.
01:12As I mentioned, there is another tool that's called Expression Blend.
01:14I'm going to switch over to Expression Blend now.
01:16I've already opened the same solution in Expression Blend and I've opened the
01:20same MainPage.xaml file.
01:23Blend has this beautiful designer for editing your UI.
01:26I have another movie in this course detailing more about Expression Blend.
01:29For now, I just want to show you one demo.
01:31I'm going to come over to the Toolbar and click on rectangle.
01:34Then we're going to draw the rectangle in my Artboard and then we'll save the project.
01:40I want to turn back to Visual Studio.
01:43Visual Studio detects the changes that I've made inside Expression Blend.
01:48That's because both Expression Blend and Visual Studio are sharing the same files.
01:52I'm going to click Yes to All, and now the changes have been imported
01:56into Visual Studio.
01:58For the rest of this movie, we're going to stay in Visual Studio.
02:00Let's start by looking at the Solution Explorer on the right side of the screen.
02:04Solution Explorer is a virtual representation of your file system.
02:07It contains folders and files from your project.
02:11Below that is the Properties window.
02:13The Properties window is not used a lot during the Silverlight development,
02:17but it is in other application development like WPF or WinForms.
02:21On the left side of the screen is a dockable Toolbar.
02:25If you mouse over this section of your screen, the Toolbar will pop out.
02:29You can dock it to the side by clicking on the Auto-Hide button.
02:33Once you've done that, you can drag these items to your XAML file.
02:37Let me show you an example.
02:38I'm going to scroll down in my XAML file, to this area.
02:46Line 101, and then I'm going to take this CheckBox control and drag it over to my XAML page.
02:52And as you can see, Visual Studios dubs in CheckBox and then CheckBox.
02:57Now it's time to compile your application.
02:59I'm going to unpin this toolbox, so we get more screen real estate to see.
03:03I'd like to spend a minute or so talking about the Build menu.
03:06In any .NET application, you have to compile your application.
03:10In Visual Studio, the compile process is handled by MS Build, which is a build engine.
03:15When I go to the Build menu, I have three choices: Build Solution, Rebuild
03:21Solution or Clean Solution.
03:23I should say I have six choices because I also have Build, PixelSmith,
03:27three times down here.
03:28The difference is you may have more than one project inside a solution.
03:34I can choose to build the entire solution in all of its subsets or I can
03:38choose to build only one.
03:40It's also possible to right- click over here and choose Build.
03:45When you choose Build, it goes out and verifies that all your code is correct.
03:49As you can see, I have some errors in my code.
03:51The name 'media1' does not exist in the current context.
03:55And it's having trouble finding something called PhysicsControllerBehavior and
04:00so I have got some issues to fix in my application before I can successfully
04:03deploy my application.
04:06There are three choices up here.
04:08First is a normal build process where it gathers up all the information about
04:11the Build and runs through the compile process.
04:13During the Build process, Visual Studio caches certain files to make things
04:17faster the next time.
04:19If I choose Rebuild, it throws out all the temporary cached files and
04:23starts from scratch.
04:25And lastly, if I choose Clean Solution, it throws out all the cached files but
04:30doesn't do the second half, which is to compile the application.
04:34You must have a compiled file for Silverlight to work.
04:37I have more details on the compile process in the programming chapter.
04:40Next to the Build button is the Debug menu.
04:42In the Debug menu, I can press Start Debugging.
04:46Here's the key about the Start Debugging menu.
04:47It runs through the build process first.
04:49So, if I press F5, it does everything I just told you about the Build menu, then
04:54it starts up Internet Explorer, launches my application and attaches a Debugger
04:58to Internet Explorer.
04:59I also have more details about this in another movie.
05:02You're going to spend a lot of time looking at your XAML and your code.
05:05To make your life more pleasant, you can go to Tools>Options and configure some settings.
05:10Let's do that. Tools>Options.
05:14The first thing I'm going to look at is Fonts and Colors.
05:17You get to pick your favorite monospace font or other font here.
05:20I like to use Consolas, which is a Windows Vista/Windows 7 font.
05:25It looks very nice on LCD screens.
05:27More important is the area called the Text Editor.
05:31Each editor inside Visual Studio has its own rules on how to color code the text
05:36and what are the allowed key words.
05:38Therefore, in the Text Editor section, there are specific tools for configuring
05:42each of those text editors.
05:44I want to look at the XAML Text Editor.
05:50You can control things like how your words wrap on the screen, whether or not
05:54you have line numbers - as you can see, I have line numbers turned on.
05:58I can also control things like how my Tabs are constructed.
06:02The reason I like to use two spaces while teaching is that I can see
06:05more code on the screen.
06:07In the Miscellaneous section, I can auto insert Closing tags and I can auto
06:12insert Attribute quotes.
06:13I like to have both of those enabled.
06:15And then finally, my favorite section is in the Formatting section, under Spacing.
06:20I can tell Visual Studio to layout my text according to these patterns.
06:26The pattern I like is to position each attribute on a separate line.
06:30Here's an example, right here. Header, IsSelected, FontSize and so on are all on a separate line.
06:38I like the way this looks in my screen.
06:40I also like to choose Position first attribute on the same line as start tag,
06:45which you can see here on line 110. The Header attribute is on the same line.
06:50You can also control what happens with empty lines.
06:52I like to Collapse multiple empty lines into content into a single one.
06:57Let me show you what that does.
06:58I'll click OK here.
06:59Add a couple of spaces.
07:02I'm also going to move the FontSize up to this line.
07:08So, now it's not on a separate line and I've got these extra spaces.
07:11Now I'm going to format my document.
07:13I'll go to Edit>Advanced>Format Document, or you can use this keystroke, Ctrl+K
07:21and then Ctrl+D. Watch what happens.
07:24It removes the blank lines.
07:27It also rearranges my attributes the way I like them.
07:30We'll be using Visual Studio a lot in this course.
07:33In other movies, I'll be providing you with more details.
Collapse this transcript
Touring the Expression Blend interface
00:00Expression Blend is a premiere tool for editing your UI in a designer.
00:05It works with Visual Studio as you might have seen it in another movie in this course.
00:08I'm in the Expression Blend and I've opened the PixelSmith application.
00:11For this demo, I'm going to create a brand new UserControl, which will allow me
00:15to show you some of the tools that are available in Expression Blend.
00:18To do that, I'm going to right-click on the PixelSmith project and then choose
00:22Add New Item, select UserControl.
00:27I'm just going to leave it at the default name and click OK.
00:30This is the work area.
00:32You can zoom in and zoom out of the work area by moving down to this Zoom button.
00:37This allows me to show you one of the tools inside Expression Blend.
00:40Do you see the four-headed arrow there?
00:42That indicates that I can hold down my left mouse button and I can now move up
00:46or down, or left or right to modify the value:
00:50up, down, left or right.
00:55Many of the UI elements inside Expression Blend use this feature.
00:59There are tools like snap to grid and snap to guidlines available.
01:04If you want to add an item to this area here, this is called the artboard -
01:08this is where you do all your work -
01:09you need to move over to your tool panel and slide down and pick one of these
01:12items like a Button.
01:14If you click and hold, a slide-out menu pops.
01:16Let's try that again.
01:17I'm going to click on the Rectangle and hold.
01:20If you just do a click, it only selects the item.
01:22So, here there is a single click, click and hold.
01:25I'm going to choose the Button and then I'm going to draw the button in the artboard.
01:30As I'm drawing the button, you can see sizing handles. You can see that I've snap to
01:36the button above it with the red dotted line.
01:39Once I've drawn the item, I can choose one of these two selectors.
01:45Blend has two selectors.
01:46It has something called the Selection (V) and it has the Direct Selection (A).
01:49Note the letter A and the letter V. That means that I could press V to turn on
01:54this tool and A to turn on this tool. Let's try it.
01:57A, the cursor changed to an arrow.
02:00Now I'm going to press V. The cursor changes to a slightly different arrow.
02:04Also, I can see some different handles showing up.
02:06Let's click in this button.
02:07I'll press A. So, with A, I can move the item around.
02:11When I have V selected, then I can see the margins.
02:16I can also see the sizing handles.
02:18I'm also able to manipulate things like the rotation of the object.
02:24If you move your mouse over the edge of this dot, it changes from a two-headed
02:28arrow to a rounded arrow.
02:30Now I can grab this item and rotate the angle.
02:34It's just setting something called a Transform, which you'll learn about
02:37later in the course.
02:39If you note, there's a little white dot in the center of the button.
02:42That's the rotation center point.
02:43So, I can grab that and slide it to a new location and now when I rotate, the
02:48button will rotate around that center point.
02:51You can also apply things like skewing and many other transforms.
02:56I'll delete the button by pressing the Delete key and then I'm going to move
02:59this up where I can see it.
03:01For the next demo, I'm going to draw an ellipse.
03:04I'll go over to my Tool panel, click on the Rectangle, hold, then choose Ellipse
03:09and then I'll come over here and draw my circle.
03:12If I want a perfect circle, I can hold down the Shift key while I'm drawing.
03:18I'll choose V to move it to a new position.
03:21For the next portion of my demo, I want to talk about the Properties panel.
03:25You'll spend a lot of time over here modifying the objects in your artboard, so
03:29you should learn how they work.
03:31The first section is the Brushes section.
03:33You can collapse this section by clicking on the white triangle, like so.
03:40When you editing things like Color, you can come over here and choose to dial in
03:44the Color or you can choose to use the Lightness and Hue and Saturation.
03:49If you do not see those values on your machine, you might need to click and hold
03:53on one of these letters, like this, and then choose like Red, Green, Blue.
03:58If you're looking for a property and you don't see it, one quick way to find it
04:02in Blend is to use the Search feature.
04:04I can come up here and type search.
04:05For instance, let's look for width, W-I-D, and it shows there are three
04:10properties in the Property panel with the word Width and in the Width property
04:14and the MinWidth property and the MaxWidth property.
04:16There is also these little pop out areas.
04:18I'm going to turn off my search.
04:21When you're in a panel, like say the Appearance panel, there is a little
04:26button here at the bottom called Show advanced properties, which allows you to see more details.
04:30This just gives you a feel for what you can do with Expression Blend.
04:33In other movies in this course, I'll be providing you with more details.
Collapse this transcript
2. Project Overview
Understanding the Visual Studio project structure
00:00When you create a Silverlight project in Visual Studio, it uses a template to
00:03generate an assortment of application settings and files and it places these in
00:07one or more projects.
00:08In this movie, I want to explore exactly what items are produced for us.
00:12I'm in Visual Studio. If you're following along in the Exercise Files, you're
00:15going to want to open the solution called UnderstandingProjectStructure.
00:20I'm going to make my Solution Explorer a little bit wider here.
00:22When I created this project, I permitted Visual Studio to create the companion website.
00:27So, I ended up with two projects inside one solution.
00:29Just for review, a solution is a container for multiple projects and a project
00:33is a container for multiple files and settings.
00:36The first project is called UnderstandingProjectStructure, this is my
00:40Silverlight application.
00:41My second project is called UnderstandingProjectStructure.Web.
00:45This is my companion website.
00:46Just for review, the project that has the bold text is the one that will start.
00:51Since I want my website to start, I would come down here, right-click, choose
00:55Set as StartUp Project, and now when I run the application by pressing F5 - we're
01:01choosing Start Debugging -
01:02it will launch this application.
01:05All Visual Studio projects have a Properties node and a Reference node in the tree view.
01:11The Reference node contains all of the files that are going to be included when
01:16I compile the application.
01:18The Properties node is where I can change some of the properties affiliated
01:22with this application.
01:23Let's take a look at the Properties in a Silverlight app.
01:27Double-click on the Properties node. That will open a new window and on the side
01:33of the window are tabs.
01:34Each one of these tabs opens up a form where I change the settings.
01:37The only two tabs we're going to look at today are the Silverlight tab and the Debug tab.
01:41In the Sliverlight tab, I can change the name of my output file.
01:45I can change the name of my default namespace, which is a programmatic piece as part of my C#.
01:51I can change my Startup object and I can also change the name of my XAP file.
01:59The XAP file, we'll talk more about in a later movie, but this is where your
02:03application is stored along with all of your dependency files, like JPEGs and so
02:09on, and this is a compressed file.
02:11It's very much like a zip file, except it has an XAP extension.
02:14I would also like to look at the Debug tab.
02:18In here, the default setting is usually Dynamically generate a test page.
02:21What this means is if I don't have a companion website and I choose this as my
02:26startup and I try to run it, you can't run a Silverlight application unless you
02:30have a webpage to host it on.
02:32In this case, Visual Studio will automatically create a dynamic page, on the fly,
02:36for me and then load that in the browser.
02:38There are two other important files in this project. There's App.xaml.
02:43This contains application level code and it also contains a place where I can
02:47store global settings, like global variables and global stylesheets.
02:52And it also contains MainPage.xaml, which, in our case, is the only page we have
02:57in the Silverlight app.
02:59In a later movie, we'll see how to create multiple pages.
03:01That's all I want to look at in this project.
03:03I'll collapse this by clicking in the Minus sign and then I'll go down and take
03:07a look at the website.
03:08This is a traditional ASP.NET site. The key part here is that it has a
03:13Reference node and a Properties node and I'm going to go in and change some of
03:17the properties for the ASP.NET application.
03:20Remember, you do that by double- clicking on the Properties node.
03:24It also has an Application pane where you can change Assembly names and so on.
03:28But the only one I'm interested in today is the Silverlight Applications tab.
03:32So, what's going on in here?
03:34When you create an ASP.NET webpage, you might have more than one Silverlight
03:38project as part of it.
03:40Here's where I can configure Visual Studio to load those projects.
03:44By default, when I created my Silverlight project and I told it I wanted a
03:48companion website, it automatically added my Silverlight project to the
03:52ASP.NET application.
03:54Well, that's about it for the tour of the ProjectStructure.
03:56What I want you to take away from this movie is that Visual Studio uses a
04:00template to create your Silverlight application, which generated two separate
04:03projects, and each one of these projects has separate dedicated settings that
04:08make sense for that type of .NET project.
Collapse this transcript
Creating a Silverlight page/UserControl
00:00Silverlight applications are often composed of many separate UserControls.
00:04In this movie, I will show you how to add additional UserControls to your
00:06project and how to change the StartUp Control.
00:09I've opened up Visual Studio.
00:11If you're following along with the Exercise Files, you're going to want to open
00:14the CreateSilverlightControl solution.
00:17I created this project in Visual Studio, but I did not choose to have Visual
00:21Studio create the companion project.
00:23So, I just have the single Silverlight application.
00:26There is a single user control in here called MainPage.xaml. I'm going to double-
00:30click on MainPage.xaml.
00:32It'll open up the editor. I'll make the window a little bit narrower here
00:36so it's easier to see our XAML and I want you to note what it says at the top, UserControl.
00:41So, UserControl is the type that is used to create this page.
00:45Let me address the odd naming that occurs in Silverlight before I go any further.
00:48In earlier versions of Silverlight, mostly due to historical reasons, people
00:52often used the term Silverlight pages.
00:55This is in keeping with traditional names for web items, for example, webpage or Java page.
01:00In reality, however, Silverlight page was a Silverlight UserControl.
01:04That tradition has endured and in Version 3, both Visual Studio and Expression
01:09Blend create a MainPage.
01:11An Expression Blend created a UserControl, but they name it MainPage, which I find confusing.
01:16So, just as long as you know.
01:18It's called Page in the name only, but the data type that it's going to create in
01:24.NET is a UserControl.
01:26Now if that's not confusing enough, Silverlight 3 debuted a brand-new Page class.
01:32So, there really is a Page class now, which makes this even more brutal, and
01:37they should have been called Main UserControl.
01:39Let me show you the Page class.
01:41File>New>Project. Silverlight has this new type of project called the
01:48Silverlight Navigation Application.
01:51Now I have a movie dedicated to this later in the series, but I want to take a
01:55quick look at it now.
01:56I'm just going to go ahead and say OK. Do not create the companion website.
02:03Now look what's different here. There is a MainPage and if I double-click on
02:08MainPage, it's still tough type UserControl and then if I go into the Views
02:13folder, let's say, look at this Homepage here, Home.xaml. What's its data type?
02:21Page, but they put them in a Views folder.
02:24Pages are used to enable URL-based navigation of Silverlight applications.
02:29Occasionally, you're going to see things named Pages where they're really
02:31UserControls. That's what I wanted you to see here.
02:35Let's go back to the other application.
02:37Now the way you can do that in Visual Studio is go to File>Recent Projects and
02:41then choose CreateSilverlightControl.
02:43Now we're back where we were and we were talking about this UserControl.
02:46So, the point of this movie is to show you how you can add other UserControls to
02:51your project and that's really simple.
02:53Right-click on your projects, choose Add>New Item.
02:57Visual Studio will bring up a dialog saying what type of item would you like to add?
03:02It's suggesting to me Silverlight UserControl. That sounds good.
03:06I'll come in here and rename this one About, like so.
03:10I don't need to put the extension because Visual Studio will do that work for me
03:13and then I'm going to press the Enter key.
03:16There is my own new UserControl. There is a root element, UserControl. Here's
03:20my new name, About.
03:22Now your Silverlight application has a problem and the problem is I've got two
03:26pages. Which one should I start with?
03:29By default, it's going to start with the one I already have, which is MainPage, but
03:32I don't want to start with MainPage.
03:33I would like to start with my About page.
03:35So, I need to go somewhere in my code and tell Visual Studio, or more correctly,
03:40to tell Silverlight to start with my new page.
03:43That is done in App.xaml.
03:45App.xaml is where you do global events, write code
03:48that applies to the whole application.
03:50So, I'm going to open up App.xaml and I'm going to go to the C# file.
03:55This is called the code-behind file and we'll talk more about that in details in another movie.
03:59Double-click on the CS file and within this Application class is a Startup function.
04:08Functions are called methods in .NET.
04:10There is a function in here that's telling it to start the RootVisual with the new MainPage.
04:16All I need to do to make this change is go and type the name of my new class,
04:21About, recompile the application and start it up.
04:28Now to prove that I'm using the About page, we need to put some text on here.
04:31So, I'm going to double-click back on the About and go over here and I'm going
04:35to add a TextBlock, like so.
04:43The TextBlock is inside my grid. My grid is inside my UserControl.
04:46This is my About UserControl.
04:48If I did everything correctly, I should be able to go to Debug>Start Debugging
04:52and see my new page.
04:57All right, so there you go.
04:59That's how easy it is to add your own UserControls to a Project.
Collapse this transcript
Discovering what happens during a compile
00:00When you compile your Silverlight application, a number of critical actions are performed.
00:04In this movie, I'll show you what happens during the Build process and
00:07what files are created.
00:10Microsoft has a sophisticated build tool called MSBuild.
00:15Like other modern build tools, it uses XML format for the script content.
00:20You create the script that contains the set of instructions for the build tool.
00:24The instructions are arranged in the preferred build sequence.
00:27MSBuild then reads the build file and performs the task in the given order.
00:33There are hundreds of predefined tasks, and if you can't find a task that you
00:37like, the system is extensible, which means you can create your own tasks.
00:43When you create a Visual Studio project, you're really creating a MSBuild file.
00:47In fact, here is a little tip.
00:49You can open your CSPROJ file in a text editor and see the Build instructions.
00:55When you build your application in Visual Studio or in Expression Blend, it
00:59calls MSBuild, which eventually calls the language compiler.
01:02The language compiler then compiles your C# code into the finished executable. Let's go and see.
01:09Once again, I am back in Visual Studio.
01:11If you're following along with the Exercise Files, you're going to want to open
01:14the CompilingYourProject solution file.
01:18This is a very simple Silverlight application.
01:21It's a standalone app.
01:22It only has default items, App.xaml and MainPage.xaml.
01:27When you want to build your application, you go to your Build menu and you
01:30choose one of these three top options:
01:32Build Solution, Rebuild Solution or Clean Solution.
01:34The only one we're interested in today is Build Solution.
01:36So, I'll click on Build Solution. That will call MSBuild.
01:40MSBuild is going to invoke my compiler and when it's done, it's going to issue a
01:43status report in the Output window at the bottom of the screen.
01:47Here is my status report.
01:49One build succeeded or updated.
01:51I had 0 failed builds and 0 skipped builds.
01:55Now I am on a really simple project so I don't have a lot of dependencies.
01:58So, there is not much to report.
02:00If you want to read more details, here they are, like here it was compiling my DLL file.
02:06Once you have compiled your application, it also generated some files you
02:09can see that it generated a file called CompilingYourProject.dll, but where are they?
02:14You just have to know how to tell Visual Studio to turn on your hidden files.
02:17Let's go to Solution Explorer and look at this project.
02:20Really, this is a simplified version of your File Explorer.
02:23It's not showing you all the files in your project, but you can enable that by
02:27clicking on the Show All Files button up here.
02:31Now you get these little white folders.
02:33Those represents the files that are out there on the hard drive, but are not
02:37being shown to you inside Visual Studio.
02:38Why don't they show you the Bin file?
02:41Because it's generated every time you do a build, you don't need to look at it normally.
02:44What's in here? Let's take a look.
02:45Double-click on the Bin folder. I just did a debug build, so I also need to look
02:49in the Debug folder.
02:52And in here, you'll see there are five files.
02:56The most important of these is your DLL.
02:58This is the file that contains your code.
03:01Not only does it contain code, but it also contains all the XAML files from your application.
03:07So, what the compiler has done is taken your XAML files and put them inside this DLL.
03:12This is good because you never lose track of that inside your DLL.
03:16Next is this one called CompilingYourProject.pdb.
03:20PDB stands for Portable Debug and these are your debugging symbols.
03:24If you're in the Visual Studio and you want to walk through your code while the
03:28application is running, it helps if you have a debug file, because it contains
03:32things that make your debugging experience more pleasant.
03:35The third file I would like to talk about is AppManifest.xaml.
03:40This file contains information about your application dependencies.
03:43I will talk more about this in a later movie.
03:46The fourth file is this XAP file.
03:49Remember that XAP is pronounced Zap.
03:52The XAP file is a compressed file.
03:54It really is a zip file with an XAP extension.
03:57So, let's open it by tricking the operating system.
03:59I am going to click on this and change the name to zip, press Enter.
04:04Visual Studio is going to warn me that I am changing the file extension.
04:08So, I will say, this is okay. I know what I am doing.
04:10And now I am going to double-click on the zip file, and you see there is a
04:15DLL and my Manifest.
04:17Now nice thing about the XAP file is this is the only file you need to send
04:21to your web server.
04:22Everything you need for your Silverlight application to run successfully
04:25is inside this file.
04:27One more file to talk about, TestPage.html.
04:31You might remember from a previous movie that if you go to Properties>Debug
04:36and choose Dynamically generate a test page, that will create a test harness page
04:40for you to try out your application.
04:42That's what this file is here, TestPage.html.
04:46What this movie has shown is you is that Visual Studio generates five different
04:49files for your output every time you do a compile.
Collapse this transcript
Using core runtime assemblies and other assemblies
00:00Silverlight applications are client-side programs.
00:03In order for them to work, the code must be available on the
00:06customer's computer.
00:08The mechanism for deploying code in .NET is called an Assembly, which is nothing
00:13more than an especially configured executable file.
00:16In Silverlight, that will be a DLL file.
00:19This DLL file must be downloaded to the customer's computer before your
00:22application can start.
00:24Your code also uses other .NET classes.
00:27So, when you compile your App, you'll take dependencies on other .NET assemblies.
00:31This means that these additional assemblies must also be available on the
00:35customer's computer.
00:37Silverlight has a number of assemblies that are compiled specifically for use by
00:40Silverlight applications.
00:41They contain all the types that are necessary to run your App plus hundreds
00:46of additional classes.
00:48In this regard, they are similar to the .NET and WPF assemblies.
00:52The Silverlight framework has been relentlessly reworked to be small and lean.
00:56This is to enable a fast install on the client's machine.
00:59It has also undergone a complete security refactoring to ensure that it is safe
01:03to run in a browser.
01:05There are two sets of assemblies available.
01:08The first set is the Core Assemblies.
01:11These are essential libraries and are pre-installed on the client's computer.
01:15So, the way of saying this is when the user install Silverlight, the Core
01:19Assemblies are automatically copied to their machine.
01:21I've highlighted the Core assemblies on the right side of this slide in orange.
01:27There are five of them.
01:29The nice thing about this is your application does not need to download these
01:33files to the user's computer.
01:36The second set are called the SDK Assemblies.
01:40These are additional Silverlight-specific libraries.
01:43They are useful but not critical.
01:45They are specifically compiled, so they work with Silverlight, but they are not
01:50automatically added to the user's computer.
01:53If you want to use something that's in the SDK assemblies, you have to do
01:56some additional work.
01:57First, you must add them to your project in Visual Studio or Expression Blend,
02:01and second of all, those must be downloaded to the user's computer.
02:05So, the deficit of this approach is your download is now bigger because you've
02:09decided to choose a Silverlight SDK Assembly.
02:13Let's see how this works.
02:15I have switched back to Visual Studio.
02:16If you're following along with the Exercise Files, you're going to want to open
02:19the CoreRuntimeAndOtherAssemblies solution file.
02:23This is a simple project.
02:24A core message for this movie is to show you what files are generated.
02:27So, I just have it very simple application with one page.
02:30I am going to build my application, and then I am going to go look at
02:33my References folder.
02:35If you open the References folder, you'll note that I just have the Core
02:38assemblies in here, mscorlib, System.NET and so on.
02:42When I compile the application, it will create that DLL in my XAP file.
02:46Let's go and take a look at those.
02:47I am going to go up here and choose Show All Files, and then I am going to
02:51scroll down to the Bin folder and double -click and drill down, double-click on
02:55Debug and we're going to find this XAP file.
02:59You might recall that the XAP file is just a ZIP file with another extension on it.
03:04In order to look at the contents of this I just need to go and change the
03:07extension to zip, satisfy Visual Studio that I know what I am doing by clicking
03:14on Yes, and then double-click on this ZIP file.
03:17Now what I want you to see here is there is only my DLL and a single AppManifest file.
03:24None of the Core DLL files are in here because they are already on the user's
03:27computer and Silverlight assumes that.
03:29I am going to add a reference to an SDK assembly.
03:34I think before I do that though I should delete the ZIP file, because it's going
03:38to generate another XAP file and this will make things easier for me later.
03:42Then I am going to go up to my References folder, and I'm going to tell Visual
03:46Studio to use a SDK Assembly.
03:48Right-click, choose Add Reference, scroll down, and find
03:53System.Windows.Controls.
03:57This assembly contains interesting controls like the TreeView control.
04:02Click OK and what do you see that's different?
04:05I now have a new node in the Reference folder called System.Windows.Control.
04:10If I compile my application and go down to my Debug folder and open up this XAP
04:16file, again, I'll have to change the extension, choose Yes, double-click on the
04:25ZIP and here is what I want you to see. System.Windows.Controls.dll is inside my XAP file.
04:32When I deploy my application and the user runs it for the first time, it's going
04:36to take this file, download it to the user's computer, and it's now going to
04:40have a copy of System.Windows.Controls.dll for it to use locally.
04:45It's also possible for you to use your own assemblies.
04:50So, I'll show you an example of how to do that.
04:52I am going to go up here and collapse my main Silverlight Project, and then I am
04:58going to add a brand new project to the Solution.
05:00I am going to right-click on the Solution and choose Add>New Project.
05:06What I want to do is build a library that I will use in multiple
05:09Silverlight applications.
05:11So, the key thing that I need to do here is tell Silverlight this is a
05:15special version of a .NET assembly that works with Silverlight Apps, because
05:19they are different, they have different security contexts than say .NET
05:23WinForms or .NET WPF Applications.
05:26So, I use the Silverlight Class Library, not the normal .NET Class Library.
05:31So, I'll choose Silverlight Class Library, and I'll call it something
05:34like LyndaLib for lynda Library and then I'll create a Class in here, add a new class.
05:44I have this VideoGenerator Class, and now I am ready to compile.
05:54I have two projects.
05:55So, if I tell Visual Studio to compile, it's going to compile both of these.
05:59What I want to have happen is I want this DLL that's generated for LyndaLib -
06:02let's go look at that, here is the Bin folder, here is the LyndaLib.dll -
06:11what I want to do is move that into my Silverlight XAP file.
06:20It's really easy to do in Visual Studio.
06:22I go to the Silverlight application and I add a reference here, just like before,
06:28Add a Reference. Remember I am in the Silverlight App now, not the lynda
06:30Library, and I click on the Projects tab.
06:34These are all the known the .NET libraries that work with Silverlight.
06:39It doesn't know about my library however, so I will go to Projects and choose LyndaLib.
06:44By doing this, Visual Studio will help me by every time I do a compile,
06:48it will first make a copy of LyndaLib in this folder, and then I will take it and
06:54copy it over to this Bin folder.
06:57Let's see how that works, Build Solution.
07:02Now it says two successful builds.
07:09I also can see I have a reference now to LyndaLib in my References folder and if
07:14I drill down into the Bin folder, I'll have to delete this ZIP file, so I don't
07:19get a name conflict later.
07:21I am going to rename the XAP file.
07:25You should be getting used to this by now. Press OK or Yes, and then
07:33double-click on the ZIP file, and what's the key thing to takeaway here?
07:37LyndaLib is in my XAP file.
07:40So, now this will get downloaded to the user's computer and it's available.
07:44Can you see any problems with this? I can see one.
07:47Every single time you run this application it has to copy these files to
07:50the local computer.
07:51If these are big files, it's going to take a while.
07:53That's going to mean that your application is going to be slower starting.
07:56Now what I can do is I can cache these.
07:59Silverlight has special cache.
08:00It's not the web cache.
08:02It's a special Silverlight cache.
08:04What I can do is tell it to stash these in the cache.
08:07I will stash these files in the cache.
08:09And then the first time I run the application, it will put them in
08:11the Silverlight cache.
08:12The second time I run the application, it will use the local copy and I won't
08:15have to download these anymore.
08:16This is another simple fix.
08:18I'll show you how to do it.
08:18We'll go up to Properties.
08:21Again, this is my Silverlight App, double-click on the Properties node, come
08:25over here to the Silverlight tab, and choose Reduce XAP size by using
08:29application library caching. Done. Save the project. We'll choose build.
08:34Let's see what's different.
08:41There is the ZIP file for System.Windows.Controls and here is my
08:45CoreRuntimeAndOther. I don't need that, and I'll delete this one again.
08:49Now, there is two ZIP files and they'll both get downloaded and the contents of this
08:53file will go in the local cache.
08:56By now, you should have a better idea of what files are created and stored in the
09:00XAP file whenever you do a compile.
Collapse this transcript
Deploying your Silverlight application
00:00Once you have compiled your Silverlight application, you'll want to share it with the world.
00:04To do that, you need to deploy it to a web server.
00:06Microsoft has simplified the deployment story by creating the XAP File format.
00:11The XAP file, as it is commonly called, contains all the necessary files for your
00:15application in a nice, compressed format.
00:17Just upload the XAP file to your server and you are ready to go. Well, not quite.
00:22You are going to need a webpage to host your Silverlight content.
00:25I talk more about the host pages in another movie in this series.
00:29You also must configure your web server to deliver the XAP file when asked.
00:34If you have Windows server 2008, you are in luck, as IIS7 on that operating
00:39system is already configured property.
00:42For other servers, including Apache, you'll need to set the MIME type for the
00:47XAP file extension.
00:49I'd like to show you that.
00:50I would like to point out that in this demo I am going to be using my web server.
00:54If you don't already have your own web server set up, you are not going to be
00:56able to follow along in the example.
00:58I am back in Visual Studio.
00:59If you are following along in the Exercise Files, you are going to want to open
01:03the Deploying solution file.
01:05Again, this is a really simple application.
01:07The only thing in here is a comment that says what I just said in the intro.
01:11Your web server has to have the MIME type for the .XAP extension.
01:16You can use any web server you want, and here is the actual extension and the MIME type.
01:22This is the newest MIME type.
01:23Let me go and show you how it configured this in IIS.
01:26I am on Vista, so I am going to come down here and choose Remote/
01:32Desktop Connection.
01:33I am going to log in to my web server and click OK.
01:47This is Windows Server 2008 and I've opened up IIS. I am looking at the
01:51different websites that are available on this web server.
01:53And the one I want to modify this one, which is called Shazzam-tool because I know
01:57this one has a Silverlight application available.
01:59So, I am going to double-click on Shazzam-tool and then I am going to go over
02:04here and double-click on MIME Types.
02:07These are all the registered MIME Types that are available to serve from this server.
02:12If you scroll down to the X's, you'll see that XAP extension is already there.
02:17But if it wasn't, I could just come over and click on Add, type in XAP here for
02:21the extension and then copy that code I just showed you in the demo and here is
02:26the MIME Type and click OK.
02:27That's all you need to do to set it up.
02:29Apache and other servers are just as easy to configure.
02:33So, that concludes the remote connection.
02:35Let's close that window.
02:35Now you are almost ready.
02:38All you need to do now is upload your files to the server and you are ready to go.
02:42All you need now is a host HTML page.
Collapse this transcript
Starting up your application
00:00Get ready to take a trip through the Start up process of a typical
00:02Silverlight application.
00:03The key players in this movie are the Application Manifest file and the
00:07Silverlight plug-in.
00:08I'd like to walk you through the process of what happens when you ask for a
00:11page that contains Silverlight content.
00:13The first step is the user requests an HTML page from the server, downloads the
00:17page and detects that there's Silverlight content in the page.
00:21It does this by seeing that there is an object tag on the page. Then the browser
00:26loads the Silverlight plug-in because of the MIME type.
00:28Once the Silverlight plug-in is loaded, the browser goes back to the server and
00:32asks for the XAP file.
00:34I'd like to show you that process.
00:36I'm using a tool called Fiddler2.
00:38Fiddler is a HTTP Debugging Proxy and it let's us monitor traffic from a website.
00:44I've started up Fiddler and now I am going to go to my browser and ask for a
00:47page that I know contains Silverlight content.
00:49I am going to www.shazzam-tool.com, shazzam-tool is a Pixel Shader Utility that
00:54I wrote that you can use to generate your own Custom pixel shaders.
00:57Now I am going to go back to Fiddler and see the traffic that happened.
01:00I've got two requests here.
01:03The first result is a 200, which says I got the page back.
01:06Then there is a 304 result, which says we had to go back to the server and get
01:11ClientBin/Shazzam.Silverlight.xap.
01:14Now the XAP file is on the local computer.
01:17The browser opens a XAP file, which you remember, is just a Zip file and reads
01:21the AppManifest.xaml file.
01:24I'll show you that.
01:25If you're following along, you are going to want to open this StartingUp
01:28Solution and then click on the Show All Files button.
01:32Build your application and then look in your Bin folder for the XAP file.
01:41If you've changed the extension on this to zip, you can open it up with the
01:48standard Zip utility.
01:51Here is the files that are inside the XAP,
01:53the AppManifest and all my dll's that I need.
01:56Next step is the Manifest contains a list of assemblies to load, so let's look
02:01at the Manifest file.
02:02I am going to double-click on AppManifest.xaml.
02:08On the bottom-half of the screen, you see there are three assembly parts.
02:12It needs the StartingUp.dll and it needs System.Windows.Controls.dll and it also
02:17needs System.Windows.Data.dll.
02:21That's telling the Silverlight runtime what it needs to load into its own process.
02:27The plug-in then loads the Silverlight runtime and then loads those assemblies.
02:31Next, it goes back to the manifest and reads the manifest to find out
02:34information about your StartUp class.
02:36Again, we'll go into the AppManifest and see that up here in the top line.
02:46Here is the class that it needs instantiate, StartingUp.App, and here is the name
02:50of the assembly that contains the code where you will find this class.
02:54There is also a note to Silverlight, which runtime version it needs.
03:00App.Constructor raises the startup event, which we can catch in our code.
03:05Our code run a startup procedure and creates the main page of that class and
03:09shows it in the plug-in by setting it to the root visual. Once that happens, your
03:12code starts running.
03:13Once your application is running, it will continue to run forever, unless one of
03:17the three things happens:
03:18there is an unhandled exception, the user navigates away from the host page or
03:22finally the browser is closed.
Collapse this transcript
Embedding Silverlight content on web pages
00:00I'd like to talk for a minute about the differences between a Web page and a
00:03Silverlight application.
00:05Both are stored on a web server. Both are downloaded to the client computer.
00:10The webpage, however, is interpreted by the browser engine.
00:13The Silverlight application ultimately must be run inside the Silverlight
00:17runtime. To make this work, the Silverlight application must be hosted on a
00:21webpage and must be placed within the Silverlight plug-in tag.
00:25This plug-in is ultimately responsible for launching your App through
00:28the client's browser.
00:29This movie teaches you how to create the plug-in on a web page.
00:32I'm in Visual Studio.
00:34If you're following along with the Exercise Files, you are going to want to open the
00:36EmbeddingInHost Solution file.
00:39This is one of those projects that has a Silverlight application, called
00:44EmbeddingInHost, and a companion website.
00:46There is nothing new to talk about in the Silverlight application.
00:50We are going to spend most of this demo in the website.
00:53For convenience sake, Visual Studio creates this ClientBin folder inside my
00:58ASP.NET website and every time I compile my Silverlight application, it copies
01:04some files into this ClientBin.
01:05Let's see what's in here.
01:07There is my XAP file.
01:09There is my XAP file.
01:09You remember from previous movies that that's what contains our compressed data.
01:14So, for convenience sake, Visual Studio copies that over here, and the idea is,
01:18at some future point,
01:19you're in ASP.NET Web Company and you're going to deploy this to the application.
01:24All you need to do is copy this entire website, just directly over to your web
01:28server and you are done.
01:29It's got the XAP file in there.
01:31This, however, doesn't make the XAP file up here on the page itself.
01:35To do that, we're also in luck, Visual Studio creates these two test pages.
01:39I'll think I'll take a look at what's inside this HTML page.
01:43The difference between the ASPX page and the HTML page is very minor.
01:47It's just a few characters difference.
01:50This is an HTML page.
01:51It's got your normal head tags, body tags, these are some CSS items up here at the top.
01:57We've got some JavaScript in here, including one that's specific to Silverlight.
02:04We'll come back and talk about that in a minute.
02:06Here is my <body> tag, and let me narrow this down a little bit, so it's little
02:11bit easier to read the comments.
02:13I have a note here that says, note there is no Silverlight XAML in the host page.
02:17The XAML is over here in this MainPage.xaml file.
02:22Over here in the web page, we are just responsible for loading the Silverlight
02:25plug-in and telling it which XAP file to load.
02:28Let's skip over this <div> tag and come down here to the <object> tag.
02:33This is the actual plug-in.
02:35It's an <object> tag that tells the browser to load the Silverlight plug-in and
02:39then down here, it's says the <object> tag will load the Silverlight plug-in
02:43for version 2 or 3.
02:45Then down here, it sets the width and height of the object to 100% and 50%.
02:48Like it says here, by default the plug-in occupies 100% of the page, but I can size it as necessary.
02:53So, if I run this now, verify that this is bold, press F5 to run, Enable
03:00Debugging, Yes, that'll be okay,
03:03and you see that the top-half of the page is the Silverlight control and the
03:08bottom-half down here is HTML. This is HTML. This is Silverlight.
03:16This is the plug-in. I can also specify to the plug-in settings, like
03:20I'm saying, look here.
03:21I would like to use this XAP file, ClientBin, go to the ClientBin and find a
03:28file that called EmbeddingInHost.xap and load that in.
03:32If there's an error, I have a JavaScript error code that I would like you to run.
03:36That's up here in the JavaScript area and there's the onSilverlightError.
03:40In case an error happens in your Silverlight application, it just whites out
03:45the Silverlight area on the screen and it doesn't cause any more problems with a website.
03:51I also have set the background color to yellow and I specified
03:55the minRuntimeVersion.
03:57This is the version number for Silverlight 3. And so if I were to change this to
04:01like say number 4 here and try to run this application,
04:04what you think would happen?
04:05It says I can't load this. I can't find a version of Silverlight, with
04:10this version number.
04:11You need to go and load it somewhere.
04:12I have a movie later in the series, talking about how to change the version
04:16numbers of your application and how to control this screen.
04:23That showed up because I have this setting here set to true,
04:25"autoUpgrade"="true".
04:27Also, I have, down here, the alternate content. If the plug-in is not
04:30available, show this link.
04:31There is a lot of other parameters available here, splashScreenSource, whether
04:37it's a windowless application and many other pieces.
04:39So, let me recap what we seen here.
04:41You can write your own HTML very similar to what I've shown you or you can let
04:45Visual Studio or Expression Blend write the Silverlight plug-in in HTML for you.
04:49I would encourage you to let Visual Studio do it because it's so much easier.
Collapse this transcript
Understanding the end user install process
00:00There are three possible scenarios when the user arrives at your website: they
00:04have Silverlight 3 installed on their computer, they have a previous version of
00:08Silverlight installed on their computer or they are living in a sad, depressing,
00:12Silverlight-deprived world. There is good news, though.
00:15It only takes a few seconds to resolve Silverlight and best of all, the
00:18Silverlight plug-in takes care of all the details.
00:20Let me show you what the install experience looks like on a computer
00:22without Silverlight.
00:23I already have Silverlight installed on this computer and I'll bet you're
00:27probably in the same position.
00:28So, I'll show you how to test your application to simulate not having
00:32Silverlight 3 installed.
00:34I have Firefox open and I'm going to go to a website that I know has Silverlight.
00:38I'm going to go to my Pixel Shader Utility website called shazzam-tool and
00:43Firefox goes ahead and loads the Silverlight application into the browser.
00:47I will now switch to Internet Explorer. What I have done in Internet Explorer is
00:52disable the Silverlight plug-in.
00:54That will simulate not having Silverlight installed.
00:57To do that you go to Tools>Internet Options, select the Programs tab, then
01:02click Manage add-ons, find the Microsoft Silverlight add-on and click the Disable button.
01:12You'll get a warning that you might need to start Internet Explorer
01:14but in my case, I do not have to do that.
01:16Now I'm going to go to the same website, shazzam-tool, and Internet Explorer
01:23thinks that I need to install Silverlight.
01:25This the default look, sometime it's called a badge that you get when you go to
01:30a site that does not have Silverlight installed. The user is supposed to click
01:33here and click on this Run button.
01:37Once you click on the Run button, it'll go through the normal install process,
01:40which takes about 20 seconds.
01:42The install experience on a Mac is nearly identical.
01:45Some companies like to rebrand this user interface with their own look.
01:50I have Netflix open. I have tried to run a movie in their Instant Queue while I
01:56have the Silverlight add-on Disable.
01:58This is what Netflix shows you.
02:00You're almost ready to watch this movie, install the Silverlight plug-in now,
02:05it only takes a minute, a few other bits of information and then some promo down
02:11here about watching the movie instantly.
02:13If I click the Install Now button, I get the same installer that I saw with
02:19the more generic badge.
02:21Later in this movie, I'll show you how to create your own UI to customize
02:25the install process.
02:26I'm going to go back and re- enable the Silverlight plug-in.
02:30Back to Internet Options > Programs > Manage add-ons, choose the Microsoft
02:37Silverlight add-on and click Enable. Then click OK.
02:42There's the warning I had mentioned and now I will press Refresh.
02:47I'd like to switch to Visual Studio and show you some of the details on how the
02:51Silverlight Control manages the Auto-update.
02:54If you're following along with the Exercise Files, you're going to want to open
02:57the EndUserInstall solution.
02:59Within this solution are two projects. There is the Silverlight project itself
03:03and then there is the website.
03:05It's helpful to have them command your website to customize the HTML page that
03:10Silverlight Control lives on.
03:11I'm going to open the website and then I am going to go down to the
03:14NotInstalledDefaultLook.html page.
03:18I'm going to double-click on the file, and we are going to scroll down and look
03:22at the Silverlight Control.
03:26Here's the Silverlight Control, All the normal parameters telling it what
03:32Silverlight file to load and so on.
03:34And down here is the minRuntimeVersion I need for my application to work.
03:38And I've entered a bogus version number. There is no 5.0.
03:41So, if I try to run this page, I'll make this my Start page by right-clicking
03:47and choose Set As Start Page and then we will do a Debug>Start Debugging,
03:54the Silverlight Control detects that it already has Silverlight installed, but I
03:57do not have the correct version.
03:59This time, I get an alternate version of the UI that says this page requires a
04:03more recent version of Silverlight. Would you like is to install the latest
04:06version from the Silverlight website?
04:08If I click on Install Now, it takes me to the Microsoft site and says I can't
04:14find a 5.0 version, which make sense. They haven't shipped it yet.
04:20Also on the main page, you see this Click to install the required version.
04:25If I want to brand this with my own look, we're going to want to change this
04:30change this autoUpgrade parameter to false.
04:32Then it won't prompt us with the Microsoft default UI.
04:35So, I have another page to check that out.
04:37It's called NotInstalledCustomLook.html. Double-click here.
04:42I'll scroll down to the Silverlight section.
04:46Again, I have a future version for the minRuntimeVersion.
04:50I've set the autoUpdate to false. I've included some anchor tags in here.
04:56I've written some of my own UI.
04:58Let's see what my custom UI looks like.
05:00I'll right-click on NotInstalledCustomLook.html > Set As Start Page, press
05:07F5 to run the application.
05:11And there is my branded page.
05:13It says PixelSmith at the top, which is the application we are building
05:15throughout this course.
05:17I can click on this button to launch the Microsoft Installer or I can click here
05:21to go to the Microsoft Silverlight website and pull the file down.
05:25From this point forward, it's the normal Microsoft install.
05:28So, you can see there we can allow the plug-in to generate the Install page or
05:32we can customize the page with our own look.
Collapse this transcript
3. The Programming Side
Exploring the relationship between XAML and .NET
00:00You cannot be a Silverlight developer or designer without being
00:02exposed frequently to XAML.
00:04XAML, more properly known as Extensible Application Markup Language, is a
00:09declarative way to express your UI design and it permeates every corner of a
00:14Silverlight application.
00:16XAML is a declarative way to describe a related group of items.
00:20In Silverlight this is usually a visual tree of objects, for example, like grid
00:24with two columns and three rows,
00:25a text box you placed in column 1 row 2 and so on. Because XML is hierarchical,
00:31it's near perfect for describing the hierarchy between instantiated objects.
00:36It also makes an intuitive way to set up properties.
00:38Here is and example of a UI you might want to create.
00:41We have a couple of labels, a couple of text entry areas, called TextBoxes in
00:45Silverlight, and an activation button, just called a button in Silverlight.
00:49This is the equivalent XAML.
00:52You can see on line 10 we have a grid, on line 12 through 20, we're defining
00:57some rows and columns.
00:59On line 22 and 25 we're defining a TextBlock and then on line 29 through 32
01:06we're defining the TextBoxes and then finally we're defining the Button.
01:09You can see that the Buttons are inside the Grid and you can see that the Button
01:14has certain content and you can see that the Button instructs Silverlight where
01:18it wants to go, in this case Grid Row number 2.
01:23XAML is not exclusive for Silverlight.
01:25Since it is just a way to describe a tree of .NET object, it is possible that
01:28XAML could have been used for any .NET types.
01:31In fact, the XAML team is working with many other teams within Microsoft to
01:34enable this scenario.
01:36I'll bet that if the Windows forms team was under active development at
01:40Microsoft, you might see them using XAML as a markup language.
01:43XAML is intended to be easy to generate from tools and it makes it much easier
01:48to separate out the design from the coding tools.
01:50Conceivably, each member of a UI team could use a different tool, Visual Studio,
01:55a 3-D modeler, your designers could be using Expression Blend and the tools would
01:59emit the correct XAML.
02:01Is XAML a programming language?
02:03Well, you can certainly use it like one.
02:05Here are some of the similarities between a language like C# and XAML.
02:09They are both a programming language. They each have their own specific set of syntax rules.
02:14Each one of them can declare variables for reuse later.
02:18You can set property values.
02:20You can call methods.
02:22Methods are what Microsoft calls functions.
02:26You can wire up delegates. That means you can instruct Silverlight to call some
02:30code and you can do that in your XAML.
02:33Here is an example of some procedural code.
02:35This is a C# code.
02:37This is the equivalent of the UI that I showed you a few slides ago.
02:42I think this is a lot harder to read.
02:44It's harder to see the relationships between the items.
02:47You can see we're creating a Grid up here at the top, we're defining some rows.
02:51I have to then call this RowDefinitions.Add and then pass in the row that I added.
02:57Here I'm creating columns. Here, on line 47, I'm creating a TextBlock, setting a
03:02property value, assigning a Grid, Position.
03:05I want it to go to row# 0 and column# 0.
03:10To me, you can walk through this code and figure what it's doing, but in a
03:13hierarchical declarative language, it's a lot easier to see the relationships
03:17and it's a lot easier for a tool to show you those with Tree View and you can
03:20collapse a section you're not interested in looking at.
03:24XAML was originally created for WPF, but a lot of teams at Microsoft heard about
03:29XAML and then started adopting it for their products.
03:32For instance, the Windows Workflow Foundation team has picked it up, the XML
03:37paper specification, which is Microsoft's new open source fixed document format
03:42and finally Silverlight.
03:44XAML is just XML text that gives us all the benefits of XML, which are listed here.
03:51It's very readable.
03:52You can archive your XAML and pull it out five years from now and be guaranteed
03:57that you will be able to read it with a Text Editor.
03:59It's easy to generate from tools.
04:01It was deliberately designed to be toolable.
04:03It's validate-able, which means that you can verify that the elements you're
04:07writing in your XAML are correct and are permitted.
04:11There are two problems with XAML, however.
04:13One is that it takes a while to parse the contents and figure out what the
04:18tree of objects is and the second problem is that the files are larger than
04:22the binary version.
04:24And that translates into a longer download time.
04:26So, when you are writing your Silverlight application, you can choose to write
04:30it with XAML only, Code only, or a combination of the two.
04:35Really, as a developer, you're going to use both.
04:37Sometimes you're going to use XAML and sometimes you're going to use C# and as
04:40we go through the course that's exactly what you're going to see.
Collapse this transcript
Choosing a Silverlight programming language
00:00While you can craft your UI with XAML, the rest of your Silverlight application
00:03will use regular .NET code.
00:05If you are new to .NET, you might be wondering which programming language to
00:08use, this movie will help you decide.
00:10Silverlight is part of the .NET family.
00:13It has its own SDK and a dedicated Runtime.
00:16If you're not familiar with the term SDK, it stands for Software Developer
00:20toolkit, which is a set of development tools that allows the software engineer
00:24to create applications.
00:26It also has a re-factored set of .NET assemblies, but fundamentally, it is
00:30still a .NET technology.
00:32In .NET and in Silverlight, you write code in a .NET language.
00:36At compile time, your code is turned into a special byte code version, known as
00:40Intermediate Language or sometimes it's called IL.
00:43This IL is not ready to run on the CPU however. Instead, it has to go through a
00:49second compile at runtime.
00:50The runtime compiler is called the just in time or JIT compiler.
00:54It's the JIT compiler's responsibility to turn your IL into native code.
00:58C# and Visual Basic are the most popular of the .NET languages and I find that
01:03they have more similarities with each other than differences. Since they are so
01:07similar and compile down to nearly identical code, choosing one or the other
01:11becomes a matter of personal choice.
01:13There have been colossal debates over the last decade regarding which is better.
01:17I say use what you like or what your company chooses.
01:21For this course, I will choose C#.
01:23To prove their similarity, I have a simple example on this slide.
01:27On the left side we have C# code and on the right side we have some Visual Basic code.
01:31Line 17 on the C# code is declaring a class constructor and I'm calling some
01:37Initialize Code in here - this is some Silverlight specific code - and then in C# I
01:42have a comment that is two slashes where the comment in VB is an apostrophe.
01:48When you declare a variable on C# use the name of the type button, the name of
01:52your variable and then you say equal new Button.
01:55In Visual Basic, you say Dim b As New Button.
01:58In Visual Basic, you never put a semicolon on the end of a line, you just hit the
02:01Enter key, whereas in C# you tell the compiler you're done with the line of code
02:05by putting a semicolon there.
02:07Here I'm doing a property assignment, b.Content = and I'm assigning a string,
02:13The equivalent VB code is exactly the same except there is no semicolon on the end and so on.
02:18Here I'm declaring an If statement, checking this condition. If this condition
02:22is true then we are going to run this line of code. VB it's almost same except
02:25we have an End If at the end instead of the End }.
02:29Down here is a function.
02:31In .NET, we don't call them functions.
02:33We call them Methods.
02:34So, you declare a method by saying the scope public, what it's going to return,
02:39C# is not going to return any value, whereas in VB we use a sub for that, to
02:44signify that and then there is a name of our function and here's the parameter
02:48that's going into the function, same over here.
02:50VB uses As String whereas C# puts the string in front.
02:53As you can see, they're very similar.
02:55I'd also like to show you how to create these in Visual Studio, so let's switch,
02:59over to Visual Studio.
03:01If you're following along in Exercise Files, you're going to want to open
03:03that choosing language solution file, and what you can see here is I have two separate projects.
03:08The first one is called CodeInCSharp and the second is called CodeInVB.
03:12They look identical, until you open up this little Plus symbol here under
03:17App.xaml and you can see in here it says, App.xaml.cs.
03:21And if you open that little plus symbol in the VB project, you'll see that
03:24this is App.xaml.vb.
03:26That's how the compiler knows that this is a VB file and this is a C# file.
03:30If you double-click, you'll see C# code here and if I double-click on the
03:35App.xaml.vb, you'll see Visual Basic code.
03:39Creating a project is easy.
03:42File>New>Project and here is the key part, when you're creating a project with
03:47the different language.
03:48You want to go to the Project type and choose either Visual Basic or C#.
03:51If I want a Visual Basic Silverlight app, I would click here and then choose
03:55Silverlight application and then click OK.
03:57You've already seen how to do this in other movies, so I want to finish the process.
04:01Microsoft has been experimenting with other types of languages recently.
04:05These so called dynamic languages have some benefits for the language
04:08implementers and also for the developer.
04:10Silverlight supports several dynamic languages.
04:13There are two examples on this slide. One is called IronPython and one is called IronRuby.
04:18I'm not going to spend a lot of time on the syntax here because we're not going
04:22to be using either one of these languages in the course.
04:24I just thought you might be interested in seeing what the syntax looks like for
04:28some of these new dynamic languages.
04:30Again, it's pretty easy to figure out here on this line in Python I'm saying
04:33b.Text="Hover here" and over here in IronRuby it's b.text="Hover here."
04:38The code looks the same.
04:39It looks the same as the Visual Basic code.
04:41It's almost identical to the C# code, except the C# code had a semicolon on the end.
04:46As you have seen in this movie, there are several languages to choose from, each
04:48with special syntax that should appeal to a segment of the programming world.
04:52In this course, I'm going to use C#. Which one will you pick?
Collapse this transcript
Exploring the code-behind file
00:00In Visual Studio and Expression Blend, whenever you create a XAML file you will
00:04also get a code behind file.
00:07What does it mean when I say code behind?
00:09If you recall, XAML is really XML text, primarily used to specify the visual
00:15object tree for your control.
00:17You are likely to write code to a sister XAML file.
00:20For example, you might define a button in XAML, but you'll have to write the
00:24button click code in C#.
00:26The code behind file is the default location for this affiliated code.
00:30You can store the code elsewhere, but that requires extra work on your behalf.
00:34Visual Studio and Expression Blend always create a code behind file when you
00:37make a new XAML file.
00:39In Silverlight, each bit of code that you write is always placed in a code wrapper.
00:43This wrapper is known as a type.
00:45The most common types use in .NET are the class type and the struct type.
00:49Silverlight uses classes for the code behind file.
00:52When you write your class, you have to put it in a physical file on your computer.
00:56So, I could say code is stored in text files.
00:58This means that if you're writing code for a Silverlight control, it will exist
01:01in a file somewhere.
01:02And actually, it can exist in more than one file.
01:04So, there are several ways you can do this.
01:06I could put one class in a single text file or I can put multiple classes in
01:10a single text file,
01:11Class A and Class B. I find it's a really difficult way to work because it's hard to find.
01:16It is hard to work with other developers if you put more than one type in
01:20a single text file.
01:22And then lastly, you can split the class and put it in multiple text files.
01:27I can write class A and put it in a file.
01:31I can write other parts of class A and put it in a second file and then I have
01:35to tell the compiler to put the two of them together with the partial keyword.
01:38I would like to show you how this works.
01:41I've switched to Visual Studio.
01:42If you're following along with the Exercise Files, you are going to want to open
01:45the ExploringCodeBehind solution.
01:48I have three XAML files in this project: App.xaml, MainPage.xaml
01:54and ProductEntry.xaml.
01:56These three files represent the entire UI of my application.
01:59If you look at ProductEntry and click on this Plus sign here in the
02:03SolutionExplorer, you'll see there is another file nested underneath it.
02:08This is called the code behind file.
02:10This is this how Visual Studio represents this to you.
02:13If I double-click on ProductEntry file, it opens up the XAML page.
02:19If I double-click on the .cs file, it opens the code behind page.
02:24Now these two are linked together.
02:25So, let's explain how they are linked together.
02:29Go back to my XAML.
02:30At the top of my XAML is a tag called UserControl.
02:34This specifies what type Silverlight is going to create.
02:37It could be a user control.
02:38It could be a page.
02:39It could be other types.
02:41If you're a programmer, this means that this is the base class of our page, ProductEntry.
02:47Over here in this next line, it specifies where to find the code.
02:52So, there is two problems we have to solve here.
02:54One is where is the file that contains the code?
02:56That's over here, the physical file.
02:58And then when I compile it, it's going to turn that into a class and at
03:01runtime it needs to know which class to instantiate, so that's what this line here signifies.
03:06Go create an instance of the class, ExploringCodeBehind, which is the Name,
03:09Space, Period and the name of the class ProductEntry.
03:13So, somewhere in my code should be a class called
03:15ExportingCodeBehind.ProductEntry.
03:17Let's see if we can find it. Here it is.
03:20Here is my namespace, ExportingCodeBehind and here is my class.
03:26Notice the partial keyword here.
03:28Remember what I said earlier? Partial means I can split the code across
03:31two separate files.
03:32I have a class called ProductEntry that derives from UserControl, and again that
03:37matches the root element here.
03:39This is the root element, which becomes the base class for a type.
03:43In Visual Studio, when I press Build button, Visual Studio will create a
03:47third file that's hidden.
03:49It's normally hidden away. We can take a look around and find it.
03:52I am going to go over to my Solution Explorer and choose Show All Files.
03:55I am going to Open this obj folder, and drill down into the Debug folder.
04:01In inside here, you'll see one file for each of my three XAML files.
04:04There's a MainPage.g.cs, and a ProductEntry.g.cs and a App.g.cs.
04:12These are generated by the MS BuildEngine, and this is where the parser
04:17instructions for the runtime engine are stored.
04:20I am not going to go into a lot of details here.
04:22Also if you create any event procedures, this is where the automatic wiring up
04:25of the event procedures goes.
04:28But let's just look briefly in here ProductEntry, see that it's warning us, this
04:32was generated by a tool don't touch anything in here, and down here is public
04:37partial class ProductEntry.
04:39This is the other partial piece of my class code.
04:45Let's write some code.
04:46We've got a ProductEntry, I'd like to change the background color of the
04:50grid when it's loaded.
04:51I am going to go to my grid. I am going to type in the Loaded events.
04:57You'll learn more about this in the XAML chapter.
04:59I am going to press Tab to have Visual Studio name my event procedure, and then
05:04I am going to switch over to the code behind and you'll see that it's written
05:09this bit of code for me right here.
05:11And I am just going to come in here and say in my code, LayoutRoot, which is the
05:17name of my grid, .Background = new SolidColorBrush.
05:24I am going to choose colors .Orange and that does it.
05:33I need to go to switch my start up page in my C# file, from my App.xaml and I
05:39need to come down here and change this from MainPage to ProductEntry, and then
05:45press F5 to run, and at runtime, my code in the code behind should run and change
05:50the color of the grid.
05:50So, I wrote my code in the code behind.
05:53I have executed the code because Silverlight found the code, saw that it was
05:57part of the Loaded event, ran the code and I now see I have an orange
06:01background on my UserControl.
06:03What I like you to take away from this demo is that any code that you write has
06:07to go somewhere in code and that's usually going in the code behind the file that's
06:11affiliated with the UserControl.
Collapse this transcript
Debugging Silverlight applications
00:00Sooner or later, you'll run into problems that can only be resolved by looking at
00:03your code in a debugger.
00:04Microsoft debuggers have been first- rate for a long time, and the Silverlight
00:08debugger continues the tradition.
00:10There are two other tools I would like to mention, even though I will not be
00:12showing them in this movie.
00:14They are Silverlight Spy and Fiddler.
00:16Debugging an application is an important skill to learn.
00:18If you are new to programming or .NET, I encourage you to get comfortable with these tools.
00:23If you are a seasoned .NET developer, feel free to skip to the next movie.
00:27If you are following along with the Exercise Files, you are going to want
00:29to open the DebuggingSilverlight Solution and then double-click on MainPage.xaml.cs.
00:34In here, I have got some code. I already have a breakpoint on line 29.
00:39You may not have this red dot in Visual Studio.
00:41It's very easy to add however, just click on the gray bar.
00:44Now I do not have a breakpoint on this line. Clicking on the gray bar a second
00:48time turns it back on.
00:49You can also press F9 to turn on or off breakpoints.
00:53The Debug menu is our main entry point for all of our debugging tasks.
00:58You can choose to start debugging by choosing this menu item. That will launch a
01:01browser, launch your Silverlight application and attach a debugger to the
01:05running browser process.
01:06Let me show you an example. Press Start Debugging.
01:08I am going to click on this first button and I hit my breakpoint.
01:14The yellow arrow signifies that I am at my breakpoint.
01:17I can now step through my code by clicking on this button, which moves one line
01:22of code at a time, and I can examine each line of code or I can shut down the
01:26application by clicking on this button here, the Stop Debugging button.
01:30These buttons may be in different locations on your toolbar.
01:32I am going to stop the application.
01:34Occasionally, you need to attach a debugger to an already running process.
01:37Let me show you how to do that.
01:39I am going to launch the application without attaching a debugger.
01:42I click on the button and I do not hit my breakpoint, even though the code in
01:48there is running. I then go to Debug> Attach to Process, scroll down the list
01:55till I find the browser I am looking for, iexplore. This is my application.
02:00I choose Attach and then I switch back to my application and click on the
02:05button and I hit my breakpoint.
02:07When you are at a breakpoint, you have several choices of how to run your code.
02:12You can click on this Step Into button and then, once you're looking at this
02:16line of code, you can hover over the data to find out what's currently stored in it.
02:20I have a string with nothing in it at the moment. It's got a null value stored in it.
02:23But as soon as I step into the next line of code, you can see that it has the
02:27word 'danger' in there.
02:28You can also read this information, by going to your locals window.
02:32Again, your version of Visual Studio may look slightly different than this.
02:35I am going to click on Locals, and then I'm going to click on the stick pin
02:38over here to Auto Hide it, so I can see it.
02:41Autos and Locals and Watch all do the same thing.
02:44They permit me to watch variables while the application is running.
02:48Autos does things for me automatically.
02:50It watches my cursor and gives me information around my cursor.
02:53Locals gives me all of the variables in scope inside this procedure and Watch is
02:58one of my favorites, because I can add anything I want.
03:00I can come down here and type X and I can see the value of X. I can come
03:04down here and type tb.
03:05It gives me the results of the text box.
03:09I can type in this, which will give me information about the page that I am on.
03:13These little plus symbols here let me drill down and get more information.
03:17I am going to press F5 to continue running.
03:22For the next example, I'd like to show you how you can examine the call stack.
03:25I have a breakpoint here in this Watch Window, and one here in the Array.
03:33I don't think I have set one yet in my call stack. Here it is, right here.
03:37I am going to press F9 and then we click on the button and I am going to step
03:42into my code. This time, I am going to use the keyboard. I am going to press F11.
03:45This function CalculateMonthlyFee is a function in another class.
03:49It's over here in this Financial class.
03:51So, when I press F11 to step through the line of code, you'll see that I move
03:54into another class and now the breakpoint is walking through the code over here.
03:59If you ever wonder where you are, you can go to the Call Stack and it will tell
04:03you that you're currently in this method CalculateMonthlyFee and you came from
04:08this method calButton_Click.
04:10You can switch back and see the previous line of code by double-clicking here.
04:15Now you see I have got a green line that signifies I am not at this line
04:18actually right now but this is where I enter the other method.
04:21And then I can go back to the Call Stack and double-click on this line to
04:24return to where it was.
04:25I'll step through the code by pressing F10 and now you can see, balance has a value of 1590.
04:32I am going to press F5 to continue.
04:38Arrays are interesting.
04:39Arrays hold multiple items.
04:40This is an array of doubles.
04:42I am going to press F10 to load this with information.
04:44When I hover over it, I get a little plus symbol, which I can pop open to see the
04:48values that are in the array.
04:50Here is a list of string.
04:52This is the generic list.
04:54It can hold multiple items, but they all have to be strings.
04:57So, I'll step through by pressing F10 or F11 and then I am going to come down to
05:02the Watch Window, delete a few of these lines by hitting the Delete Key and I am
05:08going to type in days. Shows there's 4 items in there.
05:12There is another favorite window of mine called the Immediate Window. I can go
05:17to the Immediate Window and type in days.Add, Saturday.
05:24Basically, what the Immediate Window does is allow me to run one line of code
05:30while I'm sitting in a breakpoint.
05:31I hit Enter. I come back. I have a five count.
05:35It's in red. That tells me this value has changed and if I open up my days list, I see
05:39that Saturday has been added to the list.
05:41I'll press F5 to continue.
05:46For this last demo, I would like to talk about breakpoint settings.
05:48I am going to put a breakpoint inside this loop, by clicking over here, then
05:55I am going to right-click on the breakpoint and choose Condition or Hit Count or Filter.
06:01Hit Count says I would like to break when the hit count is a multiple of.
06:04So, I can say when the hit count is a multiple of 50. OK and now I get a little
06:11white symbol over there.
06:13And what this allows me to do is click on the button and not have to step over
06:18and over through this loop.
06:19It will stop when the counter gets to 49, in this case, and I could step through
06:23that line of code to see what's happening, and then I can press F5 to continue,
06:26and the next time it hits the breakpoint, it's a multiple of 50.
06:29We are just touching the tip of the iceberg here in the debugging tools.
06:33Remember, if you're new to Visual Studio be sure and spend sometime and get
06:36comfortable with these tools.
Collapse this transcript
4. XAML
Working with XAML Elements and Property Attributes
00:00I can state this with complete confidence.
00:02If you want to become a Silverlight developer, you need to learn how to read
00:06and comprehend XAML.
00:07There is no doubt in my mind that this is true.
00:10A natural place to start your journey to XAML mastery is by studying XAML
00:14elements and attributes.
00:16Before discussing elements, however, let me briefly comment on the rules of XML.
00:20XAML docs are really XML docs. Therefore, your XAML must follow all the
00:24standard rules of XML.
00:25There is really only a handful to learn.
00:27A XAML doc or XML doc is limited to a single root element.
00:31In Silverlight, that is almost always a user control.
00:35The other normal XML rules are as follows.
00:38When you have attribute values, you must delimit those with a pair of matching
00:41items, like a matching pair of double quotes or matching pair of single quotes.
00:46You have to use proper tag closure.
00:48That means you open a tag,
00:50you close it with a matching tag with the Slash on the front or use the special
00:55one tag version of that.
00:58A XAML element maps to a single .NET class.
01:01Here is an example.
01:02That class is called System.Windows.Controls.Button and you will create that in
01:08your XAML by doing angle brackets <Button> like this.
01:13Line 9, I am specifying that I want a button element.
01:16The equivalent C# code would be on line 27 in the code example Button b1 = new Button.
01:23The Content = "Save,"
01:25that is a property assignment.
01:26I am assigning a value to a property in the Button class.
01:29So, the equivalent C# is on line 28, b1.Content = "Save".
01:36The angle bracket <Button> is called an object element. Using that element in
01:41XAML is exactly the same as instantiating an instance of the Button class and
01:45calling the default parameterless constructor.
01:49XAML elements have several types of attributes.
01:52There is one called the Property attribute that corresponds to the object's property.
01:56Here is an example of how you would set that, Margin = "20".
01:59There is a property on a Button class called Margin and I'm assigning a
02:04thickness value of 20 to it.
02:06There is also Event attributes.
02:08In that case, it corresponds to an object's events.
02:12Here is a sample code.
02:13I'm saying on this button, I have a Loaded event and I have some code called
02:17Window_Loaded, which I want you to invoke, if this event ever happens, if
02:21this event ever fires.
02:22There is also something called a directive attribute.
02:26That's a parser instruction that has no programmatic or C# equivalent.
02:30For instance, I would like to assign a localization ID with X:Uid.
02:36Localization is used when I want to have different language-specific strings
02:41displayed to the user.
02:43Then there is also something called the Attached property attribute.
02:47This is a special syntax we are working with attached properties.
02:49I am going to switch to Visual Studio and we will take a look at those four
02:52types of attributes.
02:54This is a typical Silverlight application.
02:56If you're following along on the Exercise Files, you're going to want to open
02:59the XamlElements.sln.
03:01I have an App.xaml and a MainPage.xaml and in my Main Page.xaml, I've created a Rectangle class.
03:11That's the object element and I've assigned the four different kinds of properties.
03:15This is the fill. This is a regular property attribute and I am assigning a
03:18color of Orchid to that.
03:20This is an event property.
03:22I'm saying when the mouse enters this rectangle, call my code called
03:25Rectangle_MouseEnter.
03:28Here, I'm using a directive attribute to assign a name to my rectangle.
03:33Names are important when you're writing code, because then it gives a
03:36programmatic handle for this rectangle.
03:39Lastly, is an attached property.
03:41I have a movie dedicated to attached properties, but what's happening here is an
03:45attached property means that one class exposes the property, in this case the
03:49canvas, but I'm setting it on a different class.
03:52The Rectangle class is setting the value, but it doesn't actually own the data.
03:57The Canvas class is the one that's interested in the data.
03:59It has a special syntax with a period in the middle. Canvas.Left.
04:03Let's see how you would do that in code.
04:05Go to the code behind page.
04:09On line 25, I am going to insatiate a Rectangle class and on line 27, I'm going
04:15to set the Property attribute.
04:16It's just a simple assignment, Width = 40.
04:19Here, I am going to read the value out of the property and assign it to this
04:23double variable called w. This is how I wire up events in my code-behind.
04:28The Rectangle has a MouseLeave event and I used the special C# syntax.
04:33Again, I have another movie later in the series that deals more with events.
04:36That says, please wire up this bit of code to this event.
04:40I don't have any directive attributes, because they're only usable in XAML.
04:45Then finally, this is how you set an attached property.
04:47I go to the Canvas class and call SetLeft and the I pass the rectangle in and
04:52the value that I would like to assign to it.
04:54To read an attached property, I go to the Canvas class and call GetLeft and pass in
04:59the rectangle reference and store that here this double variable called l.
05:03For those of you that would like to see how to do this in a designer, let's move
05:07to Expression Blend.
05:09In Expression Blend, I've opened the same project.
05:11I am going to double-click on the MainPage.xaml and I'm going to draw a new
05:16rectangle by clicking on the toolbar and drawing a rectangle here.
05:21Then let's assign a opacity value of let's say 50%.
05:24So, we will come over here. One of the things you can do in Blend is move your
05:28mouse over this or just slide with your mouse to set the value left or right.
05:31There is 50% and we will go look at the XAML that was written for us.
05:41There is the property setting.
05:43The point here is that you can use a designer tool and it will generate the
05:47XAML on your behalf and there you go. That's the basics for using objects
05:50elements and object properties.
Collapse this transcript
Investigating XAML namespaces
00:00There are hundreds, if not thousands, of types available for use in your
00:03Silverlight application.
00:05If you want to use a .NET type in XAML file, you need understand XAML namespaces.
00:10Using a XAML element in your Silverlight application is the same as
00:13instantiating a .NET class.
00:15To reduce the amount of text contained in the XAML file, Microsoft created the
00:18notion of namespace mapping.
00:20In .NET code, it's common to put your types in segregated namespaces.
00:24Code that works with files is stored in a namespace called, System.IO.
00:28Code that works with databases is stored in a different namespace System.Data.
00:33This prevents name collisions, especially when you're working with libraries of
00:36other developers' code.
00:38On line 23, I am trying to declare a variable of type FileInfo.
00:42This class is written by Microsoft.
00:44We also have a library, the lynda Library that has the FileInfo class.
00:48By putting in the lynda namespace it avoids confusion.
00:52The compiler knows which class I want.
00:54In XML it's also common to put groups of elements into namespaces. You can bind
00:58to these XML namespaces with the XMLNS attribute.
01:03Here is an example. I have a XML file.
01:06In my first element here, I have the trucks element and I say all of the elements
01:11that I want to use should be validated against this namespace.
01:15So, when I use truck, the validating parser in XML will verify that that is
01:19allowed in this namespace.
01:22I've also specified that I would like to work with an additional namespace.
01:25This is not the default namespace.
01:27So, in XML what you do is you type in XMLNS:
01:30and then an alias to the namespace.
01:33Then within your XAML, you use the alias.
01:35So, on line 8, I am saying I'd like use the service element or the mechanic
01:39element on line number 9.
01:40And the validating parser will verify that the mechanic element exists in this namespace.
01:47Silverlight combines these two ideas.
01:49It has XML namespaces and .NET namespaces.
01:53Common UI elements in Silverlight are mapped to a special namespace.
01:57That defines how the XAML parser knows that <button> should really
02:03be an instance of System.Windows.Controls.Button and how it knows that rectangle
02:09should be an instance of System.Windows.Shapes.Rectangle.
02:12So, that's called namespace mapping,
02:14when you take a .NET namespace and a Silverlight namespace and you say map
02:19the two these together.
02:21There are two kinds of namespace mapping.
02:22One is called pre-assigned.
02:24The idea here is that a developer creates an assembly containing some classes,
02:27somebody at Microsoft let's say, and they add some classes to a .NET namespace,
02:32for instance, System.Windows.Shape.Rectange.
02:36The developer then creates a unique identifier called a URI and they add a
02:41special tag in their code called the XmlnsDefinitionAttribute and they say,
02:47look, I've already created this predefined URI.
02:51Another developer comes along and adds his known URI to XAML namespace list.
02:55There is another one called Ad Hoc.
02:57You won't see this in any Microsoft documentation, because I made this one up.
03:01In this case, the library developer creates an assembly containing some classes,
03:04but they don't predefine a new mapping data.
03:06So, then later the consumer that's using that library picks a namespace alias
03:10and notifies the parser where to find the .NET namespace.
03:13It probably helps to see an example.
03:15So, let's look at one.
03:16Here is a default namespace, XMLNS, and this is the unique URI that Microsoft picked.
03:22That's predefined.
03:23So, I put that in here.
03:25I put another predefine namespace on line number 4 and now I can come down here and say Grid.
03:30And that's going to go look up the class called Grid inside that namespace.
03:34It looks up the mapping and finds that Grid is really is System.Whatever.
03:38Here is a polygon. That's also coming from a predefined namespace.
03:41This is an Ad Hoc namespace.
03:43I brought in to scope my assembly.
03:46I said I have an assembly called LyndaLib and I have a namespace inside of
03:50that called LyndaLib.
03:52And I would like to use that in my XML.
03:54So, I give it this alias and then I come down here and say lynda:StarShape and
03:58then a Fill value of BurlyWood, which is a color.
04:01Really quickly, I would like to look at this in Visual Studio.
04:04If you're following along with the exercise files, you are going to want to
04:07open the Namespaces.sln.
04:09This solution has two projects in it: LyndaLib, which contains the StarShape, and
04:14namespaces, which is my Silverlight application.
04:17I am going to go down and double- click on the ShowNamespaceMapping file.
04:22The demo that I'd like to show now is how easy Visual Studio makes it to add a Ad Hoc namespace.
04:27I will come to the top of my XAML window and I am going to the type in
04:30xmlns:demo = Visual Studio prompts me and says where would you like to get your
04:41.NET types for this new alias you created?
04:44I am going to choose LyndaLib, just like we did before, like you see here.
04:49Now I can come down to my body of my XML, choose demo and I can see that inside
04:56there is something called the StarShape and then there's all the properties
04:59available for me to set.
05:01That's it for this movie.
05:03What I hope you got out of this demo was how easy it is to do namespace mapping
05:06and how Silverlight uses the built-in namespace.
Collapse this transcript
Converting XAML properties with TypeConverters
00:00When you assign a value to a property in XAML, there is a high probability that
00:04the assignment is processed by a TypeConverter.
00:07That is because .NET, in most cases, is a strong typed framework.
00:11What do I mean by strong typed?
00:13Let's look at this code.
00:14In C #, on line 22, I am declaring a variable called numbers and I'm giving it a
00:19specific type, a list of double.
00:22This means I want to store lists and the only thing I want to store in the
00:24list is double values.
00:26Then on line 23, I try to assign a value to numbers, but I'm not using a list of
00:30numbers, I am assigning a DateTime = Now.
00:32If I try to do this, the C# compiler will catch this and say you cannot
00:36implicitly convert type System.DateTime to
00:40System.Collections.Generic.List<double>.
00:43Here is another illegal item.
00:45This is a string, 200, and I'm trying to assign a value to a width, which is of type double.
00:51So, .NET tries to protect us.
00:53It's not usually a good idea to store something that doesn't belong in a variable.
00:56I will get a casting error here.
00:57I cannot implicitly convert type string to double.
01:00In that case, I must use either the C # cast operator or one of the parse methods.
01:04In this case, I would use parse.
01:06I will take double.Parse. Here is the string. Go to the double class.
01:09You figure what this is is a number and get me the double value and assign it to this variable.
01:13Or here, take weight, which is a double and I want to store it in a variable of type int.
01:17So, run it through the casting operator and turn it into an int.
01:21So, that's the underlying part of .NET.
01:24XAML is built on top of that.
01:25Let's switch to Visual Studio and see what happens when we try to do this in XAML.
01:29If you are following along with the Exercise Files, you're going to want to
01:32open the XamlTypeConverter.sln and then you're going to want to double-click on MainPage.
01:38I am going to scroll down a little bit. What's wrong with this line of XAML?
01:44It's hard to tell unless you know a little bit about the type.
01:47Background is a brush. This is a string. That looks like a Type Conversion.
01:53I have a string and I am trying to assign a string to the background.
01:56Since that's not possible, when you compile this, I will go to a TypeConverter
02:00and the TypeConverter will look this up, find out what related TypeConverter
02:03there is, run the string purple through the TypeConverter and out the other side
02:07will come the actual brush that we will assign to this.
02:10Here is another example.
02:11This is a string to string.
02:13The text property expects the string. I have a string.
02:16No casting necessary.
02:17Let's continue down and look at the textbox.
02:19This is a brush that is no longer using a color string.
02:22This is a using hexadecimal value.
02:23Again, this will get passed to the Text to Brush Converter. This'll get passed
02:28from the Text to Number Converter. This will get passed from the Text to the
02:33Enumeration Looked up Converter.
02:35So, it will look up the cursor enumeration and find out what value should be set.
02:39If you need your conversion to happen at runtime instead of a compile time, then
02:43you use a special thing called the MarkupExtension.
02:45We have more on MarkupExtension in another movie.
02:48On line 39, I'm saying I have a background. It's a brush.
02:53I don't know until run time.
02:55At runtime, go look up a resource named fireBrush.
02:58So, what will happen is it will go up here to my Resources section, find a
03:04brush called fireBrush, which has a color of Red, and assign that to the
03:10background of this textbox.
03:11Normally, in .NET Microsoft has written all the TypeConverters for you, but
03:15occasionally you're going to write your own types.
03:17In that case, you would need to write your own TypeConverters.
03:19I thought I'd show you a brief example of that.
03:21I have a WeatherControl class over here.
03:23It's a user control, has a TextBlock on it that says the word WeatherControl and
03:32in the code-behind for this, I wrote a property.
03:35I created a public property called WeatherBackground and it's of type brush.
03:41If I go to use this in my XAML, switch back to the MainPage, there is going to
03:45be a conversion happening here.
03:47WeatherBackground expects a brush, but I am pathing in a string.
03:50So, how does it know to turn the string Sunny into the Yellow solid color
03:54brush that they want?
03:55The answer is I need to write a TypeConverter.
03:57So, I come over here. I double- click on WeatherTypeConverter.
04:01This is the TypeConverter that I wrote.
04:03It's the class that derives from TypeConverter.
04:07It's called WeatherTypeConverter. Keep that in mind for later.
04:09There is a method called CanConvert.
04:13What I do in here is I look at the incoming type.
04:15If it's of type string then I say, yes, it's okay.
04:17I know how to convert that.
04:18Return True and collapse this section down.
04:22Here is the actual conversion code.
04:24I wrote a function called ConvertFrom.
04:26I get the incoming value here. That would be the Sunny string or the cloudy string.
04:32I do a little bit of casting here.
04:35Then I have a switch statement, which says, if you get the string Sunny, Return
04:40the SolidColorBrush Yellow.
04:41So, see how I'mg converting?
04:43It comes in as a string.
04:44I turned it into a brush.
04:45It comes in as a string Foggy, I turned it into a gray brush.
04:49I have one more thing to do.
04:50I have to tell .NET to affiliate my custom converter with my WeatherControl.
04:53So, let's go back to WeatherControl.xaml.cs and see how I did that.
04:59On line 16, I said let's affiliate whenever you go to assign a string to
05:04WeatherBackground, call my TypeConverter and now, I can go over here to
05:08MainPage, pass in the string Sunny and it will get assigned to the
05:12WeatherBackground property.
05:13Let's see what this looks like. Press F5 and there's my WeatherControl and it has
05:19a yellow background.
05:20Come back, close the application down, and switch this to - what was one of my
05:25other types? How about we choose Rainy, switch back to MainPage, run this app,
05:33and I should get a Blue WeatherControl. Success!
05:36Now you know a little bit more about how TypeConverters are called whenever you
05:39assign a value to XAML in a non-string property.
Collapse this transcript
Nesting more complex structure with Property Elements
00:00By now, if you have watched some of the other XAML movies in this course, you
00:04know how to use object elements to create elements, and how to set values with
00:08property attributes.
00:09It turns out that property attributes are inadequate in some scenarios.
00:13So, I am going to show you an alternative called Property Elements.
00:16In many scenarios, a property assignment is simple.
00:19For example, assigning a number to a Width property is easy to express in a string,
00:24but with other assignments, it can get more awkward. Why is that?
00:29Well, sometimes the item you want to assign to a property is itself an object
00:33with its own properties that need to be set.
00:35The classic example in Silverlight is when you want to assign a background to an
00:39element, but you don't want a simple color.
00:43Instead, you think that a multicolor gradient or a tiled image would make
00:47a nicer background.
00:48This complexity is difficult to express within a string.
00:51For a short example, look at the XAML on this slide.
00:54As my code notes, this is Bad XAML, do not mimic this.
00:59I have a rectangle.
01:00I would like to assign a linear gradient brush to the Fill property.
01:03But in order to do that I need to create a linear gradient brush and add
01:07many gradient stops.
01:08The more complex the assignments get, the harder this gets to write inside a string.
01:13To simplify expressing these complex scenarios, Silverlight has the
01:17Property Elements syntax.
01:19They can be substituted wherever a property attribute is used.
01:22Some tools, especially Expression Blend, are notorious for using Property Elements
01:27in their generated code.
01:29Just to clarify, an object element results in the instantiation of a .NET type.
01:34A Property Element, in XAML, does not create instance.
01:38Instead, it sets the value of a property.
01:41Property elements are always nested inside an object element, and they are
01:45distinguished from object elements by having a period in the element name.
01:50Here is a short example.
01:51On line 9, I am using a property element, the Text property, to assign the text
01:58value to the TextBlock.
02:00And on line 10, I'm setting the Width property to 150.
02:03Let me show you a more detailed example by switching to Visual Studio.
02:08If you're following along with the Exercise Files, you are going to want to open
02:11the PropertyElements solution.
02:13And then, you are going to want to open the MainPage.xaml file.
02:20Let's look at our XAML. I have an ellipse.
02:23That's an object element.
02:24I am then going to set the Ellipse.Fill property element.
02:27I am going to instantiate a linear gradient brush, and then I am going to set
02:32the GradientStops property.
02:34To do that, I need three instances of the GradientStop object element.
02:38I would like a Blue stop, and a Green stop, and a gradient stop with its own
02:46nested property elements.
02:47In this case, the Offset is equal to 0.7, and the color is equal to LightBlue.
02:53Remember what I said earlier.
02:54You can substitute a property element anywhere that you would use a
02:57property attribute.
02:58I also thought I would show you, in Blend, how it generates these property elements.
03:04I have switched to Blend.
03:06I'm in the same solution I had opened inside Visual Studio.
03:09I'm going to go to this page called AddPropertyInBlend and then I am going
03:14to select my checkbox.
03:15I'm going to go to my Property panel and scroll to the bottom and open the
03:21Transform section, and I am going to use the new Projection property.
03:25I am going to come down here, and I am going to grab the Y value with my mouse,
03:29and slide around and set the value to like say 20 or 30.
03:32And you can see, in my designer, that I'm applying a projection to this checkbox.
03:37I will also do the same thing with the X projection.
03:41Then I will switch to the XAML View, and what you see is that Expression Blend
03:46has created a property element CheckBox.Projection and a plain projection with a
03:52rotation Y, and rotation X values set.
03:56I encourage you to familiarize yourself with both the property syntaxes as you
03:59will encounter them frequently in Silverlight examples.
Collapse this transcript
Assigning runtime data via XAML Markup Extensions
00:00If you need runtime calculation of a value, and you want to assign that value to
00:04a property in XAML, you must use a markup extension.
00:07These handy classes are injected into your XAML and enable powerful features
00:11like binding and resource lookup.
00:13Markup extensions let you break away from the static compile-time assignment of
00:17a string to a string, or the other kind of assignment we have is a string
00:20which goes through a type converter and then is eventually assigned to the destination type.
00:25Markup extensions allow us to do it.
00:26Run time is go out and run some code, the code will determine what to return, and then
00:30that gets assigned to the destination type.
00:33Another way of saying it is they permit runtime calculation or creation of data.
00:37There are four major types of markup extensions built into Silverlight.
00:41Probably the most powerful is Binding.
00:43This means to link two elements together.
00:45I have a data source value and I assign it to a property in another element.
00:51Template Binding gets a value from a control template and assigns it to a property.
00:55We will learn more about control templates in another movie.
00:58Static Resources allow me to look up a resource I have already predefined and
01:01assign it to a property at runtime.
01:04And then lastly, sometimes you need to turn off the value.
01:07In .NET, you do that by assigning what's called a null value to a reference.
01:12If you want to do the same thing in XAML, you need to use the null markup extension.
01:16I think we should look at this in Visual Studio.
01:17If you're following along with the Exercise Files, you should open the
01:21MarkupExtensions solution, and then open up MainPage.xaml.
01:28I'll scroll down here to my TextBox.
01:31The first markup extension is a Binding.
01:33I have a TextBox and I have a second TextBox.
01:37The background color of the first TextBox is set to Goldenrod.
01:40In the second TextBox, I am going to use a markup extension to say at runtime,
01:44go find out the bound value, the source is an element called textHello, and the
01:52property that you want to look at over there is the Background property.
01:56So, these two elements should have the same background color.
01:59In this TextBox, I am going to bind to myself.
02:02It's called a RelativeSource Binding.
02:04I say go to a Binding, read the Text property on the TextBox, which in this case
02:09is Red, and assign that to yourself to the Background property.
02:14This is interesting, because when I run the application, you can see that the first
02:19two text boxes have the same background color.
02:22In the third text box, I can type in values in here like Green or a hexadecimal
02:28number to get my colors.
02:34That's a self binding.
02:36These last two are a StaticResource and a Null MarkupExtension.
02:40Null means to reset to the default and this is going to look up a StaticResource.
02:44Let's look at the XAML.
02:49In my StaticResource MarkupExtension, I am setting the background from a
02:53resource that I've defined elsewhere in my XAML.
02:55I have defined something called seaBrush, and if I scroll up here to the top,
03:00you can see that I have this LinearGradientBrush with a key assigned seaBrush.
03:04And runtime is finding this brush and assigning it to the background of that TextBox.
03:08And lastly, here is the Null MarkupExtension:
03:11Background = '{x:Null}'.
03:14The x means it is coming out of the special XAML namespace.
03:19There's only a few markup extensions in Silverlight, certainly not as many as
03:22you will find in its cousin, WPF.
03:24Even more disappointing for me is that Silverlight markup extensions are not extendable.
03:28So, that means you cannot create your own markup extensions.
03:31Still, I would say that the few that we do have are among the most
03:34powerful available.
Collapse this transcript
Digging into the Dependency Property System
00:00When you investigate a visual element a like the button or a list box, you see
00:03a lot of properties.
00:05You might be surprised to learn that most of these properties in Silverlight
00:08Elements rely on a special UI system known as the Dependency Property System.
00:12This system was first conceived and created by the WPF team.
00:16This new system enhances the underlying .NET framework and permits elements to
00:19plug into useful services like animation or styles.
00:23It is important for a modern UI to be responsive to animation input to allow
00:27binding to data whether the data is stored in templates or in data sources.
00:31It's also important for it to be easy on critical resources like memory.
00:35And that's the purpose of the dependency system.
00:38The core piece of the dependency system is something called the
00:40Dependency Property.
00:41As I said earlier, most of the properties on an element are
00:44dependency properties.
00:45The button's Font Size property is one example.
00:49When you're looking at a Silverlight element, it's hard to tell a dependency property
00:52from a .NET property, just keep this in mind.
00:54You can set your Font Size directly in code, or you can set the Font Size by one
00:59of the Silverlight services.
01:01Here is an example.
01:02I have an animation running.
01:04I have the animation changing the font size.
01:07If I go to my button and say what's your current Font Size?
01:09The Silverlight services are going to say, wait a minute.
01:12I have to figure this out.
01:14It's going to go calculate the value.
01:16It's going to see if there is an animation running, and other services running.
01:19It's going to determine which one has precedence and it's going to give me back
01:22the result based on what it looks up.
01:24Let's go see how you set dependency properties in Visual Studio.
01:27If you are following along with the Exercise Files, you are going to want to
01:30open the DiggingIntoDependencyProperties solutions.
01:33And the first file you are going to look at is this one called SettingDPInCode.
01:38Double-click on the C# portion of that, the code behind and then scroll down to
01:42this section here, line 24.
01:47This is the Font Size property on the button.
01:49Here, I am assigning a value of 40.
01:51It looks just like a normal .NET assignment.
01:54That's because the author of the button wrote a .NET wrapper so I can access
01:59it directly like this.
02:01To read the value, I can go to the instance of the button and ask for the
02:04FontSize and then store it as a variable.
02:07If I would rather bypass the .NET wrappers and go directly to the dependency
02:10system, I use the SetValue or the GetValue methods on the object.
02:17Here I am setting the value.
02:18So, I call SetValue and I pass in the name of the dependency property, FontSize
02:23property, and the new value that I want.
02:26On line 31, I am calling GetValue, retrieving the FontSize property, and then
02:32casting it to a double, because the dependency system stores it as an object.
02:36As I mentioned earlier, one of the most notable features of dependency
02:39properties are that many different Silverlight services might be attempting
02:42to set the DP value.
02:44Silverlight takes a look, prioritizes each service and the highest
02:47priority service wins.
02:48Here is the priority resolution.
02:52Down at the bottom is the lowest priority.
02:55When you create a dependency property, you can pick a default value.
02:58The next highest priority is Property Value Inheritance.
03:01That means that if you have a value set on a parent, it can trickle down to its children.
03:05Next in the list comes Styles.
03:07Styles take precedence over Property Value Inheritance.
03:10We will learn more about styles in another movie.
03:13Then there's Local Value.
03:14What do I mean by Local Value?
03:16That means I am setting it directly in my XAML, or I'm applying it in code.
03:20And then lastly, the highest priority service is Animation.
03:24So, if I have written some code that sets the value to 20 for the FontSize
03:28and an animation is running, and it's currently at 30, what do you think the FontSize would be?
03:33The answer is 30, because it's a higher priority.
03:35I've got another demo that shows the Priority Resolution.
03:38So, I will switch back to Visual Studio, and close this window.
03:43You are going to want to open this one called OrderOfPrecedence.
03:46We are going to be working with TextBoxes, and see what the order of precedence is.
03:55This first TextBox, I have not assigned any value to the Background property.
04:00Therefore, it will use the default.
04:02In the second TextBox, I have done a local value assignment because I have set it in my XAML.
04:08Let's run the application by pressing F5, and see what these look like.
04:12Here is my browser.
04:15The first TextBox is using the default background, which is white.
04:18The second TextBox is blue because I assigned it in my XAML.
04:22In the third example, I am going to click on this button and I have some code
04:26that's going to assign a background brush.
04:29You see that that takes precedent over the white default value.
04:32And I can then clear that by clicking on this button, which sets it back to the default.
04:38There is a difference between setting it back to the default, and assigning
04:41a white brush to it.
04:42How do I solve this problem?
04:44Let's go back to Visual Studio and look at my C# code, double-click on this
04:49OrderOfPrecedence.xaml.cs file.
04:56Here's how I assign the brush.
04:58I create a brand new SolidColorBrush and assign it to the background. That takes
05:01precedence over the default.
05:03And here I'm clearing the value.
05:05I'm not assigning a white brush.
05:07I am calling codeTextBox.ClearValue and then I am saying here is the value in
05:11his TextBox I want you to clear, the TextBox's Background property.
05:15Now it's back to the default of white.
05:18What's next in our order?
05:19Let's switch back to the browser and look.
05:21Here I have got a pink background.
05:23It says it's set via style.
05:25So, let's take a look.
05:27Go back to Visual Studio, back to my XAML.
05:32On line 88, I have a TextBox.
05:35It has a style applied to it.
05:36We will learn more about styles in another movie.
05:39If I scroll to the top, you'll see that I have a style up here called myStyle, and it
05:46has a Property Setter that says the Background property should be Pink.
05:50So, that takes precedence over the default value.
05:55In this TextBox, however, look what's happened.
05:57We have got conflicting settings.
05:59I have a Style set, the same style as before, but I have also said
06:03Background equals LightBlue.
06:04And if you remember the order of precedence, Local takes precedence.
06:10So, even though it had the style assigned, the local blue color is what I see.
06:14In the next example, I show Property Inheritance.
06:18I have a text block and the size of the font in this text block is coming from
06:23somewhere, we can't see where yet.
06:25In the second text block, we can see we have assigned it directly.
06:29Where is the size of this one coming from?
06:32Through property inheritance, it's coming from the UserControl, FontSize = 14.
06:36So, this percolates down to all of the children elements who don't override it
06:39by assigning their own local values.
06:41And lastly, I said that animation takes precedence.
06:45I will switch back to my browser and I click on this Start Animation button.
06:49This TextBox Background is currently red, because I have a locally assigned
06:55value of Background Red.
06:56But when I click on the button, it's going to start an animation, which
06:59will modify that value.
07:00And you can see it changing from blue to white, and then over here, I am reading
07:10the current value on a timer.
07:12So, you can see that this is the currently assigned value.
07:15So, even though it's set in Red in my XAML, it's not red anymore because the
07:18animation is changing the value.
07:20Now, there is a lot going on in this demo.
07:23I really encourage you to spend some time going through the examples yourself,
07:26so you can get more comfortable with the prioritization levels available within
07:29Silverlight dependency properties.
Collapse this transcript
Creating a custom Dependency Property
00:00You are far more likely to use a dependency property than find the need to create one.
00:04Nevertheless, there will be times when making a dependency property is
00:07appropriate, like when you create a custom user control and need to configure
00:11some of its properties as dependency properties.
00:13If you were to implement these properties only as .NET properties, then sooner
00:17or later, you will discover that you have created a UI Element that cannot be
00:20animated or styled, and that would be a tragedy. So, let's review.
00:24The Dependency Property System exists to provide services to your
00:27Silverlight elements,
00:28for instance, the services like animation or control templates or styles.
00:33Your elements can also partake of these services, but in order to do that, you
00:38have to register with the dependency property system.
00:41And that means you have to write what I call ugly registration code.
00:45You will see what I mean about this ugliness in a few seconds.
00:50Microsoft has a naming convention, which you can follow if you like.
00:53Microsoft always applies the property suffix to all dependency properties.
00:58So, the FontSize property becomes the FontSizeProperty.
01:02Now when I say it, they sound exactly the same, but if you look at the slide,
01:05you can see they are different.
01:06This is the FontSize property and that becomes something called FontSizeProperty
01:10with no space between the Size and Property.
01:14Here is one line of the registration code.
01:16I'm going and saying, I want to register a Points property.
01:20This is the name I just showed you and I want to register it with the dependency
01:25system and here are some details about the registration.
01:27I think it's better to look at this in Visual Studio.
01:30So, here I am in Visual Studio, once again.
01:33If you're following along with the exercise files, you're going to want to open
01:36the CreatingCustomDependencyProperty Solution and then you're going to want to
01:40open the MainPage.xaml.
01:43In here, I'm using a StarShape that I created.
01:46I created my own custom user control called StarShape.
01:49I've brought it into scope by bringing the namespace in to scope up here in line 6.
01:54It has a property that I wrote called the Points property and this allows me to
01:58change the number of points on the star.
02:00Let's see what this looks like.
02:01We'll press F5 to run and you see that I have two 5-pointed yellow stars.
02:05Now something is not quite right yet, because I said I wanted six points here.
02:11I'm only getting five.
02:12We'll go look at the StarShape itself.
02:15I'm not going to look at the XML.
02:16That's not very important.
02:18The important part is what's in the C# file.
02:22Here's our registration.
02:23The first thing I'm going to do to register my dependency property is to create
02:26a variable in my code called PointsProperty and the datatype of this variable
02:32is DependencyProperty.
02:34There are some other keywords. Public expresses its scope.
02:37Static means this is available without having to instantiate the StarShape and
02:41readonly means that you can only set the value in the static constructor.
02:45And I have one of those, right here, static StarShape.
02:48That's my constructor.
02:49Inside my constructor, I'm going to register by going to the Dependency Property
02:53class and calling its static method called Register.
02:56Then I provide several pieces of data: the short name of my property, the
03:01datatype of my property and the type that it's affiliated with, StarShape.
03:06We'll ignore this parameter for the moment.
03:11Optionally, you can create your own .NET wrapper.
03:14This is a C# property, called Points.
03:18And when you go to get the value for the point, it really just calls out to the
03:22Dependency System and says, hey!
03:23What's the current value of the Points property?
03:26When you go to set the value, it comes in and says, okay Dependency Property,
03:30while it's doing a direct assignment of the property.
03:33This is for convenience so that VB programmers and C# programmers can set it
03:37directly in code without having to do it through XAML.
03:39I've got my Dependency Property changed.
03:41Now I can create an animation and change the value of the points from 5 to 6 on the animation.
03:45I still have one problem left though is that when I changed the number,
03:48it doesn't actually changed the number of points.
03:51For me to make that work, I have to register what's called a callback.
03:54We'll go up here and I'll uncomment these two lines of code.
03:58I will comment this one out.
04:02Let me explain what the code is doing.
04:05I'm doing the same registration as before, except in the last argument, I'm
04:10creating a PropertyMetadata.
04:12This is extra data that I'm going to give to the Dependency System by saying the
04:15default value -- if I don't provide any value, it's 5. I want 5 points.
04:20I have a callback. If the value ever changes, Mr.
04:23Dependency System, you need to go out and find this method down here, the
04:27propertychanged and run my code.
04:31All this does is look to see if I pass in a number 6, and if so, then I show my
04:356-pointed star. Otherwise, I show my 5-pointed star.
04:38Now let's run this a second time, and you see I have a 6-point star in the
04:42bottom and a 5-point star on the top.
04:44Dependency properties are hugely important for you to learn, especially if you
04:48are going to create custom user controls.
Collapse this transcript
Understanding Attached Properties
00:00Often, in UI applications there is a conversation taking place between your UI elements.
00:04Imagine that you have a tree view and a few other controls inside a form.
00:08The tree view indicates to the form where it should be placed on the screen.
00:11This might be done with absolute coordinates are depending on the
00:14form implementation,
00:15the tree view might indicate the location with a dock property or by
00:18specifying an anchor value.
00:20The problem with this model, which is actually an older model from the Microsoft
00:23Windows Forms framework, is that you end up with tight coupling between the form
00:27and any controls that are hosted by the form.
00:30If an innovative new layout ID appears, both the form and every hosted control
00:34must be changed to make this work. Granted you can create a control base class
00:38to mitigate some of the coupling, but there's a better way.
00:40That better way is called the attached property.
00:43The attached property is a dependency property, which means that's it's managed
00:46by the dependency property systems.
00:48And it helps provide a decoupled extensible model.
00:52You can think of attached properties as global properties, global in the sense
00:56that they are available as a property on any XAML type.
00:59They are defined within a type or owned by a type, let's say, the Canvas class.
01:04The Canvas then has to register that type.
01:06It does that with the DependencyProperty.RegisterAttached method.
01:11Once that is done, then the property is no longer isolated to the Canvas class.
01:15Any other element can set the new attached property.
01:19It uses the special dotted syntax, which is the TypeName canvas, dot,
01:23and then the AttachedProperty.
01:25I would say that the most common place that you see attached properties are in
01:28the Layout panels, like the canvas or the grid or the dock panel.
01:33They use attached properties to let their children elements know where they want
01:36to be positioned on the screen.
01:38Let's take a look at the key points here.
01:40An attached property value is stored in the dependency property system.
01:43That value that is stored is associated with an element that assigns the value,
01:47let's say, a button that's inside a canvas.
01:50The owning class, again the canvas, will ask the dependency property for the value.
01:55And then it will make a decision about what to do with that value.
01:57I think we should go look at a couple of samples.
02:00I'm in Visual Studio. If you're following along with the exercise files, you're
02:03going to want to open the UnderstandingAttachedProperties Solution, and then
02:07you're going to want to double- click on main page to open it.
02:12We are going to start by looking at this radio button element.
02:15The radio button is setting a toolTipService.toolTip = Sample.
02:20That is an attached property.
02:22Let's see what happens if I come in here and I put another radio button in and
02:27then I get the dropdown list.
02:28All of these items that you see here like Background, BorderThickness,
02:33Cachemode, these are dependency properties, not attached properties.
02:37And they have been registered and they're available to use in this button.
02:40So, I can set BorderThickness equal to whatever.
02:43If I pick a non-existing property, like say ABC, I'm going to get a parser
02:50error, because the radio button does not have a dependency property called ABC.
02:56Well, the radio button also does not have a toolTipService.toolTip property, but
03:01because the toolTipService class registered the toolTip property at an attached
03:06property, I'm allowed to use it here.
03:10Here is how it's used elsewhere. I have a grid.
03:13This list box is using the attached Grid.Column property to say that it wants to
03:17go in the fourth column.
03:18This TextBlock is saying, if I'm inside a canvas, I want the canvas to place me
03:2350 pixels from the top.
03:25Is the TextBox inside a canvas? No.
03:28But it stored that value in the dependency system and it's wasted memory, but
03:32it's not going to cause a parser error.
03:34Here are more examples down here.
03:37I am in the CheckBox class.
03:40I'm setting its Canvas.Left.
03:42I'm sitting its AutomationProperties. I'm affiliating this AcceleratorKey with the CheckBox.
03:47Here, I'm telling you whether the ScrollViewer. VerticalScrollBarVisibility should be hidden.
03:52It doesn't make any sense for the CheckBox to have the ScrollViewer, but for
03:55other elements, it might make sense.
03:57That's all I have for attached properties.
04:00As I've said, they are commonly used with layout panels and you can find more
04:03information about their usage in other movies in this series.
Collapse this transcript
5. Layout Containers
Understanding layout
00:00A good UI rendering engine must facilitate placing elements and positions on the screen.
00:05This is called layout and it means what the name implies, controlling the
00:09location and sizes of a section of the screen.
00:12A great layout system will prove resilient to changes in browser size and font
00:17settings and it will also remember spatial positioning between related elements.
00:22For example, your toolbar buttons should remain together in a horizontal row in
00:25spite of changes to the top location of the toolbar.
00:28Silverlight user controls can only hold one child element.
00:32Since windows and content elements only hold a single child, there must be other
00:35elements that can hold many children.
00:38In Silverlight, there are panels and there are items controls.
00:41panels are responsible for layout and have no UI of their own.
00:44Each panel has a layout specialty.
00:47Items controls are more sophisticated and also can contain children.
00:51These are controls however, not panels, and can have sophisticated UI templates
00:56applied to dramatically change their appearance.
00:59The panel base class is provided as a base class for all layout containers.
01:03And as you remember, I said just a few minutes ago, each user control can
01:07only have one child.
01:09Therefore, if you want to have a usable UI, you're going to have to have a
01:12container as the first child of the user control.
01:15Silverlight ships with a handful of built-in panels.
01:18There is the Stackpanel, which is good at stacking elements, the Grid, which
01:22excels at dividing the screen into rows and columns and the Canvas, which
01:26arranges its children by absolute coordinates.
01:28The Silverlight toolkit also provides two more essential panels.
01:32That would be the WrapPanel and the DockPanel.
01:35Details regarding the panels on this slide are available in other movies in this title.
01:39Silverlight contains a panel base class too, which makes it possible to create
01:42your own custom panels.
01:43Since the rest of this chapter elaborates on the built-in panels, I thought in
01:47this movie I would show you an example of a custom panel.
01:49To do that, I need to switch to Visual Studio.
01:52If you're following along with the exercise files, you're going to want to open
01:55the CustomPanel Solution.
01:57There are two key parts in this example.
01:59There is my Custompanel, which is called CircularPanel. I'll open that one up
02:03briefly and look inside of it.
02:04I have a class called CircularPanel, which derives from the abstract base class
02:08panel and there are a couple methods.
02:11I won't go into the details, but there is one called MeasureOverride and one
02:14called ArrangeOverride.
02:16I have written some custom logic in there that will arrange items in a circle
02:19position on the screen.
02:21Then I go to my main page and I add an instance of my CircularPanel to my UI.
02:28I brought this into scope here with the local prefix and I have a custom
02:32namespace defined up here.
02:34I now have my custom CircularPanel and I have added several buttons and I have
02:39also created a little Button_Click code here.
02:41When I click on this button, I will add another button to the CircularPanel.
02:44Let's see what this looks like.
02:45There are my five buttons arranged around the circular position.
02:51When I click Add more, you can see we're adding a lot more buttons and they're
02:57being positioned around the edge of the circle.
02:59So, what you see here is that Silverlight provides an extremely flexible layout
03:03system that can be configured in endless permutations, one of which is sure to
03:07be the layout you are looking for.
Collapse this transcript
Painting background and borders
00:00If you want to control the layout of a visual element, you must place those
00:03elements in a Silverlight panel.
00:05Each panel excels at arranging children in a specialized fashion.
00:08All panels have one interesting property that I want to explore for the next few minutes.
00:12That property would be the Background Property.
00:15The Background Property expects to have a brush assigned.
00:18In Silverlight, there are many brushes to choose from. There is the Image brush,
00:22the Video brush, the Linear and Gradient brush, but for this movie, I will
00:26concentrate on the simplest brush of all, the SolidColorBrush.
00:30If you are following along with the Exercise Files, you are going to want to
00:32open the PaintingBackground project and then you'll open the
00:36DefaultBrush.xaml file.
00:38I'll do that by double-clicking.
00:40My intention here is to show you what happens if you do not assign any color to a panel.
00:45For this example, I have a grid, named LayoutRoot, and you'll note there are no
00:51color values assigned here.
00:52There's no brush assigned to the Background property.
00:54That means that Silverlight uses what's called the default brush.
00:57And to verify what the default brush is I've written a little bit of code.
01:01So, we'll look at the code by going over here to Solution Explorer and
01:03double-clicking on the CS file.
01:06There is only a single line of code. I am going to turn off the break point for
01:09a minute to make it easier to read.
01:11I'm grabbing the LayoutRoot, which is my grid and I am asking for the Background brush.
01:16And then I am assigning that to a variable type brush.
01:19If I put my break point in here by clicking in this gray bar, I can stop here
01:23and examine that in Visual Studio.
01:25Visual Studio allows me to drill down and look at values at runtime.
01:28In order to do that, I need to make sure they are run in the debug build.
01:31So, I'll choose Start Debugging from my menus and wait a few seconds and you'll
01:36see that the application runs and I hit my break point.
01:39The yellow line signifies I am sitting at my breakpoint.
01:41And now I can hover over these items.
01:44If I hover over Background, you'll see that I get a tooltip that pops up that
01:47says the Background has a null brush.
01:49Usually that means you end up with a White Background color.
01:52I need to stop this application, so I can show you the next demo.
01:55So, I am going to go up here and click on the Stop Debugging button.
01:58Now for my next demo, I would like load this file, AssignBrushInXAML.
02:04I also need to ensure that Visual Studio uses that as my start page.
02:07So, I need to go to my App.xaml and the C# page there and modify Startup.
02:13So, I no longer need the default brush as my RootVisual.
02:16Instead, I need the AssignBrushInXAML, and I'll switch back to this
02:22AssignBrushInXAML file and the key things I want you to note here are I am
02:26assigning two different grids a Background brush.
02:29This first grid I am assigning a orange solid color brush and in the second
02:34grid, I am assigning my own custom color, using a hexadecimal value.
02:39Remember from an earlier movie that this means a hexadecimal number and then
02:43these are the four values.
02:44This is the alpha channel. This is the red channel. This is the green channel,
02:49and this is the blue channel.
02:52If I run this, you'll see some nice looking colors.
02:54The outside grid is orange. The inside grid is some sort of custom purple value.
03:01When I shut down the browser, it will stop my debugging session, which will
03:05return me back to Visual Studio.
03:07As a programmer, there might be times when you need to programmatically change
03:10the color of an element and you can do that in your code.
03:13So, for the next example, let me close a few windows so it's easier to see
03:17what's going on here, and we'll move to the AssignBrushInCode.xaml file.
03:22If you take a look at my XAML here, you'll see that it's a number of grids nested.
03:29I've got four grids named layoutRoot, secondGrid, thirdGrid, and fourthGrid.
03:34Each of these grids has a TextBlock inside of it that's telling you the
03:37purpose of the demo.
03:39Only the first grid has a Background value applied.
03:41So, the rest of them, I am going to write code to accomplish this.
03:44Naturally, that means I need to switch to the code window.
03:47So, I'll come over here and double- click on the AssignBrushInCode C# file.
03:51And in the AssignBrushInCode constructor, I'm going to construct a brand-new
03:55solid color brush and I am going to go to the Silverlight libraries. I am going
03:59to ask for the yellow brush.
04:01I don't know what the real values are for the yellow brush, but Silverlight does.
04:04This is an enumerated value called yellow, and I am taking the yellow brush and
04:09I am assigning it to the Background at this layout root.
04:11In the next line of code, I want to use some of the operating system colors.
04:17So, there is a class called SystemColors and it has the ControlColor, which will
04:21go look up the user settings on this OS,
04:24determine what they have got set for the ControlColor, and assign that to the
04:27Background of this grid.
04:28You saw how I was able to do a custom color in XAML.
04:31I can also do custom colors in my code.
04:34In order to do that, I need to use the color class and it has a method called FromArgb.
04:39A stands for alpha, r stands for red, g stands for green and so on.
04:43And what I am doing here is assigning a byte value of 255 for the alpha channel
04:49and 122 for the red channel and so on.
04:51In the last example, I want to show you how you can set a transparent color, so
04:55in this example, I am setting 60 as my transparency value.
04:59255 is fully opaque, a value of 0 is fully transparent.
05:04Would you like to see what this looks like?
05:05Well, of course we need to go back to my App.xaml.cs file and make sure that we
05:11have this RootVisuals as my Startup.
05:15Run the application.
05:16That time I pressed F5 to run the application.
05:19There is my Yellow brush, there is my System Color Brush and there is my two
05:22Custom Brushes including the one with the semi-transparent color.
05:26For my last demo, I would like to show you borders.
05:29By default, none of the panels have a border around them.
05:32So, there's a special element in Silverlight I call the border element.
05:35For this demo, let me close a few windows first.
05:39I am going to use the WrappingInBorder.xaml file.
05:43If you take a look at my XAML here, you'll see that I have a grid and outside of
05:49my grid I've wrapped it in a border.
05:52In the border element, if you want to change the color of the border, use
05:55something called the BorderBrush.
05:57And again this can take solid color, video brush, whatever type of brush you want.
06:00In this case, it's using a solid color Orange brush.
06:03I've created a second border, down here on line 11 and in here, I have set a
06:07margin of 50 so that I give myself a little breathing room around the outside
06:10edge and then, I set the BorderBrush to DarkOrange and I specified the
06:15Thickness as a border here.
06:17In Silverlight, you can set the thickness in a number of ways.
06:19This is setting a value of 2 pixels for the left and right border and a value of
06:2510 pixels for the top and bottom border.
06:28And then, lastly, I want to round off the corners and make it look pretty.
06:32So, I set a CornerRadius of 10.
06:33I am wrapping that again, around the TextBlock element.
06:37I need to switch my Startup one last time.
06:42
06:45I press F5 to run the application, maximize the browser so it's easy to see.
06:51And there's my Orange color brush.
06:53Notice I have a 10 pixel top and bottom, nice rounded corners, and there is my
06:58TextBlock inside the border.
06:59Painting bright areas of your UI is accomplished by setting the Background
07:03brush on your panel.
07:04Drawing a line around any of the four sides of an element, however, is the domain of the
07:08border class.
Collapse this transcript
Getting colorful with gradient brushes
00:00A common UI pattern in recent years is filling portions of your UI with blended colors.
00:04In Silverlight, this is accomplished with the two GradientBrushes.
00:08With gradients, you specify two or more colors and Silverlight determines the
00:12intermediate color ranges.
00:13While you can set gradient ranges in XAML or in Visual Studio, you'll be much
00:18more efficient if you use Expression Blend 3 as your editor of choice.
00:21I have opened up Expression Blend for this demo.
00:23If you're following along with the Exercise Files, you are going to want to open
00:26GradientBrushes project.
00:28In this GradientBrushes project, I have a single file called MainPage.xaml and
00:33on that file I have two grids. I have the LayoutRoot and I have a second Grid with no name.
00:40The second Grid is the one that I am going to be manipulating with the color brushes.
00:44In Expression Blend, to change the brushes you go to the Properties panel and
00:48you open the Brush section in your tool section.
00:51At the top of the Brush section are five buttons.
00:54The first button is for assigning a Null brush.
00:57If I click on this button, I now have a Null brush assigned.
01:00If I click on the second button, I get a Solid Color brush.
01:03This is where you can start having fun.
01:05I don't like this orange color, so I can come over here and drag this up and
01:08down to pick a different color tint and then I can grab this little circle here
01:12or just move around with my mouse to dial in the new color.
01:16Less saturated colors on this side, more saturated colors on right side.
01:20There is a number of other things you can change over here by grabbing
01:22your mouse and sliding up and down, changing the red color or changing the Alpha value here.
01:28And every time you do this, Blend creates the hexadecimal number in this area.
01:33So, if you need the hexadecimal number later, you can just copy this out and use
01:37it somewhere else in your code.
01:38Even more fun is playing around with the GradientBrushes.
01:41Clicking on this button brings up the Gradient Editor.
01:44Now the Gradient Editor is one of my favorites.
01:47It starts off with two gradient values, a black and a white, and I can drag this
01:52White button on the Gradient bar to a new position or I can change the color by
01:56dialing in a new value.
01:57I have selected the white button down here.
01:59I am going to dial in a yellow color and then for the black value, I am going to
02:04dial in a nice blue color. And you can see,
02:08it interpolates the colors between the two of them.
02:10Now, if you want to add more gradient stops you would just click on the Gradient
02:14Bar, move it to a new position. See how that changes as I move over there? This
02:18Blue changes over to the other side.
02:19I will make that kind of a purple color and if you don't want the gradient
02:26stop anymore, you take it with your mouse and you pull down off the gradient
02:30bar and it removes it.
02:32Another really fun tool of GradientBrushes is over here on the toolbar.
02:36If you move over to the toolbar section of Expression Blend, there is this
02:38button called Gradient tool, or you can just press the G key on your keyboard.
02:44When you select the Gradient tool, you get an arrow in the main artboard region
02:48and this arrow mimics the Gradient Bar and even more important, you can grab
02:53this arrow and slide it around to change the angle of the gradient.
02:55So, I am going to move this arrow down here to the bottom corner.
03:00This circle here represents the Gradient Stop over here, so if I move the
03:04Gradient Stop over here or on the arrow, it adjusts it the same way.
03:08So, this is a way you can dial in your gradient by looking at it instead
03:11of trying to guess.
03:13Finally, all of this is being written out in XAML, so if you switch over to your
03:16XAML view, by clicking on this button in Expression Blend, you can see that
03:20there's a grid with a background assigned using the property element syntax.
03:25And then there is my LinearGradientBrush and then there are my two
03:27GradientStop values.
03:29Using Expression Blend to create and modify GradientBrushes is fast and
03:32intuitive, and I'd say it's much easier than hand-editing the XAML.
Collapse this transcript
Controlling your layout with StackPanel
00:00The Stack panel is an example of a Silverlight panel that does one simple task.
00:04It will stack its children in a vertical or a horizontal stack while keeping
00:08each child as close to its neighbor as possible.
00:11Children elements can negotiate extra space around their designated area, but I
00:15will save the details regarding how they do that for another movie.
00:18If you're following along with the Exercise Files, you're going to want to open
00:22the UsingStackpanel project.
00:25For this example we're going to be working in MainPage.
00:27I thought I would show you an example of where Stack panel is used in the real
00:30world. In Visual Studio, you can see an example over here in the toolbox.
00:34I will move over to the toolbox and in Visual Studio, you can pin this toolbox to
00:39the edge of the screen by clicking on the Auto-Hide button.
00:43This is an example of a Stack panel.
00:45Each one of these items is stacked immediately below its neighbor.
00:48All right. Let's see how we do that in Silverlight.
00:53You create a Stack panel and you put elements in it.
00:55That's as simple as it is.
00:57Here is a TextBlock and a couple of buttons.
00:59When I press F5 to run this in the browser, you'll see that it puts the
01:05two TextBlocks at the top and it puts the three buttons immediately below the TextBlocks.
01:10Notice how close they are.
01:11That's because none of the children elements have negotiated any extra space.
01:15The only other interesting thing about the Stack panel is that you can
01:19change the orientation.
01:21So, I will go into my Stackpanel on line 12 and add one property, Orientation=
01:27Horizontal. Press F5 to see this in action and now you see there is a TextBlock,
01:35and then a second TextBlock, and then the three buttons stacked in a horizontal
01:39stack as opposed to a vertical stack.
01:41Stack panel has a single responsibility - to stack elements.
01:44As such, it is handy for many common layout arrangements like grouping the OK and
01:48Cancel button together at the bottom of your window.
Collapse this transcript
Exploring additional layout controls in the Silverlight toolkit
00:00The Silverlight API ships with three Layout panels. They are the Stack panel,
00:04the Canvas and the Grid, which are first rate for many common layout scenarios.
00:09WPF, which is Silverlight's older Cousin, has other panels, however.
00:14Not to be left out, the Silverlight team has released these additional panels.
00:17They are the Wrap panel and the Dock panel.
00:21Due to time constraints, these panels are contained in the Silverlight toolkit, a
00:24download available from codeplex.com.
00:27These panels are not some amateur hack job but are written and managed by a Microsoft team.
00:32The primary difference is that elements in the toolkit have their own shipping
00:35schedule and will be folded into the mainstream at a future point.
00:39If you are following along with the Exercise Files, you are going to want to
00:42open the toolkitpanels Solution.
00:45Before I show you any code in this demo, I want to show you an example of a Dock
00:48panel inside Visual Studio.
00:50I am going to go over to my toolbox, I am going to dock it to the edge by
00:53clicking on this Auto-Hide button.
00:55In Visual Studio there is a Dock panel that is docking the toolbars at the top,
01:00the toolbox along the left edge, some additional windows along the bottom edge,
01:05and the Solution Explorer and Properties window along the right edge.
01:09Again, this is an example of a Dock panel, and we are going to accomplish the
01:12same thing in Silverlight.
01:13To do that, I am going to open the UseDockpanel.xaml file, and we are going to
01:19talk about what you need to have in order for this to work.
01:22In order for you to use the Silverlight toolkit, you have to download it
01:25from the codeplex.com/Silverlight website and you need to add a reference to
01:32your References folder.
01:33I don't believe I have a reference, Let's go take a look.
01:35No, there's nothing in here that says toolkit on it.
01:40I could right-click on the Reference folder and choose to Add Reference, but I am
01:45going to show you a trick in Visual Studio which will allow me to have it
01:47Auto-generate my reference for me.
01:50The trick is to go to your toolbox and find the Dockpanel.
01:55Now the Dockpanel is inside the Silverlight toolkit.
01:58It won't be on your toolbox unless you've installed the Silverlight toolkit.
02:02Because I've installed that earlier, it's available to me. It's right here.
02:05I am going to grab this. I am going to drag it into my XAML file and I want you
02:10to watch line 1 of the code when I drop this.
02:14So, a couple of things happened. One is it added a line down here on line six,
02:19where it's saying, here is my Dock panel.
02:21Secondly, it knew that I needed a new namespace reference.
02:24So, at the top of my screen, Visual Studio added this line, which is the XML
02:28namespace that contains the controls I need, in this case it's
02:30System.Windows.Controls.toolkit.
02:33We've covered the details on this in another movie in the series.
02:36Now to clean up I need to delete this line. This won't work right now because
02:40the user control can only have a single child and doing my demo I added a second child.
02:44So, I am going to go ahead and delete this line, line 7 and then all my blue
02:48squiggles at the bottom of the window should disappear.
02:50All right, we are ready to go.
02:52I have created a grid.
02:54Inside the grid, I have created a Dock panel and I have added a few buttons in here.
02:58This first button I have chose to dock it to the left edge of the screen.
03:02Now I am using an AttachProperty to do this.
03:04There is another movie detailing more about AttachProperties and what I am doing
03:08here is I am saying that this button should be docked to the left edge.
03:12And then the second button should be docked to the left edge and then the third
03:15button should be docked to the bottom and so on.
03:17Let's press F5 to see what it looks like.
03:19There is the first button docked to the left.
03:23There is the second button docked to the left. The third button was docked to the bottom.
03:27Notice that the third button doesn't go all the way over to the edge, because
03:31these two items were docked first.
03:33So, they get priority.
03:34Now watch what happens if I come into my XAML.
03:37I am going to close the browser. I am going to take this button called Third,
03:40I am going to cut that out and I am going to move it up here to the top of the border.
03:45This is called the Z index and I have now said that you should dock to the
03:49bottom first and then dock this button to the left.
03:52Let's see what this looks like by pressing F5.
03:54Do you notice any difference?
03:57Yes, the third button now goes all the way to the edge of the panel.
04:01Another thing you can do is you can change what happens to this last item.
04:05The last item, when it's put in there, there is a rule called LastChildFill.
04:09If you look in my XAML you will see that LastChildFill -
04:13let me undock this, so we get a little more screen real estate -
04:15the LastChildFill is set to True, which means it takes up whatever space is leftover.
04:19What do you think would happen if I change this to False? - which is not the
04:22default. The default is True.
04:24Press F5 again to run.
04:27There is that sixth button, but it is a lot smaller now.
04:31The other panel that was added in the toolkit is called the Wrap panel.
04:36Wrap panel is a lot like the Stack panel.
04:38Now, I have already added the name space, so we can just go ahead and start
04:43using the Wrap panel.
04:44I have already added the Wrap panel and inside the Wrap panel I have several buttons.
04:48I don't have to set any values on there.
04:50The Wrap panel is a lot simpler than the dock panel.
04:53What the Wrap panel does is it tries to work like the Stack panel.
04:56It tries to get as many items on the same line as possible, but if it doesn't
04:59fit it, it will move it down to the next line.
05:01So, since this is marked as Orientation = Horizontal, it's going to try to fit as
05:06many on one line before moving down to the next line.
05:09I'd like to show you a demo, but before I do that I have to change my Startup object.
05:12I will open up my App.xaml.cs file and uncomment this line and comment this line
05:20so I am going to just tell it to use the new Wrap panel.
05:22And let's see what this looks like.
05:24Press F5 again to run. There are my buttons all on the same line, but if I
05:29make the browser window narrower, you will see that they moved down to the second row.
05:33Here is a quiz for you.
05:37What do you think will happen if I change this to say Vertical?
05:42I want you to envision this in your mind what this will look like. Press F5.
05:50Now it's wrapping them vertically and they will move to a second column if I
05:55make this shorter.
05:58Wrap panel and Dock panel are two of the newest available Layout panels
06:02for Silverlight.
Collapse this transcript
Exploring sophisticated layout with the Grid
00:00The Grid panel is unchallenged as the most powerful layout panel in Silverlight.
00:04When you create a new XAML document in Visual Studio or Expression Blend, you
00:08automatically get a grid as the main UI element.
00:11Using a grid is a two step process.
00:13First, you define the rows and columns and then you assign each child element a
00:17location in the suitable row and column within the grid.
00:21If you are following along, you are going to want to open the UsingGrid Solution file.
00:26For my first demo, I am going to look in this AddingRows.xaml file.
00:30Let's see what I have in here.
00:32By default, as I mentioned, you automatically get a grid element as the root.
00:36Since I am doing a demo, I thought I would put a second grid item in here, and I
00:40have already defined three rows.
00:42Notice how they are defined in a section in my grid called RowDefinitions and
00:46what I need to do now is define two more columns.
00:49I can copy and paste, but I thought to show you the IntelliSense inside
00:52Visual Studio here.
00:53You can see that ColumnDefinition is the only available choice, because I am
00:56inside the ColumnDefinitions area.
01:03And then I will copy and paste because it's fast and convenient.
01:07Now I have nine cells:
01:10three rows and three columns.
01:11I want to place this Button in the correct cell.
01:15If you don't supply a number, it's going to put it in cell 0, row 0.
01:20If I demonstrate it by running the application, you can see that the Child
01:25Element, this button is up here in the upper left-hand corner.
01:28For debugging purposes, you can't see the lines in the cell, so there is a
01:33property you can switch on called ShowGridLines in the grid element itself right up here.
01:40If I select ShowGridLines="True" and run the same demo, press F5, you'll see
01:47these ugly dotted lines.
01:49For debugging purposes, you can set these to true.
01:51These grid lines are not attractive however, so I never use them on the finished pages.
01:56Now you can see that this child element is up here in the cell 0, row 0.
02:00I want to move it into this cell right here, the middle cell.
02:04To do that, I will come down to the Button and I am going to use something
02:07called an attach property, which we have reviewed in another movie.
02:10Attach Properties are part of an element that's not the button.
02:13In this case, it's the grid.
02:14So, I am going to come in here.
02:15I am inside my Button, and I am going to say Grid.Row=1.
02:24Silverlight uses 0 as the starting number, so I want to go the second row, which
02:28is row one and then I'm going to say Grid.Column=1, and to prove that it's in
02:36the correct place, we'll press F5. Success!
02:41It's now in the middle cell.
02:43For my second demo, I am going to look inside this file,
02:47PlacingStackpanelInGrid.
02:49A very common scenario in Silverlight is to nest different layout panels.
02:54The grid is the most powerful.
02:56It's almost always the root element, but you end up putting other panels into the columns.
03:02So, for this example, I thought I'd show you placing a stack panel within a grid.
03:06I have got my grid.
03:09Again, I have got my second grid inside the first grid, and I have got one row
03:13and a couple of columns defined in this grid, and then I have decided to place
03:17this Stackpanel here inside one of the columns.
03:21I put it in the last column and then inside there, I have got a several buttons
03:25that are going inside the Stackpanel itself.
03:28All right, so I have defined my column where I want the Stackpanel but then I
03:31need some buttons to stack up inside the Stackpanel.
03:34I need to go to my App.xaml.cs file to change the Startup object, and there you go.
03:48Here is the stack panel within the third column.
03:50The next thing I want to look at is sizing.
03:52How can I control the sizes of my columns or my rows?
03:56The answer to that is by setting values in my RowDefinition.
04:04Now I just opened up the ControllingSizesFinished to take a quick look in here.
04:08When I go to this RowDefinition, I can set, in this case, row has a height value.
04:12I can set the Height value and I can pick a hard coded number, like 12 pixels, or
04:17I can choose auto-defined column.
04:20Rather than editing them here in Visual Studio, I think I am going to switch to
04:23Expression Blend, because Expression Blend has an easy-to-use editor for setting sizes.
04:28I am going to switch to Expression Blend, where I have already loaded the same
04:32project and I am going to double- click on this ControllingSizes.xaml file.
04:37What you see, if you look at my Objects and Timeline, is that I have a LayoutRoot,
04:41which is a grid, and inside that I have a number of buttons.
04:44Now these buttons are all overlaid over top of each other, because if you
04:47look at my XAML, I haven't specified a row or column, so they are all in the same location.
04:51In fact, I don't even have any rows or columns defined.
04:53So, I am going to fix that by using the designer, called the artboard. Here is the key.
04:58When you select a grid, like this LayoutRoot, look what happens in your mouse
05:02when you move in Expression Blend.
05:03You get this little gray bar across the top and as you hover your mouse over
05:06it, you get a little Plus symbol on your mouse, plus you get a gold triangle and a line.
05:12If I click now, I have just added two columns. Let's verify that.
05:17There is the two columns.
05:19I am going to add a third column over here.
05:23And then I am going to add a couple of rows, okay.
05:28Now that I have got these rows and columns, I can grab this and size it if I
05:30don't like the size, and I can put one of these elements in the desired column.
05:35Let's say I want to put this Button here in the second row and second column.
05:38I can click on this Button and then I can go over to my Property Editor and find
05:44the Layout section, where it says Row and Column.
05:46So, this belongs in Row number 0, Column 1.
05:51And for this button, I want it to be in row 0, and column number 2.
05:57Then I will go to this Button and I want to put that in Row number 1, the Column 0.
06:01Remember that Silverlight uses a zero based numbering system.
06:04So, column 0 would be this column right here.
06:07Let's go and verify that this is working.
06:09I will go and click on my XAML and you will see that it wrote. There it is,
06:16right here, Column="2", Row="1" and so on.
06:19So, that's how you move your elements into the correct position.
06:25You can also have an element span multiple rows or columns.
06:28It will be easier to see this if I delete a few of the extra buttons.
06:32I will go to the Objects and Timeline panel, select the Button and press the
06:36Delete key a few times.
06:37So, now I have single button and it's spanning all three rows and three columns.
06:43You can see that over here again by looking in the Property Grid, RowSpan = 3, ColumnSpan = 3.
06:48I really only want this to be in a single row and a single column.
06:52So, I just want to change those to 1 and 1 and now you see it's up in the
06:55cell where it belongs.
06:56If I want it to span multiple cells, like I want it to go across 2 rows, I will
07:00type a 2 in here, and you see what happens.
07:03The grid always stays the same number of columns and same rows.
07:06It's the element that gets changed to span across the different cells.
07:10Let's talk about sizing for a minute.
07:12These cells currently are sized using what's called Star sizing.
07:19This is a proportional system.
07:21What happens is Silverlight calculates all three of the rows or all three of the columns.
07:26I am going to use columns as far as my demos, but the same applies for rows.
07:29Let's change these to some simpler numbers, 2, 2, and what adds up to 10.
07:352+2 equals 4. We'll do 6, so that's 6 star.
07:40This is 60%. This is 20%, and this 20% of the size.
07:45So, it adds up whatever the widths of the entire grid is and then gives this 20%.
07:49If I want to change that to 10%, just type the number and now that this is
07:53one-ninth of the size.
07:55So, it's proportional sized.
07:58There are two other sizes and you can change the size by clicking on this little
08:02button here in the corner and you get the little padlocks.
08:08What this little button up here does is change it to what's called the
08:10Canvas layout mode.
08:11Right now, it's in Canvas layout mode and this is switching it to Grid layout mode.
08:16Now I can click on this to change the size. It's currently Star Sized.
08:19If I click on it again, it turns what's called Pixel sized and that means
08:22it's hard-coded now.
08:23Whatever I specify for the pixel width is the only width it will be. It never changes.
08:27And if I click on it a third time, it switches to what's called Auto size.
08:31Auto size is cool for things like, where you have got a bunch of labels in the first
08:34column and you don't know how long the text is going to be.
08:37So, you say make this first column be Auto sized and it will analyze all of
08:41the elements in that column and whatever the widest one is, that's how wide
08:45that column becomes.
08:46And looking at my XAML, you will see that that now says Auto.
08:50I'm going to click on this two more times to switch you back to closed padlock and
08:55switch over here, you now see that it says, Width 89, no star in there, which
09:00means it's fixed width.
09:03Another way of adding a control to the cell is to drag it directly into the cell.
09:07So, let's add a new button.
09:08Click over here, come over, draw a new Button.
09:12This time, I am drawing the button in the cell.
09:14Click on this arrow up here, the Selection arrow, to make it a little bit easier
09:18to see what I am doing.
09:19And now I can just pick this up and drag it into the new cell.
09:22That should automatically change the row and column numbers as I move them into
09:26the new hosting cell.
09:29I find this much easier than switching over to the XAML view and editing it over there.
09:34Well that's about all the time I have for the Grid panel.
09:37As you have seen, it is the most diverse panel available in Silverlight.
Collapse this transcript
Sizing cells using the GridSplitter
00:00You may want to allow your users to resize the Grid panel at run-time.
00:03You see this behavior in many Windows applications.
00:05For example, here in Visual Studio, I can resize the width of the sidebar, over
00:10here by the Solution Explorer, by moving my mouse over the edge of the Solution Explorer.
00:15The mouse changes cursors.
00:17Grab it with my mouse and move it to a new position.
00:19What I have just done here is made the Solution Explorer wider and I have made
00:23the work area more narrow.
00:25We can mimic this in Silverlight.
00:28If you are following along with the Exercise Files, you are going to want to
00:30open the SizingWithGridSplitter solution.
00:33For this demo, I am going to use the UsingGridSplitter.xaml file.
00:37Let's take a look what we have got in this file.
00:39I have already predefined a grid that has five columns.
00:44The first, third and fifth column are fixed size.
00:49I am going to put my grid splitter in the second and fourth columns.
00:54Now this is not necessary, but I found over the years, that it's better to
00:58dedicate a column for the splitter itself.
01:00There are some rules on the size.
01:02One of the rules is you want the column to be Auto sized.
01:06You will also need a reference to System.Windows.Controls.
01:09This happens automatically if you drag the GridSplitter from the toolbox, like this.
01:13Just scroll down here and find that GridSplitter and drag it over here and drop
01:21it somewhere in my XAML.
01:22So, it has added in my reference folder, System.Windows.Controls, and it's also
01:26added at the top of my XAML file, where it says xmlns:controls.
01:32Delete this temporary GridSplitter down here.
01:35I have already written the code for this demo.
01:36So, let's just go down here and uncomment it.
01:38I have got these two GridSplitters.
01:41Here is a neat old trick inside Visual Studio to uncomment.
01:44You can hold down the Ctrl key and tap K, U, which means uncomment.
01:49You can also use these two buttons here, the Comment and Uncomment button.
01:53Let's see what we have got.
01:54I am placing this GridSplitter in column 1.
01:57I am placing this GridSplitter in column 3.
02:00I am setting the vertical alignment and the horizontal alignment values here and
02:06I am specifying 6 pixels for the width.
02:08There is one other difference between the two GridSplitters.
02:12The second GridSplitter has a ShowsPreview= True.
02:15Let's see what that means.
02:16Pres F5 to run the app, and there we go.
02:20I now have a white 6-pixel-wide GridSplitter in this column, and I can grab it
02:26and resize the first column.
02:28I have 6-pixel-wide GridSplitter over here.
02:32The difference is this is set to ShowsPreview and I get this gray bar when I
02:35drag and it's not until I release the mouse that it resizes the column.
02:40That's what the ShowsPreview does.
02:42The GridSplitter is a simple control.
02:43It's narrowly focused.
02:44It only does one task.
02:46It has this job of sizing the two columns or two rows on either side, but if you
02:50need to provide sizable cells, it's the tool you need.
Collapse this transcript
Absolute positioning with the Canvas panel
00:00Sometimes you don't need all the power of the layout system.
00:02You just want to brute force a location of element on the screen.
00:06This is a terrible idea for data entry forms or other standard input screens,
00:10but perfect when you need a drawing surface in your application.
00:13Another use is when creating logos or Clip Art.
00:16In this case, you need to keep the parts of your artwork closely positioned
00:19within the overall drawing.
00:21The Canvas panel is the only layout container that permits assigning location
00:24based on a pixel coordinate.
00:26If you are following along with the Exercise Files, you are going to want to
00:29open the Solution PositionWithCanvas, and then open the CanvasBasics.xaml file.
00:36In this example, I have a Button inside a Canvas.
00:40The Canvas's job is to figure out where to put all of its children and arrange
00:44them on the screen, and in my case, the first button has not told the canvas
00:48where it wants to go.
00:49So, the Canvas will make a decision and put it at position 0,0.
00:53Left 0 and top equal 0.
00:55I will show you in example.
00:57I will run this app.
00:58And you will see that my Button is positioned at the upper left hand corner.
01:03It's really supposed to go at position 50, 50.
01:08So, I will go back to my XAML and I will notify the Canvas that I want to change
01:15its position with an attach property.
01:17The reason it's called an attach property again, is because the property belongs
01:21to the Canvas, but I am using it inside the button.
01:24So, I will type in Canvas.Left= '50', and then Canvas.Top= '50'.
01:35Now when the Canvas goes to position the button, it has a better idea where I
01:39want it to go and it will honor my request, better.
01:44It's not as good as I'd like however, because this button is overlapping.
01:48I will need to move this button over a little bit.
01:50If I was using Expression Blend, I could just drag this in the artboard, but
01:53since I am using Visual Studio, I have to go write some XAML to do this.
01:56I will come down here and say the left position of this button should be at 220.
02:02This is a terrible way of writing a UI.
02:04It's much better if you can use your eyes and your mouse to figure out where
02:07things belong on the screen.
02:08The next problem is to work with these two buttons and these two buttons are
02:12positioned in relatively the same space.
02:14They overlap with each other.
02:16The Green button and the Orange button are near each other and when I run the
02:19application, you will see that the Orange button overlaps the Green.
02:25These two no longer overlap because I moved them out of position, but sometimes
02:28you want items to overlap each other.
02:30If you want them to overlap, then you will have to specify which one is drawn on top.
02:35How does Silverlight solve that problem?
02:36In Silverlight's case, I didn't specify where I want to go, so it uses the order
02:41that I add them as children.
02:43This Green button was put in before the Orange button, so F5 to run.
02:51The Green button is behind the Orange button.
02:53If I take this Orange button and reposition it inside the canvas, like so and
03:02run the application,
03:03again, by pressing F5, it switched positions.
03:07There is another way to change the ZIndex and that's by using an attach property
03:12called Canvas.ZIndex.
03:14In this situation, I have the redButton ZIndex 11 and the blueButton is ZIndex 10.
03:21So, the smaller number ends up,
03:23press F5, behind the other one.
03:27So, the red index is 11 so that shows up on top.
03:30The way you would change this, of course, is obvious.
03:32You would go down here and set ZIndex to 12.
03:35I need to shut my application down first before I can edit my code.
03:41Change this to 12, and run again and notice that they have swapped positions.
03:48I can also do this programmatically.
03:49I will go back to my XAML.
03:52I am going to press Ctrl+Z to undo my change and I have written a Click
03:56procedure down here.
03:57When I click on the button, I am going to programmatically change the ZIndex
04:00to a higher number.
04:01Let's look at the code behind.
04:03Switch to CanvasBasics.xaml.cs, double- click on that file, and since it's an
04:08attach property, you have to use the special syntax on the Canvas class.
04:12Canvas.SetZIndex and then the item that you want to apply the change to and the new value.
04:17So, I am saying change the ZIndex. The canvas will say, okay you have changed
04:21the ZIndex. I need to draw you in a higher position than you were before.
04:28Click on the button, narrow a little window here to click on and it should move
04:32to the top of the stack. The Canvas is simple.
04:35It does one thing.
04:36It excels at positional layout.
Collapse this transcript
Scrolling content with the ScrollViewer
00:00None other Silverlight layout containers support scrolling.
00:03To add scrolling to your content, you must wrap a container within the
00:07ScrollViewer Control.
00:08If you're following along with the Exercise Files, you are going to want to open
00:12the AddScrollViewer.
00:14For this demonstration, I am going to work in Expression Blend, so I can show
00:17you some features in there.
00:19First of all, I want to open the MainPage. This is the existing page in the
00:23PixelSmith application.
00:25I am going to add scrolling to the gray canvas, here in the center.
00:29Currently, this canvas is configured to automatically size to the underlying grid.
00:33When I run the application, by pressing F5, and size the browser, you can see
00:42that the canvas sizes dynamically.
00:45But the trouble I have with this is that I want to be able to continue to see
00:49the two white ellipses and I cannot do that because I can't scroll down, to
00:53bring them into view.
00:56To fix this I will place the canvas within a ScrollViewer.
00:59To do this, I will use a nice feature of Expression Blend, which is called the Group Into.
01:03I am going to select my canvas. When I select it in the Artboard, you'll notice
01:08the sizing handle will show up.
01:10I could also move over to my Objects and Timeline and can click the Canvas over here.
01:15To use the Group Into function, I right- click on the canvas and scroll down to
01:20the Group Into menu item and then pick one of my containers. In this case, I
01:24want to use the ScrollViewer.
01:27Did you notice the change in the Objects and Timeline? It's subtle.
01:31You no longer see canvas over here.
01:33If I click on the little triangle and open that up, you can see the Canvas has
01:37been embedded inside the ScrollViewer. If you ever want to remove this outer
01:41item inside Expression Blend, you right -click and choose Ungroup, but I don't
01:46what to remove the group right now.
01:47There is another difference. Look over here in my canvas.
01:51In my canvas, there is a scroll bar on the right edge.
01:55Now when I run the application by pressing F5, and resize the browser, you can
02:02see the scroll bar shows up when it's needed.
02:08Now that the bottom part of the canvas is being cut off, the scroll bar shows up
02:12and I can now scroll down to see my white ellipses as needed.
02:18Silverlight assumes that I only want a vertical scrollbar.
02:21If that's not the case, I can quickly add a horizontal scroll by changing the
02:24property in Expression Blend, close the browser.
02:26I'll choose my ScrollViewer in the Objects and Timeline and then we're going to
02:32go over to the Properties panel and search for the word SCROLL. When I do that,
02:40the Property panel shrinks down to only show two properties and they are the
02:46HorizontalScrollBarVisibility and the VerticalScrollBarVisibilty.
02:50I am guessing you can figure out which property I am going to set.
02:53I am going to go HorizontalScrollBarVisibilty, turn that to Auto and run
02:59the application again.
03:03Because it's set to Auto, it doesn't show up until I make the canvas too narrow.
03:07There is my horizontal scrollbar and now it's gone.
03:11So, as you can see, it's quite simple to add scrolling to any container.
Collapse this transcript
Adjusting content with alignment, margins, and sizing
00:00Layout panels are benevolent dictators.
00:02They force their children into position on a page, but each child can influence
00:06its parent container.
00:08In this movie, I will look at three ways the element can affect its ultimate location.
00:11These properties are size, alignment and margin.
00:15Each element has a number of size properties.
00:16You can hard code a size for an element, but this is rarely a good idea.
00:20It is better to allow an element to grow or shrink based on its content.
00:23For an example, increasing the font size on the button should cause the button
00:27to get taller, unless the button has a fixed height.
00:30Size is always honored if you specify it.
00:32If you do not indicate a size, however, then Silverlight has to make some decisions.
00:37The panel will size the element to its content.
00:39For instance, if I have a button and it has a really long string in it, then it
00:42should make the button wider.
00:43Once it's sized it big enough to hold the content, then it applys its own sizing rules.
00:47For example, the Stackpanel stretches all content to fit horizontally. If you do
00:52not specify a size, it will always make it as wide as the panel.
00:56Other examples, the TextBlock's width is determined from a string content, a
01:01button's height is determined from its font size and the Stackpanel will then
01:05stretch that button width to the Stackpanel width, unless it hard codes its own
01:08width, as I mentioned before.
01:10Let's look at this inside Expression Blend.
01:12I've opened Blend. If you are following along with the Exercise Files, you are
01:15going to want to open the AdjustingContent Solution and then doubleclick on
01:19the LiveDemo.xaml file.
01:21In this example, we are going to build a toolbar for our PixelSmith application.
01:25PixelSmith is the application that we build throughout the course.
01:28Most toolbars have text and they have buttons and the buttons are stacked
01:32right next to each other.
01:33For example, in Blend, if you come over here to the toolbar, click and hold,
01:37you'll see this is a sample of a little flyout toolbar. It has an image and some
01:41text and each button is stacked immediately next to its neighbor.
01:45That's the type of view I want to mimic for this example.
01:48To do that, you need to click the arrow button, select your Stackpanel, by
01:53looking inside the border and then, we are going to over and choose the button
01:56and double-click on it.
01:57Because I had the Stackpanel selected, the button was automatically placed
02:01in the Stackpanel and the Stackpanel stretched the button to the entire
02:05width of the Stackpanel.
02:06If I hard code a size, say a width of 120, now it's no longer stretched to the
02:13width of the Stackpanel.
02:14And once it's no longer stretched to the width of the Stackpanel then I can
02:17control the HORIZONTAL and VERTICAL ALIGNMENT.
02:20Here, for instance, I can say I want to align this to the Left edge of the
02:23Stackpanel, or center it, or right align or the default is to stretch,
02:29which is supposed to it stretch it to the width but I've hard coded the width of the button.
02:33If you ever want to send it back to Auto, just click on this button here and blend.
02:38The Layout panel's where you find all of these settings. There's also some lesser
02:41used settings in the section called Advanced Properties.
02:44For instance, in here I can change the HorizontalAlignment of the content.
02:49The content, in this case, is strings but buttons can also hold things
02:52like images and so on.
02:54So, I can align the text inside the button as well.
02:59Let's add a couple of more buttons and then we'll set some margins.
03:02I am going to go back to my toolbar, double-click on the button again.
03:05Then I want to come over here and select my Textblock.
03:11I'll select the Textblock, Ctrl+C to Copy and then select my Stackpanel
03:17and Ctrl+V to paste.
03:19And now I need two more buttons, double-click two more times.
03:23The only issue I have left with this toolbar is the buttons are too close together.
03:26What I can do is add a margin around the edges and that's very simple to do.
03:31Let me select all four buttons.
03:32I am going to come over to my Object and Timeline and do a Ctrl+Click, till all
03:38four buttons are selected.
03:40And then, I am going to go over to my Margin settings in the Properties panel
03:43and type in 9 for each of the margins.
03:51I think that's too far apart between the two buttons, so I will change the top
03:55and bottom margins to 4 and I am happy with that.
04:02And now you have a little better understanding of how each element can control its
04:05position in this Layout panel.
Collapse this transcript
Enlarging your window with Full Screen mode
00:00Removing the browser chrome and maximizing the screen size is advantageous for
00:03certain types of Silverlight applications.
00:06Video players are a prime example of an application type that commonly
00:09launches as full-screen.
00:11If you're following along in the Exercise Files, you need to open the
00:14FullScreenApp Solution.
00:16Once you've opened that, you might want to take a few minutes to look at
00:20the MainPage.xaml. This is our PixelSmith application we've seen in earlier movies.
00:25Now it takes a single line of code to implement full screen mode.
00:29And for my case, I am going to do that in a button click event.
00:32So, I have a button down here on line 44 that has a click event procedure and if
00:38I switched to my code view by pressing F7 in Visual Studio, you'll see that it's
00:43a real simple line of code.
00:44I am going to the Application. I am getting the current application, getting the
00:49browser host, and then saying the content of that browser host should be shown
00:53in full screen, meaning to turn off the toolbars and all the rest of the browser
00:56chrome, very easy to do.
00:58Let's run the application and see what it looks like. Press F5 and there it is.
01:05Clicking on this button will Enter Full Screen mode.
01:07You have to have user intervention to enter full screen mode.
01:10This usually is done using a button click, sometimes with a keystroke.
01:13The reason for this is to prevent your application from arbitrarily starting in
01:16full screen mode and spoofing a desktop application.
01:19The primary security concern is tricking the user in order to steal their user passwords.
01:25To prevent this, full screen mode disables most keyboard keys.
01:29You can use the standard navigation keys, like Home or End, Page Up and Page
01:34Down, the Spacebar and the Tab key and the arrow keys are all usable.
01:38But you cannot capture the alphanumeric keys.
01:41Let's see what it looks like.
01:42I am going to click on the Enter Full Screen mode. Silverlight
01:45automatically puts this message in the middle of the screen that says
01:48Press Escape to leave full-screen mode.
01:51I'll press Escape to go back to the regular mode.
01:54Let's see that again, press Full Screen mode, there is the message from Microsoft.
01:58There's another way to close full- screen mode and that's to write code.
02:02I've that by creating this Press Escape or click here to close button. When I
02:05click on that, we'll go back to normal mode.
02:09Again, this only takes a single line of code. Let's look.
02:12The only difference here, as I said, IsFullScreen = false.
02:16As you see, it only takes a single line of code to provide your users with
02:19a full screen experience.
Collapse this transcript
Manipulating elements with transforms
00:00The container system is fairly flexible and there are a number of panels which you
00:03can choose to lay out your elements.
00:06There are certain situations, however, that don't lend themselves well to
00:08manipulations strictly with panels.
00:10In that case, Silverlight exposes the Transform class.
00:14All transforms in Silverlight are rendered transforms, which means that the
00:18transformation happens after the layout pass.
00:20So, what this means is after every single panel's had an opportunity to
00:25manipulate your element, pick its position, give it a size and it's done with
00:30what's called the layout pass, then the rendered Transform pass happens.
00:34At that point of time, you can further manipulate the element.
00:37There are four basic categories of transforms and we'll look at them now.
00:41If you're following along with the Exercise File, you are going to want to
00:43open the ManipulateWithTransforms solution file and then open
00:47TheFourTransforms.xaml file.
00:51This gives you a overview of the four types of transforms that you can render.
00:55Let's run the applications and see what it looks like.
00:59In the first row, I have a Normal button with no render Transform applied.
01:05In the second row, I am applying a Scale Transform.
01:08I have told Silverlight to make this button smaller than it normally would be.
01:13In the third, I have told it to rotate it.
01:16In the fourth example, I've Translated, which means to move it to a new
01:20position on the screen.
01:21In this case, I've translated it to the right of the original position and in
01:25the last example, I've done a Skew Transform, which as you can see, changes the
01:30angle. I no longer have a right angle between the left edge and the top edge of the button.
01:35So, we are angling the top and the bottom sides down a little bit.
01:39These are the four different kinds of transforms.
01:42The best tool for manipulating transforms is expression Blend.
01:45So, let's go there.
01:46I'll close the Browser window, switch over to Expression Blend.
01:51Here we are inside Expression Bled and you can see I have the same project open
01:54that I was looking at in Visual Studio.
01:56If you are following along, you are going to want to open this LiveDemo.xaml file.
02:01What I have added here is a list box filled with the days of the week.
02:05I'm going to manipulate this inside Expression Blend.
02:07This is really easy to do.
02:09The key in Expression Blend for manipulating objects, if you don't remember, is to
02:12go to one of these two selection arrows.
02:15This first one is called the Selection, and when I choose it and come over
02:18here, and click on the List box, I get sizing handles, margin indicators and if
02:24you know which part of the window you want to work with, you know how to apply Transform.
02:28First let's go look at the XAML and see that I just have a list box with a
02:34bunch of text blocks inside of it.
02:36No Transforms applied yet.
02:37I'll switch back to my Design view and I am going to apply a RotateTransform.
02:43To do that, I'm going to move my cursor over the corner and I can size it by
02:47grabbing it and moving the arrow left or right.
02:51If I move just a few pixels beyond the edge of that selector handle, then it
02:55changes to a rotation arrow.
02:58Now I can grab that and rotate and let go.
03:02That's how you do a RotateTransform.
03:04Expression Blend generates a new section.
03:08It creates a TransformGroup inside the ListBox.RenderTransfrom.
03:11So, that is saying, ListBox, you need to apply this Transform and these are the
03:17four different kinds of transforms that I want you to do.
03:20Notice that Scale, Skew, and Translate don't have any values in, so they are
03:25basically empty transforms, but the RotateTransform has an angle property set to -21.589.
03:32As I continue to manipulate the list box, you'll see those other items change.
03:37For instance, I want to skew the items.
03:39So, I'm going to move to this middle sizing handle and we will get slightly
03:43a few pixels off the edge of that, till the cursor changes and now I can apply a SkewTransform.
03:47One of the cool things about rotation transforms is you can change the center
03:54point by just moving this little circle here.
03:56I'm going to come down and grab this circle and move this circle down here just
04:03off the edge of the list box.
04:04Now when I do my RotateTransform, it rotates around that circle point.
04:10If you want to do a TranslateTransform, which means to move it to a new
04:13position, you don't pick it up and drag it like this.
04:16That sets the margins.
04:18In order to do a TranslateTransform, you need to go to your Properties window
04:23and scroll down to the bottom to the Transform section.
04:27It usually helps to close a few of these other sections.
04:33I'll close the Layout section and the Brushes section and then I'm going to open
04:38the Transform section and there are a number of tabs.
04:41The first tab is the TranslateTransform, the second tab is the RotateTransform.
04:45Again, I can manipulate that by using my mouse over here or by typing in a new
04:50number, let's say 60 degrees here, for the rotation value.
04:54Here is that ScaleTransform.
04:57I will make this 1.5 Larger, press Tab to move to the next text box, and
05:03lastly, I'll show you that you can click on Translate and this is how you can move the item.
05:08You might remember from an earlier movie that you can hover over this box and
05:13move your mouse cursor up and down to change that X and Y value.
05:16Well, that's about it for transforms.
05:19If you can't find a layout panel that suits your needs, chances are that a
05:23Transform will solve your problem.
Collapse this transcript
6. Understanding the Event Model
Understanding routed events
00:00The .NET framework has supported events since the very beginning and
00:03A .NET event means this. A message is sent via the .NET framework from one type to another.
00:09It might be from the button to the form that contains the button.
00:12It might be from a TextBox to a window.
00:15In .NET, when you create an event you add it to a type.
00:19If you create a class, you might create a property on that class.
00:22You might create a method on that class.
00:23You also create events as part of that type.
00:26So they're defined within the type and the event is raised within the type.
00:30For instance, if I have a TextBox, it might raise the TextChanged event.
00:35Other classes might be interested in subscribing to that event.
00:38To subscribe a developer creates a function in the subscriber code with
00:43the correct signature.
00:44In that subscriber, you add a function callback with special registration
00:48syntax. In other words, you have to register with the .NET framework that you're
00:52interested in listening to that event.
00:54When the event is raised, the .NET framework looks to see how many subscribers
00:58there are for each published event. When the event fires it looks up all of the
01:03subscribers and fires each event in turn.
01:07There is this thing called the delegate in .NET.
01:09At its core a delegate is something called a safe function pointer.
01:12A function pointer is just a way of calling a function indirectly and they are safe
01:17function pointers in the sense that you have to register ahead of time with the
01:21.NET framework what the signature of the delegate looks like.
01:24So what I say here, .NET uses delegates to ensure that the event raised by the
01:27publisher can call their code registered in the correct way.
01:32When WPF was released, which is a precursor to Silverlight,
01:36the WPF team decided to create something called a routed event to solve this
01:39thing called a composition problem.
01:41And here's the problem. Many Silverlight controls can contain sub-elements or other content.
01:47For instance, I might have a button that contains a border, within that border
01:51is a grid and within that is an image.
01:53Prior to this routed event, if an element had child content, every child element
01:58would have to write code to handle the common event.
02:01So, if I have an image inside a button, I would have to write a MouseDown event
02:04inside the image and I'd also have to write it at the button level.
02:08With routed events, however, events can bubble up to the handler in the parent element.
02:14Here is an example of the composition problem.
02:17This is some XAML in Silverlight that results in the button that looks like the
02:21screenshot at the bottom.
02:23I have a grid. Within the grid I have a button and within the button I have a
02:27border, a StackPanel, an image and a TextBlock.
02:30If I click on the image and I have a MouseUp event registered and I want to
02:35react to that, let's say I want to show a tooltip, I would have to write the
02:39tooltip show code on the image but also make sure that when you clicked
02:43here on this part of the button that it would show and when you clicked on this
02:47part of the button, it would show.
02:49So I would have to write the code at the Image level and at the StackPanel
02:52level and at the Border level and at the Button level and it gets worse because
02:56in Silverlight, you can replace the entire look of the button with something
03:00called a template.
03:01In that case, somebody could come along and say I'm going to define elsewhere in
03:05my XAML what the look of this button is.
03:07In that case, the designer would have to write all the event-driven code.
03:11So the answer to this is something called routed events and a routed event
03:14traverses the tree of UI elements and it calls every handler on any node in that tree.
03:19It starts at what's called the originating leaf and moves up to the root node
03:23and this solves the composition problem.
03:24I can write the code once at the button level, regardless of what template is
03:29applied to that button.
03:31I know it's always going to work.
03:32Here are some examples of some common events on regular Silverlight elements.
03:37There's our LostFocus and GotFocus routed events, KeyUp and KeyDown, and so on down this list.
03:44These are all routed events so I can have a MouseMove event at the parent level.
03:48So you've also got these normal events.
03:49You've got the Loaded event. What this means is you can only have the Loaded
03:54event on that actual element.
03:55You can't route it up to its parent.
03:57We also have the LayoutUpdated and the SizeChanged.
04:01WPF was the first technology from Microsoft to use routed events.
04:03So, far it has implemented a simpler scaled-down version that eases UI
04:08development, especially when you consider defining replaceable UI in templates.
Collapse this transcript
Wiring up event handlers in Silverlight
00:00In Silverlight, there are two ways to subscribe to an event.
00:03You can define the Event property in XAML or you can write code in the CodeBehind.
00:08I am in Visual Studio.
00:09If you're following along in the Exercise Files, be sure and open the
00:12WiringUpEventHandlers solution.
00:15I have two XAML pages I'm going to be working in, in this demo.
00:18I have one called InCodeBehind where I'm going to wire up the event handlers in
00:23my C# code and I have one called InXaml where I'm going to be wiring up the
00:27event handlers in my XAML.
00:28Let's start here. Double-click on the InXaml.xaml file and then I'm going to
00:35scroll down to these buttons.
00:37The idea that I have for this demo is I'm going to click on the button.
00:41It's going to run some code.
00:43My code is going to update this TextBlock down here with the output.
00:48I'll start by going to this button.
00:50In Silverlight, it often helps to name your elements.
00:53This first button doesn't have a name whereas my second button does have a
00:57name called DemoButton1.
00:59When I go up here to my first button and add the Click procedure. I'll type
01:04in click and you'll note inside the IntelliSense window that this has a lightning bolt.
01:10That's how you know this is the Click property and not a normal property.
01:13In Visual Studio, I can press Tab to finish typing and when I do that, Visual
01:18Studio wants to help me by writing the event procedure for me in my CodeBehind.
01:22All I need to do now is press Tab again and it stubs in Button_Click.
01:29My second button has a name, so if I do the same exact steps down here, Click,
01:34Tab, choose New Event Handler by hitting Tab, the difference is because this
01:40button had a name, the name of my procedure is a little more specific.
01:44You can also apply event procedures to shapes.
01:47I'll come down to this ellipse.
01:49It has a name called DemoShape.
01:51I'm going to use the Mouse events.
01:54This one has a MouseLeftbuttonUp, so I will hit Tab, Tab, and pick a nice long
02:01name DemoShape_MouseLeftbuttonUp.
02:04I'm ready that we'll look at the code. To switch to your code view,
02:07I could double-click on the CS file over here.
02:11I could go to View > Code.
02:13What I usually do however is press F7 and there are my three procedures.
02:19I'll make this window a little bit wider.
02:19The first one is called button_Click and so on.
02:24I'll go into my code.
02:25You remember what's going to happen. The Button class is going to raise the
02:29event and my Silverlight UserControl is going to react to that and run the code.
02:33I'm inside my UserControl.
02:35I'll go here and I'm going to say I have this TextBlock called resultTextBlock.
02:41I don't need to finish typing.
02:42I just type the period. Text="button." This should be a semicolon.
02:56I'll copy this Ctrl+C, and paste it into my other two event procedures and then
03:01just change the string like so.
03:10Press F5 to run the application and if I did everything right, I should be able
03:15to click in this button and see a new output down in the TextBlock.
03:22And finally let's click on the ellipse. Excellent, everything worked.
03:29Another common way of wiring up events is to go to your CodeBehind.
03:31I'll close two windows here. Find my InCodeBehind.xaml file.
03:39Let's look briefly at the XAML here.
03:42It's almost exactly the same.
03:43I have one button instead of two.
03:44I have an ellipse and I also have the same TextBlock called resultTextBlock.
03:51What I'll do now is I'll go to my CodeBehind, double-click on the C# file or
03:55press F7, and now I'm going to do all the wire up in code.
03:59I'm going to do the wire up of the code in this method here called
04:03InCodeBehind_Loaded.
04:08Here's the first example.
04:10I have the demoShape.
04:11This is the instance of the ellipse.
04:13This is the event that's part of that ellipse and this is the special C#
04:18operator that does the wire up and this is the code that's going to handle it. So, let's review.
04:23The object that's going to raise the event, the event itself, the code that's
04:28going to handle the event and this is the key to wiring the two together.
04:33Here's a little more verbose syntax.
04:35It's exactly the same except it's using the delegate syntax here.
04:38It's saying to create a new MousebuttonEventHandler.
04:41This is the legal syntax, but for convenience sake C# allows you to do this
04:46shorter syntax which I prefer because I think it's easier to read.
04:49You might recall we talked about routed events in another movie.
04:52Since this is a routed event, there's also a way to go to the routed
04:56event system and wire up the event handler. That's done by going to your
04:59shape and calling AddHandler.
05:01This is part of the dependency system and then it's a little more convoluted,
05:05I have to type in this static member of the Shape property and then I have to use
05:11the new syntax here. I can't get away with the shortcut.
05:14And then I have to pass a Boolean parameter for this last argument.
05:17Most of the time, you'll see it done like this example.
05:20I though I'll show you a live wireup.
05:22I'll come down here after this comment.
05:25I have a button called demoButton1.
05:29I want to wire up the Click event. When I do the += syntax, Visual
05:34Studio knows that this is an event handler and says, would you like me to write
05:38the rest of this line of code?
05:39I press Tab if I do and it stubs in the new RoutedEventHandler and a suggested
05:45name for the new event handlers it's going to write.
05:47As you can see, in the yellow tooltip, I can press Tab again.
05:50It happened very quickly but what happened is it wrote this chunk of code for me.
05:59Come down here, type in Button and to demonstrate this I need to change my
06:07startup object so I'll go to my App. xaml.cs, scroll down in here and change
06:13my RootVisual to use my new InCodeBehind and then press F5 to run and we have success.
06:26What I would like you to take away with this movie is there are two ways to wire
06:29up your event handlers.
06:30You can do it in your XAML file or your CodeBehind and Visual Studio has a lot
06:34of tools to help you write the code.
Collapse this transcript
Understanding Event Bubbling
00:00You can define a routed event handler in any element in the element tree.
00:03When an event fires, it starts on the original element and traverses the element
00:07tree looking for event handlers.
00:09Each handler encountered on the way to the root element is called in sequence.
00:12I'm inside Visual Studio.
00:14If you're following along in the Exercise Files, you're going to want to open
00:17the EventBubbling solution.
00:20I have three demos in this movie:
00:22BasicBubbling, BubbleToRoot and SuppressingEvents.
00:26We're going to start with the BasicBubbling.
00:28I'll double-click on this XAML file and look at our tree of objects.
00:33We start with the UserControl at the root.
00:35It has a Grid within it, and then within the Grid, I have a button.
00:40The button is built out of complex content.
00:43Inside the button is a border, and inside that is a StackPanel, and inside that
00:48at the bottom of the tree, often called the leaf node are the two leafs,
00:52the image and the TextBlock.
00:55So if I left-click on the image, it could handle the event.
01:00Whether it handles or not, it's also going to go to the StackPanel and see if it
01:03has any event handlers registered, and so on up the tree.
01:07Eventually, you will get to the UserControl.
01:10This is interesting, because I can either handle things at individual levels
01:13like here at this image, and at this border level, or I could come up to the
01:17UserControl to say, I want to handle all the events.
01:20Then all the children elements don't have to have their own handlers. We can
01:23handle them at once.
01:24This is great, because it means I don't have to write redundant code over and
01:27over again for all of the children of this button.
01:29This button has a border and a StackPanel and image.
01:32In the old days, I would've had to write an event handler for each one of those.
01:35Now, I can just do here at the button level.
01:37If you go look at the code behind for this, double-click on the CS file,
01:41you'll see that I've registered my intent to listen to the event on several of my items.
01:46The border and the button, the image, the StackPanel, and the TextBlock have all
01:50registered to listen for the MouseLeftbuttonDown, and they've all said they want
01:54to use the CommonMouseDownCode, which is down here.
01:58All I'm doing in here is gathering up the string with the sender, the TextBlock
02:03or the button that caused the event to happen, and then I'm showing their
02:06results in a list box.
02:07What should happen is every item that handles event should show itself in the list box.
02:11Let's see if that happens.
02:12Press F5. There is my complex button with this complex content.
02:18If I click on the image, you can see that the image had a chance to handle it,
02:22and since the image is a child of the StackPanel and the border, you see that
02:25they also handle the event.
02:27I can dismiss this list box by clicking on it with my left mouse button and then
02:31we'll try it by just clicking on maybe the border here. Because I didn't click
02:36on the image, it never got a chance to handle it.
02:41For my second demo, I'm going to choose BubbleToRoot.
02:45It's exactly the same UI.
02:49The difference is in my registration code.
02:51I'm setting up my handler at the root.
02:55This, in C#, means the current class.
02:58My current class is the BubbleToRoot: UserControl.
03:01That's the same as in the XAML, wiring it up here at the UserControl level.
03:07So none of my children have their own dedicated code to handling this.
03:11What I'm doing here is I'm getting a string with a little bit more detail.
03:14I'm getting the sender.ToString, and then I'm getting the OriginalSource.
03:22I can't press F5 yet, until I go over here and choose App.xaml.cs and change my startup.
03:32Now, I can press F5.
03:33I only have one handler at the root level.
03:39I'll click on the TextBlock and it says, okay, the sender was the
03:43root, BubbleToRoot.
03:44That's the user control and the Original was the TextBlock.
03:46Then I'll click on the border, then I'm going to click on the image.
03:50So, you see what's happening is the parent is catching each one of these.
03:54In my last demo, I want to show that you can suppress the event.
03:58As it's bubbling up the tree, you can say, don't go any higher.
04:03Same tree of objects.
04:06Open up the code behind.
04:08Here in my Constructor, I'm registering an event for the UserControl and one for this image.
04:14If you click on the image, MouseLeftbuttonUp, I say e.Handled = true.
04:19e is coming from the second argument here, MousebuttonEventArgs.
04:22When I say e.Handled = true, I'm saying, don't let this bubble any further up the tree.
04:29That means this code down here, which is supposed to show the string with the
04:32sender and the original, will never get called and I won't see this string.
04:37Once again, I have to switch the startup object.
04:40Double-click on the App.xaml.cs file.
04:46Uncomment the SuppressingEvents user control, and then press F5.
04:52When I click on the grid, I see that that was the originating object.
04:57I click here on the TextBlock. That was the originating object.
05:00I click on the border, that was the originating object, but when I click on the
05:02image, that code no longer runs. Now I'm only writing the code that's inside the
05:07image code, and it's suppressing the event ever getting to parent object.
05:12What I hope you take away from this video is that event bubbling allows you to
05:15capture events in the simple way, at the parent level, and not have to write the
05:19code in all the children.
Collapse this transcript
Detailing the mouse events
00:00There are a handful of mouse specific events available for Silverlight elements.
00:04These events are unique in that they send some mouse exclusive details to your
00:07event code, via the MouseEventArgs parameter.
00:11I've already opened my application, so I can show you how these mouse events work.
00:15This is a very simple interface.
00:16I have a few ellipses and some rectangles on the bottom of the screen.
00:19If I moved this ellipse, I have set the MouseEnter and MouseLeave event.
00:22So, when I move in, it changes the color and on the MouseLeave it sets it
00:28back to the original.
00:29Down here in the Mouse Wheel, if I scroll my mouse wheel while my mouse cursor
00:33is over this ellipse, I can shrink or grow the width of the ellipse.
00:38On this example, I'm capturing the Mouse Move event.
00:41So, as I move from pixel to pixel, you can see where we are inside the user control.
00:46And in the second example, it's where we are in relationship to the ellipse
00:49upper left-hand corner.
00:51And then finally, we have Mouse Cursors.
00:52These are not really events but they are properties that are important for using in your UI.
00:57As I hover over each one of these rectangles, you'll see that the mouse cursor changes.
01:06Let's see how this is done.
01:07I will shut down the browser, which should return me to Visual Studio.
01:12If you're following along with the Exercise Files, you're going to want to open
01:14the DetailingMouseEvent solution.
01:16In this application, I only have one interesting page. That's MainPage.
01:20Double-click to open that, and in this example, I'm wiring up all my events in the XAML.
01:25I'll show you a couple of examples.
01:27Here's one for the Ellipse, MouseEnter and MouseLeave.
01:31Here is the one for the Ellipse MouseMove.
01:37If I look at the code behind, double-click over here in the C# file, here is my MouseEnter.
01:43What's going on here?
01:45I'm getting the sender, which will be the ellipse, and I'm getting some mouse
01:49specific information here in the MouseEventArgs.
01:52In .NET I grabbed the sender, which is typed as object.
01:56That's not good enough however, because I need to cast it to an ellipse type so
01:59I can work with its Fill property.
02:00So I'm using the C# As operator to that.
02:03I say consider this as an ellipse, I'd like to work with its Fill property and
02:07now go get a brand-new brush and assign it to this fill.
02:09On MouseLeave, I do the same thing except I set it to white.
02:14In the MouseMove event, I have to write some code to get the position on the screen.
02:19That's done with a method on the e object.
02:22I call GetPosition. I pass one of my elements on the screen.
02:25In this case, I'm passing the UserControl.
02:28In the second example I'm passing the MoveEllipse itself.
02:31So, that gives me a point value.
02:34I can then go read that point value and read its X and Y coordinates.
02:39What I do with those X and Y coordinates?
02:40I format them into a string with a comma between the two values and then show
02:45them to the user in that TextBlock.
02:48MouseWheel. Simple.
02:49You get the Delta passed to you in the MouseWheelEventArgs.
02:53The Delta is which way you roll the mouse wheel.
02:55Did you roll it up or down?
02:57You get a positive Delta or negative Delta.
03:00So, I take the Width and the Delta, divide that Delta by 10 and then add that
03:06to the existing width.
03:08And I said, I would show you how cursors work.
03:10To do that I need to switch back to my XAML.
03:15Scroll down to these rectangles and it couldn't be easier.
03:20Here is my rectangle, there is my cursor and this rectangle uses the Eraser
03:25Cursor and this one uses the Hand Cursor.
03:28Each one of these mouse events has its place in your developer tool bag.
03:32The MouseLeftButtonDown event is often used to start a drag operation.
03:36The MouseWheel works well for providing a zoom feature in your application.
03:39I encourage you to experiment with each one to learn more.
Collapse this transcript
Detailing the key events
00:00When you need to get text input in Silverlight, you turn to the text controls.
00:04And if you need to react to text input events, you turn to the key events.
00:08I'm in Visual Studio and if you're following along with the Exercise Files,
00:11you're going to want to open the DetailingKeyEvents solution.
00:14This is a simple project.
00:15It has one interesting file, MainPage.
00:19MainPage has some text boxes in it, which I want to use for showing you
00:23the keyboard events.
00:25I'm scrolling down in my content until I find several text boxes.
00:31Here is one and it has a TextChanged event.
00:34Here is one that has no event, and a second one that has no event.
00:40At the top level, I have a routed event setup.
00:43So, at the Grid level I have a KeyUp set for the entire grid.
00:47What this means is it's going to call this function, KeyUp, which I will show you right here.
00:56And all I'm doing here is analyzing the KeyEventArgs and finding out which keys you pressed.
01:01So I look at some code.
01:02If you've the keyboard modifier turned on and you also press the keyboard
01:06control, I'm going to show a string Ctrl, and if you've the Shift key enabled,
01:11I'm going to show you Shift.
01:13And then I'm also going to show which key you pressed, and then the modifiers.
01:16Let's see what this looks like.
01:18Press F5 to run the application.
01:21Then I'll go to this text box and type the letter a. And in the list box down
01:24below you see that I typed the letter a, and I didn't choose any modifiers.
01:28Since this event is at the Grid level, I can also go to this text box and type
01:32say the letter b, and you see that I typed the letter b with no modifiers.
01:36Let's do Ctrl+A, which selects all the text, and you see that I now get a Key = A,
01:42and I have the modifier of Ctrl set. All right.
01:45Let's go back and look at smart code.
01:47Down here, I've one of my TextBox set to have TextChanged.
01:51This event fires every single time the text is changed in any way in that
01:55TextBox, by deleting a whole bunch of characters and so on.
01:58And while I'm in there, we might as well look at this code.
02:02This is at the TextBox.
02:04Remember I've a KeyUp at the root level.
02:07Now, I'm setting a KeyDown event for one of the text boxes and only one of the text boxes.
02:13What I'm doing is looking at the KeyEventArgs, seeing which key you typed.
02:17If it's equal to Key.B then I reject the keystroke.
02:21I say e.Handled = true, and that says don't show the text in the textbox.
02:27Let's see what this look like. Press F5 to run.
02:31Come down here and type a few letters.
02:34See that the TextChanged event fired.
02:36I'm going to select this letter, and then I'm going to choose Delete and that
02:40should fire the TextChanged event.
02:44For my last demo, I'll come down here and I'll type the letter a.
02:50Everything worked fine.
02:51I'm going to type the letter b. You can see that I typed the letter b in the
02:55list box, but nothing shows up in the text box.
02:58This is very handy when you have text items where you don't want the user to
03:01enter certain keystrokes, like no numbers, no periods.
03:05You can reject those without having to check the text after it's been entered.
03:09You can check it while it's being typed in the text box.
03:12There are only a handful of key events in Silverlight. Even so they provide
03:16several valuable services.
03:17For example, they can help you determine when text is changed so that you can
03:20warn the user that their data should be saved.
Collapse this transcript
7. Using Silverlight Controls
Displaying text on the screen
00:00It's hard to think of a practical user interface that doesn't show text to the user.
00:04Even the most intuitive touch-based applications have lists of data or show text
00:08in a hovering tooltip.
00:10The text block and the label are the two elements to show read-only text in Silverlight.
00:14Since the label is mostly used for data binding scenarios, that will leave us
00:18with the text block as the main subject for this movie.
00:21In this movie, I decided to use Expression Blend to edit my files.
00:24If you're following along with the Exercise Files, you're going to want to open
00:27the DisplayingTextOnScreen solution and then double-click on MainPage.xmal.
00:32In this application, I'm enhancing our PixelSmith application.
00:36PixelSmith is the application that we built throughout the course and in this
00:40chapter, we're going to be modifying PixelSmith in nearly every movie.
00:44I need to zoom out a little bit, so I'm going to take my mouse wheel and scroll
00:48and then, I'm going to hold down the Spacebar and use the mouse to center the
00:52PixelSmith UI on the screen.
00:54What I'm going to fix in this demo is this text block right here.
00:58I cannot see the text.
00:59So, I'm going to fix that first by modifying the raw XAML.
01:02I'm going to move to the XAML screen.
01:06If you press F4 while you're in the XAML view, you can see a full-screen view,
01:11and then I'll scroll down and find my comments in here.
01:15The first change that I want to do is I want to remove the Text property.
01:18I'll cut all of the text and what I'm going to do is place it between the
01:26beginning and ending TextBlock tags.
01:27So, now I will move to the end of the TextBlock, delete this tag, type in an
01:31angle bracket and then paste in the text I just cut at this location.
01:37Now why am I doing this?
01:39Because later in the demo, I'm going to start putting line breaks and multiple
01:42fonts in here and the only way I can do that is if I have text between the
01:46beginning and ending tags.
01:48Now it's time to return to my editor.
01:51I'll click over here on the Design button, and then I'll press F4 again to
01:56get my toolbar back.
01:57I ensure that I've got my TextBlock selected and then I'm going to go over to my
02:01Properties panel and type in wrap.
02:04What that does is filter down available properties, so that I can see
02:08TextWrapping and then I can say, I'd like to wrap the text.
02:11As you can see, that's already an improvement.
02:14Now it's time to go back to my XAML.
02:16So, I'll click on the XAML button.
02:18Press F4 to go to full-screen mode, and what I want to do next is add
02:23formatting to this text.
02:24So, this is kind of like editing HTML text.
02:27I want to put a line break in here, and I'm going to do that right after the word "buttons."
02:36I'll put two of those in here.
02:40And then I'm going to create something called a run.
02:42I'm going to put a run around this word "work area."
02:46A run is a lot like a div tag in HTML.
02:52I see that Expression Blend Auto Complete has made a mistake.
02:55I didn't want the word Runwork, so I'm going to go ahead and delete it.
03:00By placing a run around the word "work area," I can then format just a section of the text.
03:19I've added the Foreground = "Gold" and TextDecorations="Underline".
03:23It's time to see what this looks like in the designer.
03:26Switch back to the Design view and zoom a little bit.
03:29There is my two line breaks and there is my underscored and gold text.
03:34So, that's it for formatting a text block.
Collapse this transcript
Gathering text input from the user
00:00There are times when you need to gather text input from your users.
00:03There are three controls I am going to look at today.
00:06One is the TextBox.
00:07The second is the Password Control.
00:09And the third one is this interesting new one called the AutoComplete Box.
00:12If you're following along with the Exercise Files, you're going to want to open
00:15the GatheringTextInput solution.
00:17We're going to look at two places here.
00:20MainPage is our typical PixelSmith application.
00:23The only difference is -- press F5 to take a look at it.
00:28In the UI, I have added this button here called Show Options.
00:30And when you click on the Show Options button, it's going to launch a special
00:34window called the child window.
00:36I have a whole movie on child windows, but let me just say this, child windows
00:39are windows that pop-up over the main UI.
00:43And then I can dismiss them and they disappear.
00:45It typically uses dialog forms.
00:47If I close the PixelSmith and return to Visual Studio, here is the child
00:53window, the OptionDialog.
00:54And this is full of different TextBoxes.
00:59We have the obvious one, the simpleTextBox for just typing text in.
01:04We also have another very easy one to understand the PasswordBox.
01:08All the PasswordBox does is bullet out your text as you type.
01:11You can customize the PasswordBox with your own characters, if you don't
01:15like the black bullet.
01:18In the next TextBox, I have some sample text in there and I want to spend a few
01:22minutes talking about the selection properties you can do in the TextBox.
01:26So, I've added a few buttons.
01:27Again let me show the demo, and then we'll come and look at the code.
01:31Click the Show Options button, type some text here.
01:35Type some other text here, so you can see the block type characters.
01:38And then in this example, I can choose Get Selected.
01:42I'll select some text here, and choose Get Selected and it prints it out in
01:45this other TextBox.
01:47I can set the selected text by clicking here.
01:49I can choose all the text and finally, I can specify a string and it will find
01:54the string within the TextBox and select that value.
02:01To see more, I am going to have to go to the code behind page.
02:06Here is the code for getting the selected item.
02:09I go to the TextBox and I get the SelectedText property and assign it to a string,
02:13then I show it in the other text box.
02:16In the setSelected, I move the Focus.
02:19This is important, because if the Focus isn't on the TextBox, it won't do what you want.
02:22So, I set the Focus to the TextBox and then I say Select.
02:27You want to set the start position and the number of characters that you want to return.
02:32SelectAll is even simpler.
02:33You just set the Focus and then call the SelectAll method.
02:36And then the selectByString, as I move the Focus there, make sure that you
02:41have something selected.
02:43And if you do, then I replace it with this string.
02:46I will show you that in a few minutes when I return to the demo.
02:49The next one I want to look at is the AutoCompleteBox. Here it is.
02:57The AutoCompleteBox is part of another library.
03:00So, I had to bring it into scope.
03:03I have this property down here set. IsTextCompletionEnabled = "True".
03:07And I have a FilterMode that I can set.
03:09In this case, I am saying, if the word starts with my value.
03:12The idea behind the AutoComplete is you give it a list of words, and as you
03:17type in the AutoCompleteBox, it gives you a dropdown list of those words so
03:20you can complete them.
03:21The list of items comes from my code behind up here in the constructor.
03:29I have created a list of strings with the day names available, and then I took
03:34this list of strings and applied it as the item source of the AutoCompleteBox.
03:38So, now we'll run the application.
03:42I have got two more parts of this demo to show you.
03:44One is if I've got some text selected in here, I can select by string.
03:47It replaces what I had selected with the new string.
03:53And then down here in the AutoCompleteBox, I can type in Monday, Tuesday, and so on.
04:00And the AutoComplete part is that I don't have to finish typing anymore.
04:03I can just hit Enter to have it AutoComplete my date entry.
04:07That's all I have for text controls.
04:09All three of them are them are useful, but I will have to say that my favorite
04:11is the AutoCompleteBox.
Collapse this transcript
Using and embedding Silverlight fonts
00:00In this movie, I like to talk to you about using and embedding Silverlight fonts.
00:04Any element in Silverlight that displays text has a font property.
00:08If you dig into the font property, you'll find lot of interesting pieces there.
00:11You can change the font name through the font family.
00:14You can you change its size or its weight, make it bold.
00:17You can change its style, which means italics usually.
00:20And then you can stretch it.
00:22FontStretch only works on fonts that have condensed or expanded versions.
00:27Silverlight includes a number of built-in fonts.
00:30As long as you use these fonts in your Silverlight application, you won't have
00:34to do any extra work.
00:36However, if you use anything other than the standard fonts, you have to embed
00:39them in your Silverlight assembly.
00:41Let me show you how to do that in this movie.
00:43The basic steps are you add the font file to the project, say a TTF or an OTF file.
00:49You set the build action for that font to resource and then in your code,
00:53whenever you want to use it, you use the font family name.
00:55You use the physical filename and then the # symbol and then the font name.
00:59If you use the Expression Blend, you don't have to go through any of this,
01:02because Expression Blend does all the work for you.
01:04Be sure that you verify that you have a license to embed this font.
01:07If you're on Windows XP, there is a utility called the Font Extension Utility
01:11that lets you investigate this.
01:13Here is a URL to find out more.
01:15If you're on Windows Vista or Windows 7, you can just right-click on the file
01:19and choose Properties.
01:20Let me show you that.
01:21I am inside the Fonts folders inside my Windows application.
01:24Let's say I am interested in trying to embed this font in my
01:26Silverlight application.
01:28I right-click on it, and choose Properties, then go over to the Details tab and
01:34look at the Font embeddability property.
01:37The two legal values here are Editable or Embeddable. All right!
01:42Let's choose Blend to see how it is to add a font.
01:45If you're following along in the Exercise Files, you're going to want to open
01:47the UsingFonts solution, then double-click on the MainPage.xaml.
01:53This solution contains the PixelSmith application, which we've been working on
01:56throughout this course.
01:57I can't see the top.
01:58So, I am going to hold down the Spacebar and then drag till I can see the word PixelSmith.
02:03Then I am going to select PixelSmith.
02:06Next, I am going to go to the Property panel, go down to the Text property.
02:10You might need to collapse a few of the sections, and then in this dropdown,
02:15you can see your fonts.
02:16I am going to scroll to the top.
02:19Any font that has this blue symbol here is a built-in Silverlight font.
02:22So, that means I can just click Arial Black, and use that font.
02:27If I pick a font that's not one of the included font, like Aharoni, I get a
02:33warning inside Blend.
02:35The font, Aharoni, isn't a built-in Silverlight font.
02:38You must embed this font for it to display in your application. Okay.
02:42I will select Embed and run the application.
02:47That's all you need to do.
02:49The steps were choose the font, make sure you select Embed.
02:56You can also see, using the Font Manager, which fonts you want to embed in the application.
03:01You can see that I have got one font embedded right now.
03:03I guess the takeaway from this movie would be use Blend whenever you want to do
03:09a non-standard font.
Collapse this transcript
Understanding content controls
00:00Before learning buttons and other elements, we should spend a few minutes
00:03learning about content controls.
00:05This is a simple concept that has powerful ramifications in
00:07Silverlight Templates.
00:09A few of the Silverlight elements derive from ContentControl.
00:12All this really means is that the control can have child content.
00:15In other movies, we've seen other elements like the panel that can contain children.
00:19But its main purpose is to control the layout of its children.
00:22And there's another element called ItemsControl, which is used for showing lists of data.
00:26Content controls just have one simple job. They render any children content.
00:32That could be a UI element, like an image control or a TextBlock, or it can be a .Net type.
00:37Now since .Net types don't usually have a UI, you specify the UI with something
00:42called a DataTemplate.
00:44The number one rule about content elements, they can only have a single child element.
00:49Here are some of the content controls.
00:52This is the base class, the ContentControl, and then there is a ButtonBase
00:55class, and then I see a few buttons over here.
00:58There is the button that derives from ButtonBase, the RepeatButton, the
01:02Hyperlink and two types of ToggleButton, the CheckBox and the RadioButton.
01:07The other really interesting one is the ToolTip, which is your little floating
01:11tip that pops up as you hover over an item.
01:13All of these can contain children content.
01:17I have switched to Visual Studio.
01:19If you're following along in the exercise files, you're going to want to open
01:21the ContentControl solution.
01:23And then you're going to want to double-click on MainPage.xaml.
01:27We'll start by looking at a CheckBox.
01:30A CheckBox is a content control. I specified Content as a string.
01:35Since strings cannot be loaded into the visual tree, you have to place them in
01:39something that can be shown in the visual tree.
01:41In this case, it would be the TextBlock.
01:43If you desire, you can specify your own explicit TextBlock as I've shown here in line 31.
01:48I have a TextBlock inside the content of my CheckBox.
01:51Let's see what they look like.
01:54Press F5, and I have two check boxes, both showing strings.
02:01You might remember that I said just a minute ago, that there is only a single
02:07child allowed as content.
02:09In this CheckBox, I have a StackPanel as the child.
02:12But since the StackPanel can handle multiple elements, I have put two TextBlocks
02:17within the StackPanel.
02:25I've now added a CheckBox to the StackPanel, which is inside the CheckBox.
02:31Yes, it sounds odd, but let's take a look.
02:34Here is my check box and here is a check box inside the check box.
02:38Now I am not encouraging you to go ahead and do silly things like this in your UI.
02:41But the point here is that ContentControl can have any valid UI elements.
02:46For instance, what about something more useful like this image, and this text?
02:50And this becomes really powerful later when we start learning about control templates.
02:54I can completely replace the UI of this button with my own UI of any valid UI element.
03:00Would you like to see how I put this image in the button? Sure.
03:08Here is my button, here is my content.
03:11Inside the StackPanel, I have an Image and a TextBlock.
03:15We'll learn more about content controls in other movies.
03:17Content controls may seem like a simple idea, but they enable some powerful features.
03:21Most importantly, they enable data templates and they enable control templates.
Collapse this transcript
Understanding the button controls
00:00The button is a simple to understand control.
00:02The most common use of the button is to initiate an action, but you may be
00:05surprised to learn that the check box and the radio button are also considered button elements.
00:10I think we should just go right directly to Visual Studio.
00:12If you are following along with the exercise files, you're going to want to open
00:15the ButtonControls solution and then double-click on the MainPage.xaml file.
00:21I thought I would show what the user interface looks like first before
00:23modifying any of the XAML.
00:25Press F5 to run the application.
00:28I have two buttons at the top, which I am going to use to demonstrate the new
00:30ClickMode feature in the button.
00:32I am going to add another control called the RepeatButton and then we are going
00:36to discus the check box and radio buttons, in particular how you can group radio
00:40buttons into separate groups.
00:41Let me start by scrolling down here to the first button and I'm going to set
00:48something called the ClickMode on this.
00:50There are three ClickModes, only two which you use regularly in Silverlight.
00:53The first one we're going to look at is the Release mode and the second one down
00:58here will be the Press mode.
01:03The difference is when the click event fires. In the Press mode it fires
01:06immediately as soon as you mouse down.
01:08In the Release mode it does not fire until you let go of the mouse button.
01:11Let's see how that looks. Press F5.
01:17On this first button, I am going to click-and-hold.
01:18You see that the button turned blue.
01:20That signifies I am holding the mouse button down.
01:22I let go the mouse button and you see my time-stamp.
01:26On this one, I am going to click- and-hold. The event fired immediately.
01:29You can see I am still holding the button down, because the button still blue.
01:33When I let go of the button nothing additional happens.
01:37Sometimes in your application you want to have a button that repeats an event
01:40over and over again.
01:41There is a special button that does that in Silverlight.
01:43It's called the RepeatButton.
01:45For my demo I am going to copy this button code right here and I am going
01:50to paste it down here.
01:54Then I need to modify a few things.
01:55The first thing I need to do is change its name. Otherwise the compiler will not like me.
02:02Change the event handler name. Come up here and change this to say RepeatButton
02:07and then I am going to modify three properties.
02:09I am going to delete the ClickMode and I am going to add a delay.
02:13I am thinking of one second delay or 1,000 milliseconds will do, and then I'd
02:20like the interval-- When you repeat what's the values that's going be added.
02:24I think I will use 50 for this one.
02:28We'll run the application again.
02:35I did not change the text on the button, but this is the RepeatButton here and
02:39when I click on it, you see there is a one second delay and then it starts
02:44firing at 50 milliseconds.
02:48Every time it fires I've got a little bit of event code in my C# file.
02:52Let's double-click over here, and I am just incrementing a counter and then
02:58calling the counter.ToString and assigning that to the text property of this TextBlock.
03:05Next, I would like to talk about check boxes.
03:06They are pretty simple.
03:08They're basically a toggle button.
03:09You click once to turn them on.
03:10You click another time to turn them off.
03:12Instead of looking like a button though, they have some text and they have a
03:15checkmark within the check box.
03:18In the first CheckBox, I've said IsChecked is True, so it should automatically
03:22have the checkmark in it when we see this for the first time.
03:25In the second CheckBox I've set the IsChecked property to Null.
03:28Well, what does that mean?
03:30That means it's not checked and it's not unchecked. It's neither.
03:34That's used when you have two or more items that have a Boolean property and you
03:38can't represent them, like I have a read- only file and not read-only file and I have
03:42a check box that's supposed to represent that and I'd select both files.
03:44I can't use a check box to show that state.
03:47In this third text block I've done the same thing. The only difference down
03:50here as I said IsThreeState = "True".
03:52What this allows is the user can select from checked, unchecked, and indeterminate.
03:58Whereas in this check box, once they click on the check box, they only have the
04:02choice of checked or unchecked.
04:05Press F5 to see what this looks like.
04:08First check box on and off, the second check box is currently Indeterminate
04:12and I click on it once.
04:13It goes unchecked and then back to checked and so on and in the third one, it's ThreeState.
04:21Next I want to talk about radio buttons.
04:23I have three radio buttons here.
04:25This is a color radio button, Orange and Burgundy, and they're mutually exclusive
04:30and then Shiny I want to be part of a different group.
04:33I am going to put another radio button in here.
04:40There are my radio buttons.
04:42You turn on the IsChecked property to show the little black dot in the radio
04:47button and for my last example down here I am going to choose this last radio button.
04:53Paste in a copy, do a tab to fix things up a bit.
04:57Change this to say Matte, IsChecked is False and lastly, I want this to be part
05:04of the same group name.
05:06That's how you link two radio buttons together.
05:09Both these radio buttons share the same group name, so only one of these can
05:12be checked at a time.
05:14In the last radio button, I have set the IsChecked to False.
05:17Now when I run this, these two radio buttons are part of the same groups.
05:21So only one of them can be enabled at a time.
05:22Let's give it a try.
05:24Press F5 and if we look, the first two radio buttons are mutually exclusive and
05:31the second two radio buttons are mutually exclusive.
05:34Here is a little tip.
05:36It's possible for you to set both of these to True or both of them to False.
05:44However, if you set them both to True, you've created a set of radio buttons
05:47that you can't disable the second radio button.
05:50Sometimes developers like to have both radio buttons not selected so the user
05:53has to initialize the first value.
05:55In that case we would just set them both to False.
06:04And run the application by pressing F5.
06:08You see neither one of them has a bullet in it, but now I can
06:10switch back-and-forth.
06:12Really that's all I have for buttons.
06:13They are very simple controls, like I said at the beginning of this movie.
Collapse this transcript
Showing list data with ItemsControls
00:00I'll wager that you have lists of data you need to show your users.
00:03You probably have arrays packed with financial information, lists of customer
00:07statistics, warehouses full of data, and you need a way to show those to your users.
00:12That's why all UI frameworks have list controls.
00:15In Silverlight, these list elements are called items controls.
00:18Their purpose is to wrap a collection of data and alternatively apply a data
00:23template to the results.
00:24I'm in Visual Studio.
00:26If you're following along, you're going to want to open the
00:28ListDataWithItemsControls solution and then double-click on the AddInXAML.xaml file.
00:38On line 15 of my XAML, I have a list box.
00:41You can add items directly.
00:43This is common when you just have a simple UI where you need one or two items in the ListBox.
00:48I've decided to add a TextBlock and then some more unusual items, a TextBox,
00:53a Button, and a CheckBox.
00:56Any valid UI element it can be added as a child of a ListBox.
00:59In fact, I am going to come down here and add an Ellipse, fill it with orange.
01:05Oh, I see I have got OldLace instead of Orange.
01:08OldLace is a valid color, so I am going to go ahead and leave it in, and I will
01:10set the Width to 30 and the Height to 30, close my element. Oops!
01:18I have a typo here, too.
01:22Now let's run this application.
01:23Let's press F5 and you can see there is my text block, there is a text box, which
01:32I can type in even though it's in the list box.
01:34Hidden there at the bottom is my OldLace ellipse.
01:37Now you'll notice I can select it.
01:39Ellipses by themselves are not selectable, but it's inside the list box now, so
01:43it does have a selection handle on it.
01:46That is because all list controls have a helper that works with them.
01:50In our case, the ListBox has something called the ListBoxItem.
01:55So, when I put this CheckBox in up here, it actually got wrapped in a ListBoxItem.
01:59I can also do it explicitly as I've done in this second ListBox.
02:02Here is a more practical example of what you might put inside a ComboBox or a
02:06ListBox instead of an ellipse.
02:08I've created a StackPanel and inside the StackPanel I have an Image and a
02:13TextBlock and then I close my StackPanel.
02:15Then I am going to run this application. I am supposed to be able to see my
02:21ComboBox down here at the bottom of the screen.
02:23I've made an error.
02:24If you watch one of the earlier movies we talked about avoiding hardcoding sizes.
02:29In this example, if you take a look at the top of my XAML file, you'll see that
02:33I hardcoded the height of my UserControl.
02:36So, it's not flexible enough to shift.
02:37So, I can type something in here like Auto.
02:41Run the application a second time, and now the combo box appears and if you look inside,
02:47there is my image of the camera and the text that goes next to it.
02:51That's a more practical use.
02:52You can even do more with this with something called data templates and we'll
02:56talk more about data templates in another movie.
02:58In the real world, you're probably going to have code, however, that contains data.
03:03So, there has to be a way to programmatically add data to these ListBoxes.
03:06So, that's what my second example will show.
03:08First though I need to go to my App.xaml .cs file and change my Startup object.
03:17Then I need to open this FromCollectionInCode.xaml file.
03:23In the XAML itself I just have two list boxes with names and in the code behind,
03:26I am going to press F7 to go to the code behind.
03:31I'm showing how you can go directly to the ListBox it has a built-in items
03:35collection and I can say Items.Add and put a string in there.
03:40Another way of doing this would be to go and have some data store.
03:43In this case I have a list.
03:44This is a generic list of strings.
03:46I declared a variable here called days.
03:49I filled the list of strings with the days of the week strings and then I can
03:54assign the days list directly to the ItemsSource.
03:57Now notice there is a difference, Items, ItemsSource.
04:01You can only have one of these active at a time.
04:03Again, I'll reiterate. This is the built-in one that is part of the ListBox.
04:06You use ItemsSource when you are doing data binding.
04:09Let's see what this one looks like.
04:13And there you go.
04:14There is the first list box with the month names and here's the second one with
04:18the days coming from that generic list.
04:21There are a surprising number of elements that derive from items control.
04:24The TreeView is one that surprises most people is the TabControl.
04:27Also, there is one called the DataGrid, which allows you to do multiple rows,
04:32and multiple columns and you'll find that inside the Silverlight toolkit.
Collapse this transcript
Exploring the Silverlight toolkit controls
00:00The Silverlight toolkit is chock-full of extra controls including the TreeView,
00:05TabControl, several chart controls, calendars, the DataGrid and many more.
00:09In this movie, I'd like to show you a few of the controls that are available in this toolkit.
00:13Remember the toolkit is developed by the Silverlight team, is free to use, and
00:17is available from CodePlex.com website.
00:20Using a control from the Silverlight toolkit requires that you add a
00:22reference to the toolkit DOL.
00:23I will show you how to do that in Visual Studio.
00:26If you're following along, you are going to want to open the
00:28ExploringSilverlightToolkit project and then double-click on that
00:33MainPage.xaml file.
00:36The Silverlight application of this project should look familiar to you.
00:39It's PixelSmith, which we've been building throughout the course.
00:43When you install the Silverlight toolkit, it adds a Silverlight controls to the
00:46Visual Studio toolbox. Let's find them.
00:49Here is the toolbox. I am going to pin it to the side of window by clicking
00:53the Autohide button.
00:54I am going to take this Accordion control and I'm going to drag it into my XAML.
00:59When I do that, it's going to do two things.
01:01It's going to add a new XML namespace to the top of my UserControl and it's going
01:04to add a reference to my References folder.
01:07So I will open my Reference folder so you can see that happened and I need to
01:10scroll down here and drag my Accordion control.
01:15Keep an eye on the References folder.
01:21We now have two new references and I also have a new XMLNS:layouttoolkit added to
01:29the top of the UserControl.
01:33I am going to run the application so you can see what the user interface
01:35looks like right now.
01:37I have five buttons in this area and I am going to wrap three these buttons in
01:40one accordion item and two of them in another accordion item.
01:46To do that I need to take this ending tag for the accordion and move it down
01:52here where this comment tag is.
01:54I am going to cut the ending tag and paste it right here.
02:00Next, I need to wrap this StackPanel in an AccordionItem.
02:11Again, I need to cut this end tag and paste it after the end StackPanel.
02:19Then I am going to take both these tags and wrap them around the StackPanel down here.
02:22I am going to copy these.
02:23Copy this one, paste it here right after the StackPanel, and copy the first one,
02:29this AccordionItem right here on line 45, and paste it right above the StackPanel.
02:36I am going to have in Visual Studio rearrange my code. Format Document makes it
02:43little bit easier to read.
02:45Then I am going to hide my toolbox.
02:47Then let me review what I've done here.
02:49I have created an Accordion control.
02:51I've created one AccordionItem.
02:53I've wrapped the StackPanel and its three buttons inside the AccordionItem.
02:57Then I've created a second AccordionItem and I've added this StackPanel with its
03:02two children buttons within it.
03:03Let see what it looks like.
03:08Press F5, launch the browser, the first three buttons are in the first
03:12AccordionItem and the last two buttons are in the second AccordionItem.
03:15It's not quite perfect yet.
03:18I am going to make two more changes.
03:20I am going to add a header to the AccordionItem.
03:23So, I will find this one right here on line 46.
03:29Header = 'Shapes' and we are going to make the font larger.
03:38Then I am going to copy this Header and FontSize and I will paste it down here
03:44on the second AccordionItem and then modify this to say Images.
03:51One final change I am going to go to the Accordion itself and then I am going to
03:57set the ExpandDirection to Right.
03:59You will see what that does in a minute.
04:01Press F5 to run the application.
04:06By setting the ExpandDirection to Right we now have the shapes turned sideways and
04:15by adding a header, I know it's Shapes and Images, so I know what will happen when I
04:18click on this button.
04:22There are a number of controls in the Silverlight toolkit. Rather than write a
04:27bunch of code in this demo, I'll show you another way you can learn more
04:29about these controls.
04:30When you install the Silverlight toolkit, it adds a demo application.
04:34You can find that in all programs.
04:37The Microsoft Silverlight 3 Toolkit November 2009, that's the latest version I
04:43have installed on my computer.
04:46Then is folder I am going to open up this toolkit Samples.
04:50This is a really nice reference application.
04:53You can spend hours in here learning how this works.
04:55I am going to start by looking at the DataVisualization section and clicking on the pie chart.
05:02What you see here is an example of the pie chart in use.
05:05You can come down here to the bottom of the window and click PieSample or
05:08PieSample.xaml.cs or Pie Sample. xaml.vb to see the source code.
05:14I can collapse this by clicking this button over here.
05:16This is a Silverlight control.
05:20Scroll down to see multiple versions of the chart.
05:22You notice that some of them have dynamic items being added on a timer or an
05:30animation and then I can click over to see animated charts.
05:34Scroll up so you can see those a little bit better.
05:40That's fun. There is also a section on clendars up here at the top.
05:46Scroll through to find a date, select it.
05:48There are several controls up here for choosing how you want the control be configured.
05:52Don't allow selection, Contiguous Dates Selection, so I can choose multiple days
05:57in a row, but I will also show you the TabControl.
06:01Here is the TabControl.
06:02I can dynamically add items by clicking on this button to this TabControl.
06:06You can see up here you can put sub-content on each tab item.
06:13Another favorite of mine is the BusyIndicator down here.
06:18You can show this UI to your users when they are waiting to have something
06:21download, like for instance over here and you can click on Cancel or Pause
06:25during this process.
06:27Lastly, I want to show you the TreeView. There it is.
06:32It looks like most TreeViews you expect.
06:36You open a node and there are your children nodes underneath it.
06:41There are a massive amount of controls available in this toolkit and I know you're
06:44going to enjoy exploring them on your own.
Collapse this transcript
Digging into the DataGrid
00:00The king of the ItemsList is the DataGrid.
00:02This element can show multiple rows and columns and has sophisticated layout and formatting.
00:06It also supports a rich set of events, which fire as the user manipulates
00:10the data in the grid.
00:11For this demo, I am going to use Expression Blend primarily because it has some
00:15excellent data binding tools.
00:17If you're going to bind the data grid to data, you need a data source.
00:20In my project, I have a Person class, which is quite simple.
00:24Double-click on the Person.cs and you can see that it's a Person class with a
00:27FirstName, LastName, Gender and ScienceGroup property.
00:32I also have a Persons class, which derives from the list of persons.
00:38That means that it can contain multiple persons.
00:41It's strongly typed to only hold the Person class and as you can see, in my
00:45constructor I'm adding a number of new person items to the list.
00:50If you're following along, don't forget to open the
00:52DiggingIntoDataGrid solution.
00:55Double-click on BasicGrid. xaml and now go to the Data tab.
00:59This is one of the tools I was mentioning in Blend.
01:01If you go to the Data tab and come over here and click on this button, the Add
01:05live data source button, and then choose Define New Object Source,
01:10you are shown a list of the potential data sources.
01:12I want to use my Persons class so I will come down here, and select it and then click OK.
01:18Now note that I have a PersonsDataSource and I have a Persons (Array) underneath that.
01:24If I take the Persons (Array) and drag it over to my artboard, you will see that
01:27it says Create a ListBox and bind its ItemSource property to PersonsDataSource.
01:32If I let go, you will see that I do get a ListBox that's bound.
01:35It doesn't look quite right though because FirstName, Gender, LastName
01:39are all mixed together.
01:40The solution to this is to create something called a data template, which is the
01:44topic of another movie.
01:45I will delete the ListBox.
01:46I will then go to the tool panel and add a DataGrid.
01:50Move over here, come down and click on the Assets button, which allows me to
01:55find any element that's available.
01:59I type in datag, which brings up my DataGrid in the filtered list. I choose it,
02:06and then I can select it and draw the data grid in my artboard.
02:10Now I can take that Persons (Array) and drag it over, and I get a
02:15slightly different tooltip.
02:16It now says Bind DataGrid ItemSources to PersonsDataSource, and
02:20that's precisely what I want.
02:22I let it go with the mouse button, and instantly the DataGrid is populated with data.
02:27Now, all I need to do is make it look nicer.
02:28I will go to the Properties pane.
02:31You need to scroll down till you see the Common Properties area, make sure
02:35that's open, and then change the HeadersVisibility to All.
02:39Keep you eye on the Data Grid now while I do this, there.
02:43What happened is that it added a row header, which makes it easy for your users to select the row.
02:47I would also like to turn on the Gridlines by selecting All.
02:52This is subtle so keep your eye on the grid.
02:54And then finally I am not happy with this FirstName.
02:57It should have a space in it.
02:59I will go over to the panel, choose Columns (Collections), click the button
03:03with the three dots on it, select the first column, and then I am going to do two things.
03:09I am going to make it bold by clicking the Bold button, and then I am going
03:13to go up to this header and I am going to type a space right here, and then click OK.
03:20The result, all of the data in that column is now bold, and the header now looks better.
03:26The grid is very sophisticated.
03:28It would take me several hours to go through all the details of what you can
03:30do with the Data Grid.
03:31So, what I thought I would do from my last demo is show you how you can
03:34group data in a grid.
03:35To do that you need to go look at the GroupingData.xaml file.
03:39I will also run the application so you can see what this looks like.
03:44Before I do that though, I must switch my App.xaml start. Open up App.xaml,
03:49open up App.xaml.cs, scroll down and make sure you uncomment the line where we
03:54set the GroupingData as the Startup object and comment out this other line
03:58where we had the basic grid.
03:59Then press F5, and here is our grid now with grouping available.
04:07I can click on Gender, and now it groups by Gender.
04:11Male, Female -- Oh!
04:13And I see we have a typo.
04:14Linus somehow has a typo in his gender so he shows up in a separate group.
04:21Go back to ScienceGroup. Now they are grouped by Chemist, Biologist and so on.
04:27This is all done in my code behind for grouping data.
04:35I have a little bit of code down here.
04:37This code fires whenever you choose something new in the combo box.
04:40This code runs the first time the page loads.
04:43What I am doing is I am getting the underlying data source for the DataGrid
04:47with this here.
04:48Then I am getting the PagedCollectionView, which allows me to split the view
04:52of my data into pages, and then I store that here and then I say I have a group description.
04:59Look for the ScienceGroup in the underlying data.
05:01Make that the criteria for the group.
05:04Then I no longer bind that personGrid directly to the underlying data source.
05:08I bind it to this paged view instead.
05:11And the DataGrid knows what to do with that.
05:13It splits it into multiple pages, in this case separate groups.
05:17All my combo box code does down at the bottom
05:19is swap out of the GroupDescription based on Gender or ScienceGroup, what
05:24it finds in the combo box.
05:25So, it goes to the combo box, gets the selected item in the combo box, which
05:28should say something like Gender, and then that sets the
05:31PropertyGroupDescription over here.
05:34What more can I say?
05:36The DataGrid is very powerful and well worth your time learning how to use it.
Collapse this transcript
Using DataBinding to work with data
00:00One of the most satisfying areas in this Silverlight API is the binding framework.
00:04This is a huge topic, one that deserves its own title.
00:08So, in this movie, I will concentrate on showing you a few simple data binding tricks.
00:12In this movie, I am going to be using both Visual Studio and Expression Blend.
00:15I am going to start in Expression Blend.
00:17If you're following along with the Exercise Files, you are going to want to
00:19open the DataBindingWithData solution and then double-click on the
00:23SetupBinding.xaml file.
00:26I have three controls here that I am going to use.
00:28I have a check box, a text box and a text box.
00:31What data binding does is it takes and synchronizes the data in a source and
00:37pushes it into the target.
00:39Silverlight supports something called element to element binding.
00:41So, what I am going to do is I am going to take the Text property of this
00:44text box, I am going to bind it to the Content property of this check box.
00:48To do that, I select the check box, move to my Property panel, type in content
00:56and here is the Content property.
00:57Normally, I can type a text in here, like Hello, and you will see that it shows
01:03up as the Content property of the checkbox.
01:06Well, what I want to do is do a binding.
01:08To do my binding, I am going to click on this little white button.
01:11Yes. This is a button.
01:12Some of my friends on the Expression Blend team are calling this as the Property
01:14Peg now for setting properties.
01:17You click on it once, and then we are going to go down to this yellow section
01:20called Data Binding.
01:21Remember, that color yellow is going to be important in just a second.
01:25Click on Data Binding and what I am doing is an element to element binding.
01:29So, I am going to click on this middle tab, and then I am going to choose the
01:32source, which is the textBox, and then I am going to choose its property, which
01:38is the Text property. There it is.
01:40It tells me the data type of the Text property is String and then down here
01:44it shows that I am binding to the Text property again.
01:46That's redundant information at this moment.
01:49Click OK. I told you to keep an eye on that color yellow. Do you see it?
01:54In the Property panel, the peg is now yellow, which signifies that it is
01:58data bound, and it also reinforces that by putting a yellow border around this.
02:01That's to tell you that this data is no longer coming directly from the element.
02:04It's coming from a bound element.
02:07If I run the application and type some new text in here, it works like you expect.
02:16For my next binding, I am going to do something a little different.
02:18I am going to bind the Is.Checked property of the check box to the string
02:24that's in this text box.
02:26Now I have to do some extra work, because the binding framework doesn't know
02:29what to do with a String to a Boolean value.
02:32So, all I have to do is run this through a value converter, and when the binding runs,
02:36it will run my code in the value converter.
02:39It will then tell it what to do with this string and turn it into a Boolean.
02:42I am going to use Visual Studio for this editing because Visual Studio has much
02:46more powerful tools than Expression Blend does for writing code.
02:48So let's switch over to Visual Studio, and I am going to open this file called
02:52TextToBooleanConverter.
02:55It's going to take string in, text.
02:57It's going to give me back a True or False.
02:58So, I will come over here and I am going to say I am going to implement
03:01the IValueConverter interface, and here is the part I want you to see in Visual Studio.
03:06There is a little blue tip below the I in IValueConverter.
03:11If I hover my mouse over it, a little combo box pops up.
03:15I can select that with my mouse, and then choose Implement
03:17interface 'IValueConverter'. Wow!
03:20Look at that.
03:22It wrote my Convert method for me and it wrote my ConvertBack and it placed them
03:26all in a region tag, so I can collapse this and not have to look at it later
03:30when I am working on other parts of my code.
03:33For my example, I am only going to implement the Convert method.
03:35So, I am going to come in here, and I need to return a Boolean.
03:39I'll start by declaring a string variable.
03:48I'm going to get the incoming value passed to me here.
03:51I am going to take that and convert it to a string and store it in a string variable.
03:56Then I am going to say, return, I need to return a Boolean, candidate.Length > 0.
04:08If the string has characters in it, then return True.
04:11If the string has no characters in it, return False. We will save this.
04:15I will switch back to Expression Blend, verify that my
04:19TextToBooleanConverter was changed.
04:22I will compile my project by choosing Project > Build, or Ctrl+Shift+B.
04:26Now I am going to do my binding.
04:29I will switch back to my XAML file, choose my checkbox, and go through the steps
04:33I went through before.
04:34I will find the IsChecked property, click on the Property Peg, choose Data
04:40Binding, make it an element to element binding, click the textBox.Copy.
04:46It's not going to find the Text property when you look in the list, because
04:49it doesn't think that it's bindable.
04:50I am trying to bind to a Boolean, so it won't show it to me.
04:53So I will come down here, and choose Show > All Properties.
04:59Now you can see the Text property.
05:01I will select Text and now I need to bring the converter into the mix.
05:04I will click on the Show advanced properties, and then come down to where it
05:08says Value converter, and choose my new TextToBooleanConverter.
05:14Click OK and let's test out our binding. Press F5.
05:21While there is text in the text box, I have a check in the check box, when there is no
05:25text in there, there is no checkmark.
05:27I have one more example I would like to show. Probably a little more practical.
05:32That's here in the BindingToolTip.
05:35I have a CalendarDays class, which has a DayName and an IsWeekend Boolean value.
05:41And then I filled this CalendarDays class, which is a list of calendar days, with data.
05:45Monday is not a weekend, and so on.
05:48In this BindingToolTip, I have a list box and this is your control and it is
05:52bound to the tooltip.
05:55Now tooltip is the little floater that pops up when you hover over something.
05:59I have bound the tooltip to the list box.
06:03And then the key thing about this, I am searching through my XAML to take a look,
06:06is in my list box I say the tooltip is bound to the SelectedItem.DayName.
06:12What this means is when I start up the app, let's be sure and change your Startup.
06:16Go to App.xaml.cs and uncomment this line for the BindingToolTip and then press F5 to run.
06:30And I select an item and then I hover my mouse over the list box that shows Friday.
06:34I will select Sunday now and then hover over the list box and it says Sunday.
06:40I really love the data binding in Silverlight.
06:42I can't stress enough that you should learn more about how it works.
Collapse this transcript
Floating UI with Popup and ToolTip
00:00The tooltip showed up in user interfaces about 20 years ago and continues to be
00:03a useful addition to your UI.
00:05I guess the most common use for the tooltip is for informational bubbles.
00:10For example, the user hovers over a button and the tooltip provides additional
00:13context regarding what the button will do.
00:15I have switched to Visual Studio.
00:16If you're following along with the Exercise Files, you are going to want to open
00:19the FloatingUI solution.
00:21This solution contains the PixelSmith application, which we have been working on
00:24throughout this course, and then double-click on the MainPage.xaml.
00:30Then you should double-click on MainPage. xaml.cs, because I want to show you something.
00:34Since the last time we saw PixelSmith, I have added a little bit of code that
00:38actually adds some ellipses and rectangles to the work area.
00:41It's some simple code.
00:42I have an AddEllipse_Click event that creates an ellipse, sets a few
00:47properties, positions it on the canvas, and then wires up an event procedure,
00:52the MouseLeftButtonUp.
00:54I also do the same thing for rectangles.
00:57Let's see what it looks like.
00:58I will press F5 to run the application.
01:03And then I will click the Add Ellipse button.
01:06Oh! Look at that.
01:07Some new orange ellipses.
01:09I click on the Add Rectangle, and I see some yellow rectangles.
01:14There also should be a tooltip. If you hover your mouse over the Rectangle button,
01:18you will see a simple text tooltip show up.
01:20I am going to show you how to make that look better in a minute.
01:23But I would also like to point out this thing called Show Popup.
01:25There is a related element that looks a lot like a tooltip called the popup.
01:30What's the difference between the two?
01:32The tooltip is launched automatically by the system when you hover over a control.
01:36That tooltip only stays on the screen for a limited amount of time, then it fades away.
01:40The user cannot interact with the tooltip in any way.
01:42The popup on the other hand must be launched by you.
01:45Once launched, it can remain on the screen or automatically fade away.
01:49And another key difference is that the user can interact with the content on the popup.
01:53I can click here and there is a button, and I can click on this button to close the popup.
01:58The primary usage of popups is for little workpad areas inside your
02:01Silverlight application.
02:03Let's close that one and then close this, and let's show you how to make that
02:07tooltip look a lot nicer.
02:08I am going to switch to my MainPage.
02:10I am going to do a Ctrl+F, and then look for tooltip and then dismiss this dialog.
02:19But here is my button.
02:21Here is the ToolTipService.
02:22This is an attached property, so that I can attach a tooltip to any element and
02:28here is the neat part about tooltips.
02:31Inside the beginning and ending ToolTip tags, I can put any UI I want,
02:35any valid Silverlight UI.
02:37For my example, I am going to put a StackPanel in here and I will cut the end
02:42StackPanel and paste it down here, on line 65.
02:48I am going to change the orientation of the StackPanel to horizontal and I am
02:54going to add an image.
02:55Now I already have an image in this project.
02:58We haven't talked about how to add images to projects yet.
03:00We will cover that topic in the Images and Media chapter.
03:03For right now, I am just going to add an image and set its Source property,
03:06which tells it which picture to load, and I am going to use this one over
03:10here called burst.png.
03:12Burst.png is a file I have already added to the project.
03:16And then I will close my Image tag and I am going to press F5.
03:24Are you ready to see this awesome tooltip?
03:27Okay, so it could use a little more work.
03:28But imagine if you had a couple of hours to work on this.
03:31Any valid Silverlight UI can be put into that tooltip.
03:33I need to talk briefly about the popup.
03:35There is the popup. H ow do you turn on a popup?
03:38I have two button click procedures that do this.
03:41Let's look at the C# code.
03:44On one button click, I say propertyPopup, which is the name of my popup, .IsOpen = True.
03:50And on the other button, the one that was on the popup itself, I say
03:55propertyPopup.IsOpen = False, and that's all you need to do.
03:58Remember you can use any UI in there.
04:00So, you could use it as a little toolbox, whatever projects come to mind.
04:03Before you get too carried away with the popup control, check out another movie
04:06in the course, which shows the child window.
04:08It's a much more powerful version of the popup.
Collapse this transcript
8. Navigation
Swapping content with UserControls
00:00In most of the movies in this course, I have been using a single page
00:03application, but in the real- world that's not often the case.
00:06What we need is a way to dynamically swap out content at runtime.
00:10I am going to show you two demos in this movie.
00:12If you're following along with your Exercise Files, you are going to want
00:14to open the Navigation solution, then open the SwappingContent and then
00:19open MainPage.xaml.
00:23The Silverlight application in this project should look familiar to you.
00:26It's PixelSmith, which we've been building throughout this course.
00:29I am going to run the application and show you what we are going to do in this
00:32part of the example.
00:34What I want to have happen is when we add an ellipse, I want an ellipse specific
00:38toolbar to show up in this area.
00:41And when I add a rectangle, I want a rectangle specific toolbar to show up here.
00:45I have already written the toolbars. They are in the UserControl folder. One is
00:51called EllipseUserControl and one is called RectangleUserControl and they
00:56consist of a few buttons and a little bit of code.
00:58Let me show you an example.
01:00What my code does is when you create an instance of the EllipseUserControl
01:06I pass in a workEllipse and I store it in a member variable and then when you
01:11click on the gradientButton, I create a new GradientBrush and assign it to the
01:15Ellipse that was passed in.
01:16Now let's switch back to MainPage.
01:19In MainPage, I have added a new border called borderToolBars.
01:30This is the place where I am going to put the two user controls.
01:32I'll switch to my code behind page and I am going to scroll down here. I have a
01:41couple of methods down near the bottom called rectangle_MouseLeftButtonUp and
01:45the ellipse_MouseLeftButtonUp.
01:49This code is going to fire when you click on the ellipse in the work area or the
01:53rectangle in the work area.
01:55What I am going to do here is write one line of code. borderToolBars.Child = new
02:02RectangleUserControl.
02:05This is my UserControl I am going to put into that toolbar area and then I am going to pass in
02:10this item here, which is the sender.
02:12That will be the rectangle I just clicked on in the work area.
02:14So, I am going to say sender as Rectangle. I am doing the as Rectangle because
02:20my code in the UserControl is expecting it to be of type rectangle.
02:26I will copy this code and I'll paste it down here. Paste.
02:31And then I am going to change this slightly, and I am going to say this is going
02:34to be EllipseUserControl and this is going to be as Ellipse.
02:42I needed to add the code one other place.
02:45Up here in this function called AddShapeToWorkArea, I have got a little if
02:49statement in here that says, if the shape is an ellipse then you are going to
02:52create an ellipse and make it orange and wire up some event handling.
02:56And right here I also need to do the code I just wrote here and here and
03:02this should be changed to Ellipse and this should be changed to an Ellipse.
03:08Now this should be shape as Ellipse.
03:16Shape is the local variable that contains my shape.
03:19Now let's run the app.
03:24I click on the Add Ellipse, I get a new ellipse. I get the ellipse
03:27specific toolbar up here.
03:29I can drag this around. I can click on the Gradient to make it a gradient color
03:33and click on the Bigger button to make it bigger.
03:35I click on the Add Rectangle.
03:36Keep your eye on the toolbar.
03:39It changes to the Rectangle toolbar. I can then make this wider or change its color.
03:45So, what I have shown you in this part of the demo is I can dynamically change
03:48our part of the UI at runtime.
03:53There is another problem I need to solve occasionally, which is I want to
03:57change out the entire user interface inside the Silverlight area, instead of just a subset.
04:01I have a demo of that up here in this project called ChangingRootVisual.
04:04I am going to right-click and make this my Startup project, so when I run
04:09this the next time, it will use this project and I am going to go look on my App.xaml.
04:16In most of the application we have written so far in this course, what has
04:19happened inside the Startup event is that I have been assigning a Silverlight
04:24UserControl to the RootVisual.
04:27In Silverlight, once you have assigned a UserControl to RootVisual you are not
04:30allowed to change it at any future point.
04:33There's a little trick you can use that to get around this.
04:36Instead of storing a UserControl for the RootVisual, use any container control.
04:41In this case I have got a border.
04:42So, I have created a border, I set the BorderThickness and then I assigned the
04:46border to the RootVisual and then I said go to the border and make its child
04:50new MainPage, which is this page right here, the MainPage.
04:54So when I run the application it looks like your normal Silverlight app, but now
05:00I can click on this button and I should be able to switch over to another page.
05:04Swap out the entire UI with a new look. I just need to write a couple lines of code.
05:10The key to making this work is that I wrote this little method down here called
05:13ChangePage that I can pass in any UserControl to this ChangePage and it will
05:17automatically swap out the root.
05:22On the second page, I am going to type in App.ChangePage, new MainPage and then
05:32I am going to go to MainPage and then in its Button_Click procedure I am going
05:37to say App.ChangePage(new SecondPage(). All right, ready to go.
05:53There is the main page.
05:54I click Go to Other Page.
05:56It swaps out the entire UI with the new page. I click on go back to Main and it
06:01goes back to the previous page.
06:02These are just two techniques for dynamically changing your content at runtime.
Collapse this transcript
Creating dialogs and child windows
00:00Silverlight 3 added a couple new project types and a few new class types too.
00:04I would like to spend this movie looking at the ChildWindow class, which
00:07simulates a modal window within your Silverlight application.
00:10If you're following along with the Exercise Files, you are going to want to open
00:13the DialogsInChildWindow solution.
00:15Then we're going to add a brand new child window.
00:18I'll do that by right clicking on the DialogsInChildWindows project,
00:23selecting Add > New Item and then inside the Visual C# Templates choose
00:30Silverlight Child Window.
00:31I am going to name mine Demo and then press Enter.
00:37Here is my new child window.
00:38If you look up at the root element, you will see that it is
00:40controls:ChildWindow. That's my class.
00:44I want to show this and I have a button that I am going to use to show this.
00:47It's inside by EllipseUserControl class. Open up the UserControls folder,
00:53double-click on the EllipseUserControl. xaml file and then scroll down until you
00:57find the printButton.
00:59I am going to type in click, press the Tab key, select the New Event Handler and
01:07then I am going to press F7 to switch over to my Code view.
01:12There is my printButton event procedure.
01:15What do you think I am going to do in here?
01:17I am going to write a little bit of code to launch that brand new child
01:19window that I created.
01:25Here I am creating a new variable. I type Demo, storing it in this variable
01:30called dialog and then I am going to call dialog.Show.
01:36Show is a method that shows the new window.
01:39I think we are ready to go. Press F5.
01:45You need to add an ellipse, so that the Ellipse toolbar shows up.
01:49Then click on the Print button and this is what a child window looks like out of the box.
01:54It has an OK and a Cancel button.
01:56It has a nice little animation when it shows up and disappears, watch the
01:59animation when I click OK.
02:02Watch the animation when it comes in.
02:03You can also click on the X button, to have it go away.
02:08I need out some user interface on there and rather than writing that during the movie,
02:12I have already written one called the PrintWindow class.
02:15So, we'll close the browser.
02:17You already have this PrintWindow, which is a child window, so I'll just need to
02:21modify my code slightly.
02:23I'll move to my print event and type in var dialog = new PrintWindow and then
02:30I am going to show the window like before.
02:31I am going to do one other thing before I show the window though.
02:34If I want to get the print results back from the print window, I need to set up
02:38an asynchronous call back.
02:40In Silverlight when you call Show it's run on another thread.
02:44What I need to do now is moved to this line and say dialog.Closed, which is an
02:51event, +=, tab, tab.
02:56What will happen now is the dialog will show, Silverlight will go back to
03:00showing the main UI. When I close the dialog, it will fire an event that runs
03:05this dialog_Closed method.
03:07And what I will do in here is create an instance of my dialog window var
03:15dialog = the sender.
03:18The sender is the PrintWindow, so I am going to do a cast. PrintWindow and then
03:25put sender over here.
03:36I have declared a variable here called result, which gets the result coming
03:40back from the dialog.
03:41That's whether the user pressed the OK or the Cancel button, and then I will write
03:44an If statement here, if, tab, tab. If you press Tab twice it uses what's called a C# code snippet.
03:52Then I will say if result==true, and I just need to write one more line of code.
04:01resultsTextBox.Text = string.Format, Printer, Copies.
04:22That will be one, and these are the little tokens that get replaced at runtime.
04:26After this double quote, I say here is the data that goes where
04:30where the token is.
04:31So, that will be dialog. PrinterName, and dialog.NumberOfCopies.
04:42These are properties that I created on my dialog.
04:44If you go to my PrintWindow and look at the code behind, I created a property
04:52down here called NumberOfCopies and a property called PrinterName.
04:55I think we are ready to go. Press F5.
05:01Add an ellipse.
05:03Click on the Print button.
05:05This is my custom window.
05:07I choose this printer right here and I want five copies.
05:11When I click OK, I was able to return those results back and put them in this text box.
05:18If I click on the Cancel, it doesn't affect it because it knows I didn't
05:22click the OK button.
05:23We run it one more time, we'll change this to 13, and then OK.
05:27Oh, I see I forgot to select an item in the list box.
05:30I am going to need to shut the application down and restart.
05:35I will show you what happened.
05:38Click the Ellipse, click the Print button. I didn't have anything selected in
05:42this first list box.
05:43That's why it errored out on me.
05:46Here is the last step.
05:46It brings a copy value in and the printer that I selected in the list box.
05:51Child windows make it even easier for you to make an application that behaves
05:55like a desktop application.
Collapse this transcript
Using the Navigation Framework
00:00One of the problems with rich application frameworks like Flash or Silverlight
00:03is that even though they live in a webpage, they are not HTML pages.
00:07The downside of this fact is that if you create a Silverlight application with
00:11many pages, there isn't an easy way to remember your location in the URL.
00:15This makes it hard to link to content within your application.
00:18Silverlight 3 introduces a new navigation framework which solves some of these issues.
00:23Within Silverlight 3, there is a navigation framework.
00:25In order to add it to your application you must add a reference to the
00:28Windows.controls.navigation DLL.
00:31There are two key parts of the navigation framework, the Frame and the Page.
00:35The Frame class is very similar to a border or other content control.
00:40The main difference is that it understands the browser and supports forward and
00:43backwards navigation.
00:45It also allows you to use a URI to load any page in your app.
00:49To work with the frame, there is a special class.
00:51You can use the user control with the frame but you're more likely to use a page
00:55because it also has access to the navigation system, and can request access to
01:00the browser history.
01:02Not only that but it can accept parameters from the URI, which it can then
01:05use within the page.
01:07The navigation framework is large. There are other interesting classes including a
01:10Mapping class, which makes it easier to map a simple link to a more complex
01:15navigation, and the Journal, which is used to access the forward and backwards history.
01:20To demonstrate this, I am going to switch to Visual Studio.
01:23If you're following along with the Exercise Files, you are going to want to open
01:25the UsingNavigationFramework solution and then choose 'Starting from Scratch'
01:29and then double-click on MainPage.xaml.
01:35This page contains a frame.
01:37Remember what I said in the slides, that the frame understands a navigation system.
01:42This is going to be the location for all of my content.
01:45I've named this mainFrame.
01:47I am going to press F5 to run the application.
01:50You notice that I have an empty frame.
01:51That's because I have not loaded any content into this frame yet.
01:54Let's return back to Visual Studio and see how I am going to load the content.
01:58I am going to double-click on MainPage.xaml.cs.
02:04On line 24, I am using some standard procedures I have used in other movies.
02:08I am creating an instance of this UserControl and I'm assigning it to the
02:12content of the mainFrame.
02:13This is not using the navigation portion of it.
02:16I'm just making this a child of the frame.
02:18If I want to use the navigation part of the framework then I need to use the
02:22Navigate method instead, like I am doing in this button click down here.
02:26So I say mainFrame.Navigate.
02:28It's a little more complex because I have to use a URI. I specify a
02:32BlueControl.xaml, but let's look at the difference when I use the Navigate method.
02:38Press F5 again to run the app. Note that the Back button and the Forward
02:42button are not lit up.
02:43I am going to click Blue (Loaded as Content).
02:46That was the first code I showed you.
02:49I have made this user control a child of this frame.
02:53Notice still that the backwards and the forward buttons are not lit up, but if I
02:57click on this button using the Navigate method, the same user control is loaded.
03:02It looks exactly the same except for the timestamp, but now I have access to the
03:08browser history and I can click on StartingFromScratch, which will take me
03:10back to the previous page.
03:13In my Travel application, this frame will host multiple elements.
03:15To gain access to the navigation system, I'll make these pages instead of user controls.
03:20I'll show you how that's done.
03:21I am going to close the browser, and return back to Visual Studio.
03:25The way that you add a page to your application is you right-click in your
03:28application, choose Add New Item and then you choose Silverlight Page
03:34instead of Silverlight User Control.
03:35I already have several pages added.
03:39The first thing I am going to do is I am going to go to my main page, and I am
03:42going to find my frame and then I'm going to tell it load a default page.
03:47I'll do that with the Source property.
03:52Next I am going to go to the Bungelow page.
03:55It has two buttons on it, so we are going to double-click on the
03:58Bungelows.xaml.cs file and I am going to write code in these two button click procedures.
04:03I am going to gain access to the NavigationService, I am going to call the
04:08Navigate method, I am going to create what's called a new URI, and the name of
04:16this URI is going to be Pages/Skiing.xaml.
04:32It will have a relative path.
04:34I am going to copy this line of code down to the Home button, Ctrl+V, and then
04:40I'll change this to say Home and I think I am ready to test. Press F5.
04:50It loads my Home Page.
04:52The URI is different.
04:54I can now drill in and then save these URLs in my browser history or I can
04:58save them as Favorites.
05:00Note that at the end of the line says #/Pages/Home.xaml.
05:05That's passed into the navigation framework and it knows that needs to bring
05:08up the home.xaml page.
05:10When I click on Beach Vacations, it takes me to the Beach Bungelow Page.
05:15The navigation history is working.
05:18I can say I'd rather go Skiing, which takes me to the Skiing page. Again
05:22it has a smart URL up here. It says skiing.xaml.
05:26I can click on I'd rather go back to the Beach or I'd rather go home.
05:30So I am doing the navigation in my application, with each navigation having a nice
05:34unique URI, plus I'm getting the history that the user expects in many
05:39browser-based applications.
05:42There are other things that you can do with the navigation system, one of these
05:45is called URI mapping.
05:46Let me show you this.
05:48On MainPage.xaml, you can add a mapping somewhere in your application, like this.
05:54What this says is the simpler URI, /Home, really maps to Pages/Home.xaml.
06:02Now when the user requests this shorter URI, it will get mapped to this one.
06:06This is a lot of details to remember.
06:09Let me show you the really easy way to do this.
06:12Right-click on Solution, choose Add > New Project and pick the Silverlight
06:18Navigation Application.
06:26I'll just call this number 2.
06:28It looks like I have already created that one once before.
06:34Then I'll right-click here and set that as my Startup project and press F5.
06:39You'll want to look through this in more detail but what you are going to see
06:42here is Microsoft has already written the URI mapping.
06:45It's already written the framework.
06:46It's already written the frame and you can click on these buttons up here to
06:50navigate back and forth.
06:52On the Expression Blend website they include about 20 different templates to
06:55make this navigation frame to look better.
06:58All in all I'd say that the navigation framework is well worth your time investigating.
Collapse this transcript
9. Images and Media
Adding binary resource files to a Silverlight project
00:00Most applications have files that are affiliated with the program, but are not
00:03part of the runtime.
00:04These could be image files, sample documents, or even PDF files.
00:09The key is that these are binary files that have to be shipped with your application.
00:13In Silverlight, it's trivial to embed these files in your assembly.
00:17It's incredibly easy to extract these resources.
00:20In fact, some controls like the image control can extract the resource with just
00:24a URI location, no code necessary.
00:27For today's demo, I thought I would use Visual Studio.
00:29If you're following along with the exercise files, you're going to want to open
00:32the AddingBinaryResources solution.
00:35I'm going to add an image to the Images folder.
00:37To do that, I'm going to right-click on the Images folder. Choose Add > Existing Item.
00:43In your Exercise Files folder is an Assets folder that contains some pictures.
00:49I'm going to choose pslogo.png and then click Add.
00:53Now let's see how I can embed this in our DLL.
00:56Choose the PNG file and look at the Build Action.
01:00It's already set at Resource, which means it will be embedded.
01:02There are a number of choices available in this dropdown.
01:04For instance, here is the Compile option.
01:07This is used when you have source code.
01:09Let's compile our application and then I think I'll go and show you how that
01:15file is embedded inside the assembly.
01:17To do that, I'm going to use a .NET utility called Reflector.
01:20This is a free utility that's available on the Internet.
01:22I've already installed it on my machine, so I'm going to go down to my Start menu,
01:27type in reflector, and then click on the exe file.
01:31If you haven't run the installer you will not have this on your computer.
01:36Reflector is a disassembler.
01:38You add a DLL, I've already added mine here, but it's easy to add another one.
01:43Choose File > Open, and then find a .NET file, then I'll click Open.
01:50I can then disassemble this DLL by drilling down into the Resources folder.
01:56Once I selected the Resources section, I can press the Spacebar to disassemble.
02:01The disassembler window appears on the right edge of the screen and this shows
02:05all of the files that are part of my assembly.
02:08Here's the one we're looking for, images/pslogo.png.
02:12You can also see that our XAML files are also embedded as binary resources.
02:16I'll close Reflector and return back to Visual Studio.
02:21Now that I have this image there, at runtime I might need to extract those bytes.
02:26To do that, I'm going to go to my MainPage.xaml.cs file, and I'm going to scroll
02:32down to the AddImageElementButton_Click event procedure.
02:37I've already written some of the code.
02:39Let's see what it's doing.
02:40I'm going to the Silverlight application and I'm saying, I know you have some
02:45resources embedded in the DLL.
02:47I would like to get one of those resources and I would like to get it as a stream.
02:52A stream in .NET is just byte information.
02:54Here is the unique URI that identifies the item that I want.
02:58The important part for us is images/pslogo.png.
03:03Once I get the resource stream, I'm going to use a reader to extract the data
03:09and then I'm going to put it into a byte array.
03:11So, I'll create a byte array named imageBytes, readBytes.
03:22Then I need to tell on how many bytes I want. We'll take 12, thank you.
03:27I'm going to place a breakpoint on this line of code by pressing F9, and then
03:32I'm going to press F5 to run the application.
03:35Then I need to click on this Images section, click on the Image Element button,
03:40and I hit my breakpoint.
03:41I'm going to press F10 to run this one line of code and then I'm going to hover
03:46over the byte array and you can see that it does have byte data inside of it.
03:50Let's stop the application and talk about a few more things.
03:54There is another way to include this image file.
03:57Instead of embedding it in the DLL, I can go to the file and choose Content.
04:05Now what happens is it's put inside the XAP file instead of the DLL.
04:10That's the only difference.
04:11The benefit for this is that your administrators can swap out the resources in
04:15the zip file without you having to recompile your application.
04:19Then you need to use this syntax to extract the information.
04:23In this demo, I used a byte array to retrieve the information, which is a very
04:26low-level way of doing business.
04:28As I said before, some of the controls make it even easier to extract the resource.
04:32For example, just watch the Static Images with Image Controls video also in this series.
Collapse this transcript
Showing static images with the Image Control
00:00Showing an image in a Silverlight application couldn't be easier.
00:03That's because the Image control does all the work.
00:05The only tricky part is understanding how the Image control sizes your content.
00:08Oh, and there is one other limitation. You can only use PNG or JPEG files.
00:13That's right, no JIFFs.
00:15For today's demo I'm using Expression Blend.
00:18If you're following along with your exercise files, you're going to want to open
00:20the ShowingStaticImages.sln.
00:22I need to add a couple of pictures to this application.
00:25So, I'm going to right-click on the Images folder, choose Add Existing Item and
00:31then I'm going to go to the Exercise Files folder and then to the Assets folder
00:35and choose the Camera and the pslogo.png file.
00:39Then I'll click Open.
00:41Then I need to open my MainPage.xaml.
00:45I need to zoom out just a little bit.
00:49Probably, the easiest way to add an image is just to grab the image out of the
00:52Project Explorer and drag it into my application.
00:55That creates an image application and sets its source property to the picture
00:59file that I just dragged.
01:01If I want to have a little more control over where the item goes, I can do that too.
01:05Let's delete this camera.
01:06I'm going to add another row to my grid.
01:10In Expression Blend you can do that by moving along the edge of the grid until
01:14you get the orange triangle.
01:16I'm going to click right here to add another row.
01:19Then I'm going to come over to my tool panel and click the Assets button, come
01:24up here and type in ima, which filters down my list of elements to the Image and
01:30the MultiScaleImage and I'll click on Image.
01:33Then I'll choose Image in the tool panel and draw my image in my new row.
01:38Now that I have my image, I think I'll set my margins to 5 pixels each and
01:50lastly, I'll add my picture.
01:52We'll go to the Source property and choose pslogo.png. There you go.
01:57I want to talk about size for a minute.
02:01The image element has the Stretch mode. The Stretch mode has four values.
02:04It's used when the native image size and the container size are mismatched. Here's a list.
02:09There are four settings for the stretch enumeration.
02:11The first three shown in the slide leave the image with its original aspect ratio.
02:15So, that would be None, Uniform, and UniformToFill.
02:17Fill stretches your image to fill the entire contents of the container.
02:23None applies no sizing.
02:25In Uniform, the image is enlarged to fill the space, but does not extend
02:29beyond the boundaries.
02:30That means that it keeps its aspect ratio.
02:32On the slide you can see the picture was made wide enough to fit, but it wasn't
02:35stretched vertically.
02:37Then there is UniformToFill.
02:38That enlarges the image until it fills the entire container, but it keeps its aspect ratio.
02:43So, in that case, it's cutting off some segment of the picture.
02:46In Blend, I can change these values by choosing the Stretch enumeration. That's a Fill.
02:53That's a None. The picture is too big to see at this point.
02:57There's UniformToFill and the one that I had, which is best for me was Uniform.
03:02I've added one more feature to PixelSmith application.
03:06I've now made the Image button work.
03:07So, I should probably show you the code.
03:09I'll go to the code behind page.
03:11We're going to press F4 while I'm in Expression Blend, which will remove my
03:17tool panels, and then I'm going to scroll down to the
03:21AddImageElementButton_Click event procedure.
03:25What am I doing here?
03:26I'm declaring an image variable and a border variable, setting a few values on
03:31the border, including my picture, and saying get a new bitmap image, here is the
03:36URI define the image, assign it to the source of the image control, much better
03:41than working directly with the bytes, setting my Uniform Stretch value and then
03:46adding the picture to the border.
03:48So what happens when you run the application?
03:50Press F5 and let's see. It looks great.
03:53I've got my logo down here, and if I click on the Images and then Image Element,
03:58you can see that I'm adding the PixelSmith logo here in the work area.
04:04That's about all I have to say for the Image element.
04:06It's pretty simple element to understand.
Collapse this transcript
Painting UI with the Image Brush
00:00There are a number of interesting brushes in Silverlight.
00:02By now you should be comfortable with the Solid Color Brush and the two Gradient Brushes.
00:06There is another brush that I'd like to examine in this movie.
00:08It's called the Image Brush and it allows you to paint an image onto any UI
00:12element, not just the image control.
00:14For this demo, I'm using Expression Blend.
00:17If you're following along with the exercise files, you're going to want to
00:19open the PaintingUI.sln.
00:21Then double-click on the MainWindow.xaml file.
00:24I already have a TextBlock added to my application and I have the Curls.png file
00:30available as a resource in my application.
00:32The easiest way to make an Image Brush is to take a picture and drag it on to your artboard.
00:38When you do that, Expression Blend adds an image control and set its source
00:42property to the image, in this case, Curls.png.
00:45Next, I can go to the tools and choose Make Brush Resource > Make
00:50ImageBrush Resource.
00:52Expression Blend asked me for a name to identify my brush.
00:55I'm going to call it CurlBrush and I'm going to place that resource in the Window.
01:02You'll learn more about resources in another movie.
01:04I no longer need the Image element, so I will delete it.
01:08Press the Delete key.
01:10I told you that an Image Brush can paint onto any element.
01:13I will add an ellipse.
01:14I'm going to go my tool panel and I'm going to go to this area.
01:18If you do not see the ellipse, you need to click-and-hold and then choose Ellipse.
01:24Now I can come over and drag an ellipse in my artboard.
01:28When you want to set the Fill value for the ellipse, you go to the Brushes panel
01:33and then you select from one of these buttons. This is no brush.
01:36This is Solid Color Brush.
01:39This is a Gradient Brush, and so on.
01:41If you click on this last button, called Brush resources, you will see that
01:46there is a CurlBrush.
01:48I'll select that and now I'm painting that ellipse with my CurlBrush.
01:52I'll come over here and choose my Selection arrow.
01:55Then click on that TextBlock, and again, I'll go to Brushes > Brush
02:00resources > CurlBrush.
02:02This time I painted the background. Since that's not what I intended to do,
02:06I'm going to click No brush, then Foreground > Brush resources > CurlBrush.
02:12That image brush is a special kind of brush called a Tile Brush.
02:17What that means is that I can take the image and paint repeated versions of the
02:21image over and over again on my element.
02:23To modify my Tile Brush, I will click on this button, Tile brush, and then I
02:29need to pick a TileMode.
02:31Currently, I'm in None.
02:32What does that mean?
02:33Watch what happens when I come over here and click on my ellipse.
02:36Then I go choose this tool called Brush Transform.
02:38If you do not see this, you need to go to your tool panel and press-and-hold and
02:43then choose Brush Transform or just press the B key.
02:47Now when I move over to the image, I am modifying the tiling of this image.
02:52I'll grab this lower corner and slide it up and you see that the image
02:56is getting smaller.
02:58Next, I will go over to the TileMode and choose FlipX.
03:04You see that it's repeating the pattern over and over again.
03:07There are several settings in TileMode.
03:10There is one called Fill, which just repeats the image over and over again, and
03:13then FlipX, FlipY, and FlipXY, alternate the axes of the repeated image.
03:19My favorite is FlipXY.
03:21Again, I can come over here and move this around, change the size, do things
03:26like rotate the image.
03:31I could make it really tiny, which gives me a nice pattern on my element.
03:37What more is there for me to say?
03:38The Image Brush is a great way of painting subsets of your UI with
03:42whatever picture you want.
Collapse this transcript
Embedding video with the MediaElement
00:00Many of the first Silverlight applications were video players.
00:03That's because in the first release of Silverlight, video was the most powerful feature.
00:08Silverlight continues to make it easy to add video with the Media element.
00:11Currently, Silverlight supports Windows Media Video version 7 through 9 VC-1 and 8.264.
00:18You can also embed audio files including the ever popular MP3 format.
00:23For today's demo I am going to use Expression Blend.
00:25If you're following along with the Exercise Files you are going to want to open
00:28the ViewingVideo solution and then open the HelpWindow.xaml file.
00:35You might want to zoom out so you can see the entire window.
00:39Next I need a help video. To do that, I am going to right-click on my project and
00:44choose Add Existing Item.
00:46You should have an Assets folder inside your Exercise Files. Find the help.wmv file,
00:52click on it and then choose Open.
00:55Next I'll show you how easy it is to add the Media element. Take your movie file
01:00and drag it over to your artboard, position your video where you want, and then
01:06come over to the Property window and scroll down to the Media section.
01:10Here are some settings for the video. AutoPlay is currently enabled. You can
01:14also control the Balance, Volume, the starting position of the video, whether or not it's muted.
01:19Let's see what it looks like. This is my alter ego, the CEO of PixelSmith. Press F5.
01:29To run the help file, you need to click on the video help link.
01:33(Male speaker: Hello! My name is Arturo Diega and I'm the founder and CEO of PixelSmith Labs.)
01:38(You can call me crazy, but I know that you hold in your hands the most amazing graphics design tool written this century.)
01:44(I'm talking about PixelSmith 7.)
01:47(But don't take my word for it.)
01:48(Try it yourself and if you're not completely happy, I challenge you to write a better application.)
01:55Let's show you how to control that video.
01:56I am going to close the browser, return back to Expression Blend, then I am
02:01going to choose my Start button and before I move over to my code, I should
02:06probably unselect the AutoPlay on the MediaElement.
02:10The MediaElement has been auto named help_wmv.
02:12I am going to call this media1 and press Enter.
02:18Then I am going to click on the Start button, open the Event pane by clicking here.
02:22I already have some codes typed in so all I need to do is put my cursor in his
02:26text box and hit the Enter key and it'll take me to where code is in Expression Blend.
02:32Now all I need to do is compile the application. Whenever you rename things in
02:35Blend you always need to recompile. I can do a Ctrl+Shift+B, and then I'll
02:43call the Play method.
02:44Rest of the code is pretty simple.
02:48To do a stop, I'll choose media1.Stop and I suspect you can guess what the
02:57next line of code will be. media1.Pause. You can set up an event procedure
03:05called MediaFailed and then you can instruct Media element to call this when it
03:09can't find the media.
03:11This is handy for showing your user the message like the "server is
03:14unavailable, try again later."
03:16I haven't wired that up but this is what the function would look like.
03:19I have got two more things I need to do make this a nice-looking application.
03:23One is I need to ensure that the volume control is wired up to the Media control
03:28and at the bottom of the screen I have a locator.
03:31This is going to show where we are at the video.
03:33Unfortunately Silverlight doesn't provide any events to give us access to where
03:36we are, so we are going to use some timer code.
03:38Let me show you that.
03:42I have a timer called the DispatcherTimer. In Silverlight the DispatcherTimer is
03:46the thread-safe way to call code at timed intervals.
03:53The first thing I am going to do is set up the Interval value to a new TimeSpan.
04:04And I am going to check where we are in the video every 200 milliseconds.
04:09Next, I am going to start the timer and finally I have this callback, which is
04:19getting called every 200 milliseconds, so I need a wire that up. timer.Tick +=
04:25and it says press Tab to insert.
04:32It moves my cursor down into my Timer_ Tick event procedure and down here I am
04:36going to say location TextBlock.Text= media1.Position.ToString(), and close the
04:47line of code with a semicolon.
04:50Last thing I need to do is get the volume working.
04:51So, I'll return back Blend and then I am going to do a data binding.
04:55Click on MediaElement, move back to your Property panel, find the Volume, and
05:00click on the Property Peg, choose Data Binding. Click on Element Property.
05:07I am going to search for my numericUpDown control, which is inside the StackPanel.
05:12There it is and then I am going to set it to its Value property and then click OK.
05:18I am ready to run this again, press F5, click on the video help link.
05:25It no longer auto starts.
05:27It waits till I press the Start button.
05:29(Male speaker: Hello! My name is Arturo Diega and I'm?)
05:32I can click the Stop button.
05:34(Male speaker: Hello! My name is Arturo Diega and I'm the founder?)
05:36Pause button.
05:38(Male speaker: ..and CEO of PixelSmith Labs.)
05:40(You can call me crazy, but I know that you hold in your hands the most amazing graphics design tool written this?)
05:47I can change the Volume. You can also
05:49see that it shows my current position within video.
05:51I've got one more trick up in my sleeve.
05:53You see three rectangles?
05:54We are going to turn those into videos too.
05:57Return back to Expression Blend, click on the video and then go to Tools > Make
06:05Brush Resource > Make VideoBrush.
06:07I'll call this helpBrush, click on OK, select the rectangle and I am going to
06:15have to scroll up, click on this button right here Brush Resources, and then
06:19choose the helpBrush and do the same thing for the other two rectangles.
06:28Now I'm painting this video in three other areas on the screen and I can do some
06:32fun things like spin them around.
06:38If you hold the Shift key down while you're spinning, it constrains the angle.
06:44Now I want this one to be at this angle.
06:49And now let's see what this looks like. Press F5 to run the application.
06:57Start the video.
06:58(Male speaker: Hello! My name is Arturo Diega.)
07:00And now you have got four copies of Arturo telling you how great PixelSmith is.
07:07Adding video is nearly as easy as adding a static image. I'm sure you'll find
07:10many uses for the Media element.
Collapse this transcript
10. Energizing the UI with Animations
Understanding Silverlight animations
00:00If you programmed traditional animations you've probably work with the timer and
00:03wrote some event handling code to move items on the screen.
00:06I'm sure some of you may remember the days when animation was stateless,
00:09meaning your code had to remember where each sprite was during each step of the animation.
00:13In Silverlight this is no longer the case.
00:15In Silverlight most animations will be created in your Xaml or with a tool
00:19like Expression Blend.
00:20It's really not necessary to write a lot of code.
00:23Here is the key thing about Silverlight animations. They modify a dependency
00:26property to a new value over time interval. They are time based not frame rate based.
00:32What does that mean?
00:33You specify the initial value and the final value for their dependency property.
00:38You also specify the animation length. Silverlight calculates the frame rate
00:42based on these values and other factors.
00:44Since Silverlight applications are modifying dependency properties, you will
00:48know what the underlying .Net type is for that property.
00:51You need to select the correct animation type.
00:53There are only four to choose from.
00:55A Color animation changes from one color to another, for example the TextBlock's
00:59Background property.
01:01The Double animation changes a numeric value, FontSize for instance, and the
01:06Point animation is used to change a point value, an X and Y coordinate pair.
01:11Finally there is an unusual animation type called the Object animation class.
01:16Here you animate from one object to another object.
01:19Think of this as a way to replace one instance of one object with another at a
01:23specific time interval, here is an example of that.
01:26I have the background of a button and I want to apply a Solid Color Brush and
01:3110 seconds later I want to apply a Gradient Brush.
01:35You can further break down Silverlight animation into two categories, From/To
01:39animations and KeyFrame animations.
01:42A From/To Animation is the simplest to create and the easiest to understand.
01:46You pick your animation type, say the Double animation.
01:49You specify the beginning double value and the ending double value.
01:53You also specify the interval and you're done.
01:55Silverlight will interpolate between the beginning value and the ending
01:58value over the time span.
02:00Keyframe's are a little more complex. Here you specify specific time intervals,
02:05say 1 second, 1.5 seconds and 10 seconds.
02:10These are the keyframes and for each keyframe you'll also set a starting and
02:14ending double value.
02:16There are several types of keyframes. The Discrete KeyFrame changes instantly
02:19from one value to another. The Linear KeyFrame is similar to the To/From
02:23animation discussed earlier. The value is interpolated between the two values
02:27over the time interval.
02:28The Spline KeyFrame is based on a Bezier curve.
02:31This allows you to smoothly accelerate or decelerate the animations based on the curve.
02:36You rarely use an animation class by itself.
02:39It is usually grouped into a storyboard.
02:41The purpose of the storyboard is to control your animation time line.
02:44It might contain one or more animations, for example you could have the Color
02:48animation and a Double animation in the same storyboard.
02:51This is also where you control the animation.
02:53There are a number of useful methods available, like Begin, Stop, Pause and so on.
03:00For this demo, I am going to use Visual Studio.
03:02If you are following along with the example files, you are going to want to open
03:05the UnderstandingAnimation solution.
03:08We're not going to look at of code in this example. We are just going to run the application.
03:11Press F5.
03:14This application is using the Navigation framework so you are going want to
03:17click on this Four Animation Types button, and here you can see the Double
03:21Animation, which is going to animate the font size.
03:27You can see the Color Animation, which is going to animate one of the bands of the gradient.
03:33You can see the Point Animation, which is going animate two points on this shape.
03:40And lastly the Object Animation, which is going to change from a Solid Color
03:43Brush to a Radial Gradient Brush to a Linear Gradient Brush.
03:47If you're interested in learning more about this example you can always look
03:50through the Xaml in the application.
03:52There is also two samples here.
03:54This first one is an example of a more interesting animation.
04:01It's bringing in some blocks, rotating them, changing the colors and putting some text.
04:11And then lastly we have this example which is applying a perspective transform
04:16on a number of rectangles.
04:17That's kind of fun to watch.
04:19This is been an overview of animations. The rest of this chapter teaches you how
04:22to set up your own animations.
Collapse this transcript
Creating animations
00:00In this movie, I am going to show you how to create your own animations
00:02in Expression Blend.
00:05If you are following along with the exercise files, you're going to want to
00:06open the CreatingAnimation solution and then you are going to want to double-click
00:10on the MainPage.xaml.
00:12I am going to hold down the Spacebar and then use my mouse to move to the lower left.
00:18Next, I'll use the mouse wheel to zoom in a little bit.
00:22I am going to choose the Selection icon and then click on my image.
00:29Here is what I want to have happen.
00:31In the first two seconds of this application's lifetime, I want to animate in this logo.
00:36To do that I need a storyboard.
00:38Storyboards are available in Expression Blend on the Object and Timeline at the top.
00:42You click on the Plus symbol and then name your storyboard.
00:46I will call my storyboard LogoStoryboard. OK.
00:52Did you see the change in the UI?
00:54There were a couple of changes.
00:56The first is there's this red border around the artboard.
00:59This is very important.
01:00While that red border is visible, you are recording every action in the artboard.
01:06If you want to stop recording you click the red button.
01:09To re-enable a recording, click on the gray button.
01:12The other change in the UI was over here in the storyboard.
01:16There is now a yellow line on the timeline.
01:19This timeline represents the keyframes I am going to make.
01:22So, far, I can only see 0, 1, and 2 seconds.
01:25I can show more seconds by pressing F6, which will change to another view.
01:31Pressing F6 is the same as going to Window > Workspaces > Animation.
01:35The animation workspace has a longer timeline.
01:38See I have 8 seconds to work with.
01:41I will turn off recording, so I can set up my logo the way I want.
01:45I am going to take this little white dot in the center of the image and I am
01:49going to move it to the lower left-hand corner.
01:52Anytime you rotate an object, it rotates around this point, which is usually in the center.
01:57I'll zoom out a little bit, and then I am going to grab this corner of the image
02:03and I am going to rotate the item down below the edge of the UserControl.
02:08Now I am ready to record.
02:09Press the Record button, come over to the timeline and click the Record Keyframe button.
02:15Since my yellow marker was at that the 0 second mark, it created the keyframe
02:19for the image at 0 seconds.
02:22Now I am going to go to 1 second and click and press the Record Keyframe button again.
02:28Now I move the UI to where I want it to be at the end of 1 second animation.
02:32So, I will rotate this back to where I had it before.
02:37Then I'll press this Play button here to see what it looks like.
02:41Nice, but I think it's a little bit too fast.
02:44To change the time I'll grab the white dot and slide it over to 2 seconds and
02:51run the animation a second time. That's nice.
02:54I am ready to try it out.
02:56I do need to write a little bit of event procedure code though before I can do that.
02:59I will press F6 to return the workspace to the original workspace, I'll
03:04double-click on my C# file and then I am going to scroll down to the bottom
03:09to show you the code that I am going to use.
03:11This says the beginning time of the storyboard is 2 seconds from the starting
03:15of the application.
03:16So, I am going to delay 2 seconds and then, I am going to say
03:20LogoStoryboard.Begin. You ready?
03:23Let's press F5 to see what happens.
03:27Keep your eye in the lower left-hand corner, and there comes our logo. What do you think?
03:31Isn't that nice?
03:33If you ever want to change that animation, you go back into Expression Blend,
03:36bring up your artboard, press F6 to get to the timeline where you want it,
03:42press the Record button and now I can do something else like change the
03:45size, change the color.
03:47Maybe I'll make this a little bit smaller. I made a mistake.
03:51I was on the 2 second mark.
03:53So, I am going to do a Ctrl+Z. I'll rewind the storyboard, there we go.
03:57Now, I should change the size here.
03:59Grab this corner and slide it a little bit smaller.
04:02Now when I run the animation it will change its size and also rotate.
04:07Let's look at it in the storyboard.
04:08I am going to make this a little bit taller and we are going to look in the image.
04:12If I expand this, you can see there's a RenderTransform and in there is a
04:17rotation for the angle.
04:20This is the value that's being animated, the Angle property.
04:23I can right-click on this and say Edit Repeat Count and say I would like to
04:27repeat over and over again.
04:29I can click on Set to forever, which will make it run forever.
04:32I can say, I would like to run it one or two times.
04:35There are also some storyboard properties, if you come up and you click on the
04:38storyboard itself up here, and I dismiss this dialog first.
04:43Click here. I can over and say AutoReverse the storyboard and how many times to
04:48repeat the entire storyboard, not the individual animation.
04:51Now that you know the basics of creating animations and storyboards, the rest of
04:55this chapter will show you additional pieces of the animation's story.
Collapse this transcript
Controlling animations
00:00Once you have an animation running, you might need to exert some control over
00:03the animation lifetime.
00:05The Storyboard class exposes useful methods like Pause and Stop.
00:08I am going to start in Expression Blend.
00:10If you're following along with the Exercise Files, you're going to want to open
00:13the ControllingAnimation solution.
00:14Then scroll down and double-click on MainPage.xaml.
00:18I am going to zoom out a little by using my mouse wheel and then I am going to
00:24use my hand to center the artboard.
00:26I'd like the look at these animations.
00:28To do that I am going to press F6.
00:30F6 switches you to the Animation workspace, which puts the animation timeline
00:34along the bottom edge of the window.
00:37I'll then go and open one of my existing storyboards.
00:40I have four storyboards in this UserControl.
00:43The one I want to look at first is MoveStoryboard.
00:46Can you see this little red dot here?
00:48That means that within this border is an animation keyframe.
00:51I'll pop-open the border by clicking in the arrow, drill down to the
00:55artboardCanvas and now you can see the keyframes.
00:58There's a keyframe for the blue ellipse and a keyframe for the red ellipse.
01:03Let me show you what the animation looks like.
01:08Now I want to write some code to control the start and stop and speed of
01:11 this animation.
01:12To do that, I am going to switch to Visual Studio.
01:15I have the same project open in Visual Studio.
01:16I am going to double-click on the MainPage.xaml.cs file.
01:20I am going to scroll down to the bottom of my code.
01:23It's very easy to start a story, but we will see that in other movies.
01:26You choose the Begin method on the Storyboard.
01:29It's also very easy to pause the storyboard and resume it after it's paused.
01:35I thought I would add a little bit of code to show you how you can change the
01:37speed of the storyboard as its running.
01:40Type in if. Press tab twice.
01:42That lets Visual Studio stub in some code.
01:44I'll say MoveStoryboard.SpeedRation==4.0.
01:54I'll copy this piece and paste it in here and then I'll assign a value of 1 to
01:58the current speed. Semicolon in there.
02:01Then come down here and I'll write an else clause.
02:08Paste in my code again and set that to 4.0.
02:11So, basically, what I am saying here is if the speed is 4.0, set it to 1.0.
02:15I might need my semicolon here.
02:18If it's any other speed, set it to 4.0.
02:19It's time to see what this looks like. Press F5, click the Start button,
02:30the animation is running.
02:31I've got it set to Autorepeat.
02:33I can pause and I can resume it.
02:37I can also make it go a lot faster.
02:40Click on it a second time, it will just a slow back down.
02:43Another type of thing you can do with animations as far as controlling them is
02:47link animations together.
02:48You have got one animation that runs for 2 seconds and then you want to start
02:51another animation at that point.
02:53I am going to show you a couple of techniques to do that.
02:56Let's switch back to Expression Blend.
02:58One technique is to have a single storyboard like the one I have here.
03:02I've got two animations running in parallel.
03:04What I can do is rewind my animation.
03:07Notice that the red border is on, which means I am modifying my animation.
03:10I am going to put a new keyframe in for this red ellipse right here.
03:16I am going to click the Record Keyframe and then what I am going to do is I
03:20am going to move this keyframe over to 6 seconds and this keyframe over to 4 seconds.
03:27Rerunning the animation and you will see that the first ellipse runs and then at
03:314 seconds it's going to stop and then the red ellipse will run for 2 seconds.
03:34So, I have two animations within the same storyboard.
03:38So, if I call storyboard Begin, storyboard Resume, storyboard Pause or
03:42change the speed, it's going to affect both of those double-animations
03:44within the storyboard.
03:46Another technique is to have two storyboards, and one storyboard initiate
03:50the second storyboard.
03:52Let me show you an example.
03:53I am going to open my GreenStoryboard and run it.
04:01It stops right when it gets next to the purple ellipse.
04:04I'll then open my PurpleStoryboard and run it, and you see the animation looks
04:10like it's dropped and it's sliding down the screen.
04:11What I'll do now is I'll go over to Visual Studio and I will start the animation
04:17in this TriggerAnother_Click procedure.
04:19So I'll say GreenStoryboard. Begin. That starts the storyboard.
04:26Then I am going to register a call back that says when the GreenStoryboard is done,
04:29run this code.
04:33Do a +=. I need a new EventHandler, so I am going to press tab twice, once, twice.
04:40Visual Studio stops the coding for me.
04:42I'll remove this exception down here and then I'll type in
04:45PurpleStoryboard.Begin.
04:49I am ready to run. We'll press F5.
04:56I'll initiate the GreenStoryboard by clicking on the Trigger Another button and there you go.
05:03The completed event is the only event that the storyboard exposes.
05:06I wish there were more events for the storyboard.
05:08I miss the WPF current time and validated event.
05:10That was a handy event for reporting progress during the animation.
05:13I shouldn't complain, however. The storyboard is certainly better than
05:16writing timer-based code.
Collapse this transcript
Simulating physics with animation easings
00:00Easing is a term for modifying an animation so that the rate of change is not uniform.
00:05In other words, it's a way to introduce acceleration or deceleration in your animation.
00:10A simple easing might start out slowly and accelerate to the end.
00:13A more elaborate easing might simulate the rate of the bouncing ball, fast then
00:18slow to a stop, then speed up again and repeat for a few seconds.
00:22I'm using Expression Blend for today's demo.
00:24If you're following along with the Exercise Files, you are going to want to open
00:27the AnimationEasings solution and then double-click on the MainPage.xmal file.
00:33On my artboard I am going to zoom out by using my mouse wheel and then hold the
00:37Spacebar down and my mouse to move it to the center of the screen.
00:40I am going to press F6 to switch to my Animation workspace, and then I am going
00:44to click on Objects and Timeline and add a new storyboard. Click the Plus
00:48symbol, call the new storyboard EasingStoryboard and press OK.
00:54I have got my red border. We're ready to record. I am going to select all four
01:00ellipses by Shift+Clicking on the ellipses.
01:05Then I am going to move my timeline to three seconds. You can adjust the time
01:09with your mouse if you don't get it quite at three seconds.
01:12And then I am going to go to my Properties panel and I am going to go to the bottom.
01:17You might need to collapse a few of your sections to see the bottom portion of
01:20the screen, to the Transform section.
01:22Select the Translate Transform and type a number value of 300 in for the Y value.
01:29Now run the animation. Click the Play button and you can see it all four
01:33ellipses are moving down the screen.
01:34Now I like to play with easing.
01:36Select the red ellipse, rewinding the animation by clicking the Go to first
01:41frame, and then click on the keyframe for the red ellipse right here.
01:46Over in the Property panel, you now see a new section.
01:50It says Easing at the top of it.
01:51There are three sections over here.
01:53KeySpline and EasingFunction are the two that we are going to look at today.
01:56Click on KeySpline, and then take this yellow dot here in the bottom of the
02:01KeySpline, and drag it up on the screen.
02:05This allows me to dynamically change the speed of the animation.
02:10This means to start fast, and then slow down at the end.
02:13For the green ellipse, I am going to select the green ellipse keyframe, and
02:17I will do the same thing.
02:18So, we have two slightly different curves, then I will run the animation.
02:25You see the difference? The red one starts off fast and then slows down at the end,
02:32the green one starts out slow and then it is goes fast at the end.
02:35And now for the purple ellipse, I use new Easingfunctions.
02:39Select the PurpleEllipse keyframe, choose the Easingfunction, and then open this dropdown.
02:45Easings are organized in three columns.
02:48You have an In easing and Out easing and a combination of the two.
02:51So, let's look at the In easing for this first one, and run the animation, maybe
02:55scroll this down just a little bit so you can see all the screen.
02:57It goes backwards a little bit and then down. See that curve.
03:03It goes backwards and then up.
03:06If I choose the Out easing and run it, it goes the opposite.
03:11It goes up and just a little beyond the edge of the regular animation then back
03:15and you can probably guess what will happen if I choose this one.
03:18Up, a little bit outside the boundaries, and then back.
03:24This is how you can bring a much more natural feel.
03:26You want to do bouncing ball? Choose this one here called Bounce.
03:35I love the new Easing functions in Silverlight.
03:36They really bring a snap to your animations and Expression Blend makes them easy
03:41to add to your timeline.
Collapse this transcript
Create a custom easing
00:00If none of the built-in easing suits your needs, you can always create a
00:03custom easing and it's surprisingly easy to do.
00:05I am going to start this project out in Expression Blend.
00:08If you're following along with Exercise Files you are going to want to open the
00:11CustomEasing solution and then double-click on the MainPage.xaml file.
00:16I am going to zoom out a little bit with my mouse wheel and then I am going to
00:20open up the EasingStoryboard.
00:22Remember to do that, you go to your Object and Timeline, click on this dropdown
00:25at the top of the Object and Timeline, choose the EasingStoryboard and then the
00:30storyboard shows up.
00:32Let's run this storyboard and see what it looks like.
00:37I have different easings applied to each one of these animations.
00:40I am going to press F6 to make it little bit easier to see my timeline.
00:43I am going to click of on the BlueEllipse and then I can see the keyframes in the bottom.
00:48Let's look at the easings I have applied.
00:50The BlueEllipse has no easing, the RedEllipse has the Exponential In easing, the
00:57GreenEllipse has the Quintic InOut and this last one has None.
01:01Run the animation again so you can see the results at different speeds.
01:05I am going to generate a custom easing and apply it to the PurpleEllipse.
01:10To do that I am going to switch to Visual Studio.
01:11I am in Visual Studio, I have the same project open, I am going to click up
01:16here to turn off the Show All Files.
01:18That way it'll look more or like what you have in your computer.
01:20Then I am going to right-click on CustomEasing and add a new class.
01:25I am going to call this MathEasing.
01:25I'll write a little bit of code.
01:31So, I typed in EasingFunctionBase. Actually I didn't type it. I pressed Tab and let
01:38Visual Studio do the typing for me.
01:39And if you note there is a little blue rectangle underneath the letter E. If I
01:44move my mouse over this, and click on that little dropdown arrow, I can have
01:47Visual Studio write the code for me.
01:48Implement the abstract class 'EasingFunctionBase'.
01:51Now I just need to go in here and write some code.
01:54This is the timeline that's being passed to me by Silverlight. I can modify this.
01:59If I multiply it, I can speed up the time. If I divide I could slowdown the time.
02:03What I am going to do is return Math.Pow, and do Ctrl+S to save.
02:14Then I am going to switch back to Expression Blend and now I see the
02:17message that I was expecting, which is that blend is picking up the changes in Visual Studio.
02:20I'll click Yes. I'm going to add my custom keyframe to the PurpleEllipse.
02:24To do that I need to click on the PurpleEllipse keyframe, bring up the
02:29Properties pane, choose the Easing section, open that and then scroll to the bottom.
02:35It looks like I haven't compiled here in the Expression Blend yet.
02:38Let me try doing that. Project > Build Project. There it is.
02:46What is supposed to happen is Expression Blend realizes there's a new class,
02:50which is of type easing. It adds it to the list of existing Easing functions.
02:54Now it's just a simple matter of selecting my custom easing and applying it.
02:58What I really like, it's a great implementation in Expression Blend, is they
03:02have analyzed my custom easing and written out on a graph showing what it'll look like.
03:06I didn't have to do any of this. Blend does this automatically.
03:09So, I click the custom easing and then, I run the application.
03:16I said at the start of the movie it would be easy to add a custom easing and it is.
Collapse this transcript
11. Templates and styles
Placing XAML resources within a FrameworkElement
00:00If you are a programmer, you know how to reuse items.
00:03You probably create variables, big code modules or place code in external DLLs.
00:07My point is that you know how to do this in your favorite programming language.
00:11But what about reusing items in your XAML?
00:13You are in luck, because XAML has a way to do this too.
00:16It's known as XAML resources.
00:18The basic concept in XAML resources is that every resource is stored is stored
00:21in a ResourceDictionary.
00:22The ResourceDictionary is .NET type and every item that you put in the
00:26ResourseDictionary must have a unique key.
00:28All Silverlight types can be placed in the ResourseDictionary and some .NET
00:31types can be placed in there as well.
00:33And a FrameworkElement in Silverlight is things like the page, the UserControl,
00:37the StackPanel, a button. Those are all FrameworkElements.
00:41Each FrameworkElement has a resources property of type ResourceDictionary.
00:45This gives a very convenient way of adding any item to the ResourceDictionary.
00:50I have switched to Visual Studio.
00:51If you are following along with the Exercise Files, you are going to want to
00:54open the PlacingXamlResources Solution and then open the Views folder and choose
00:59PutResourcesInXAML. Double-click on that file.
01:02Here is an example of adding some resources.
01:05I am using a navigation type application here.
01:07So, at the page level, I am using the Resources property, which gives me access
01:11to the ResourceDictionary.
01:13I am instantiating a SolidColorBrush, setting some properties on it and then
01:17because it's in a ResourceDictionary, I provide a key.
01:21Without this key I'll get an error.
01:22I am also placing a rectangle in there named BurntRectangle.
01:27That's at the page level.
01:29At the Grid level, I am also placing a resource called helloString.
01:32This shows that you can use .NET types.
01:34I have a string class, which comes from the mscorlib assembly and I also have an
01:40instances of two of my classes.
01:42If you look over in my project, you'll see that I CalenderDays.cs file and if
01:48you open that up, I have a class called CalenderDay, which has Dayname and
01:53IsWeekend, and a class that is a list of calendar days.
01:56So, I am instantiating an instance of my CalendarDay and assigning the day name
02:01Tuesday and IsWeekend= 'False' and I am instantiating my entire list of calendar days.
02:05I can then use these in my XAML.
02:07How do you use the resource in your XAML?
02:10Use what's called the MarkupExtension.
02:12The curly braces in this signify that you are calling the MarkupExtension.
02:15You use the word StaticResource.
02:17That's the type of MarkupExtension and then the key name of your resource.
02:21Here is an example. The background color of this button will come from a
02:25StaticResource named RedBrush.
02:28You might be wondering where these ResourceDictionaries can be stored.
02:31They can go in an element, they can go in any of the parents of that element,
02:34they can be at the user controlled level or at the application level.
02:37They can also be an external files called Merged Dictionaries.
02:41If you specify a resource with the key, Silverlight attempts to find a matching
02:45resource and it does that by walking the object tree.
02:48It starts at the element where you declare your intention to use the resource
02:52and walks the object tree trying to find a match.
02:54The first match that it finds as it walks up the tree is the one that is used.
02:58Here is an example. I have two buttons in an application on separate pages.
03:03In this orange button,
03:04if I specify that I want to use the default brush, it will look to see if there
03:07is a default brush defined at this Button level. If not, then it will look in
03:10the grid, then the page, and then the application and then in any
03:15ResourceDictionaries.
03:16This blue button will do the same thing only it is walking a different tree.
03:20If I specify a resource at the application level, both buttons could use it.
03:25Let's see how you can use some of these resources.
03:27I am going to switch back to Visual Studio.
03:28I am going to open the UsingResource.xaml file.
03:33This has the same resources I showed you on the previous example.
03:37So, the difference is how do you use them?
03:40I have a TextBlock down here and the foreground color of the TextBlock is going
03:44to come from the Sun Brush.
03:45This TextBlock's text property is going to come from a resource called
03:48helloString, which is defined here.
03:51This ListBox is going to get it's information from the Days business class.
03:57I suspect you would like to see what this looks like.
03:59I'll press F5. Click on the UsingResource button.
04:05There is the TextBlock using the brush.
04:08There is the TextBlock using the helloString and here is the list box that has
04:11been filled with the items from the CalendarDays class.
04:15When you click on the show contents of ResourceDictionary, it walks through the
04:18contents of the research dictionary and shows you the key value, HelloString, and
04:23the current value that's stored in there, Hello, day, and the type is
04:27CalendarDay and the Days type is CalendarDays.
04:29A couple of things to keep in mind when using resources.
04:33You cannot put a UI element in a resource and reuse it.
04:37Sometimes developers think this will be a good idea to put a button in
04:39resources, but a UI element can only be a child of a single parent,
04:42which prevents reuse.
Collapse this transcript
Centralizing settings in styles
00:00Web programmers figured out a long time ago that it doesn't make sense to set
00:03the same property values repeatedly on an element.
00:06That's why we have CSS style sheets on most web pages today.
00:09Silverlight, while not a web page technology, suffers from the same issues.
00:13Writing the same property settings is not only tedious, but it makes for harder
00:17work when it comes time to change the properties.
00:19For example, in this screenshot, I have got two TextBoxes with exactly the same
00:23properties set on each individual TextBox.
00:26If I decide that Yellow is not the correct value for the foreground I have
00:30check out every TextBox in this application, verify that it has a yellow
00:33foreground and then decide if that's one of the TextBoxes I want to change to the new color.
00:38Silverlight provides styles to elevate this problem.
00:40A style is very simple.
00:41It's a collection of property settings.
00:43Any value that can be set in a Property attribute can be set in a style.
00:48Styles can contain property setters, they can contain animations and storyboards.
00:55They could also contain control templates.
00:57Here is an example of a style.
00:59At the top of the example, I have a Resources section.
01:02Within the Resources section, I am creating a style and giving it a key so I
01:06can reuse it later in my XAML.
01:07I am also specifying some information to Silverlight about the type that I
01:11expect to use this on.
01:12I am saying TargetType= "TextBox".
01:15Then I do a series of setters.
01:18The first setter is changing the property background to a value of Yellow and
01:23the second setter is setting the property margin to 20.
01:26Inside the main body of my document, I have two TextBoxes.
01:30The first TextBox is not using a style so it will use the defaults.
01:33The second TextBox has a MarkupExtension applied to the style.
01:37So, I say Style= StaticResource and then I specify my style, DataEnteryTextBox.
01:43I'll show you how easy it is to create styles in Expression Blend.
01:46If you are following along with the Exercise Files, you are going to want to
01:48open the UsingStyles solution and then double-click on the MainPage.xaml file.
01:55Size your artboard so that you can see the entire window.
01:58I thought I would create a style for a button and apply it to all of the buttons
02:03over here in this area.
02:04To make it easier to see, I am going to create a temporary button and just draw it in my artboard.
02:11Select the button, and then go to Object > Edit style.
02:17There's two choices within this menu.
02:19I can edit a copy of the existing style or I can create a brand new empty style.
02:24I will chose Edit a Copy.
02:25I will give it a style Key.
02:28It also says Name up here but it's the key because it's going in
02:30a ResourceDictionary.
02:31I am going to call this one BigButton.
02:35Then I define the scope, where to put this resource in this lower half.
02:38I can define it at the application level, within this document only, and I can
02:43even drill down into certain elements within this document.
02:47It's also possible for me to put in an external file.
02:50To do that I would need to click the New button, create an external file, and then click OK.
02:59I have just generated a new file.
03:01You can see it over here in Project explorer, OurStyles, and then I am going to
03:04say put the BigButton in the ResourceDictionary.
03:08We are now in the Style Editor.
03:10The way you can tell we are in the Style Editor is by looking at the top of your artboard.
03:14There are two symbols up here.
03:16The first symbol says Button and this allows me to go back to the button in the artboard.
03:21If I click on this icon, that's the style.
03:23Now I am working on the style only.
03:26Choose the item over here. Remember that styles are just set in properties.
03:29So, go up to your Property panel and start editing.
03:32I want a bigger font.
03:35I want the margin to be larger.
03:37So, I am going to go up and choose the BorderBrush and I am going to set the
03:42BorderThickness to be 3 pixels and then I am going to scroll down to the
03:49Transform section and I am going to apply a Skew transform, clicking on the Skew
03:56button and then we are going to take this X value and skew it to about -5.
04:01I think that's good enough.
04:03I'll scope up to the Button. By the way you can click here or you can also click
04:07on this icon over in the Objects and Timeline.
04:09There is my new button.
04:11Now I want to apply the style that it just generated to these three buttons over here.
04:15So, I'll click the Add Ellipse button and then I am going to go to my Property
04:19panel and type in style.
04:21That shows me the Miscellaneous section. I am going to click on the little
04:24property peg and choose Local Resource > BigButton.
04:29Now this is a bug in the Accordion control.
04:35Occasionally the Accordion control will collapse down inside Blend and you
04:39cannot see the contents anymore.
04:40You are suppose to be able to click on the different headers.
04:43I can solve this by adding a second button quickly and then adding a style.
04:50Local Resource > BigButton.
04:52If I ever decided to change the style, go back to the first button, choose
04:56Edit Style > Edit Current.
05:01Now you can see it remembers what style I am editing.
05:04Maybe I'll change this to be slightly bigger in the font size, 24 points
05:08makes more sense, and you see that both buttons have changed because they
05:13share the same style.
05:14With style you will never have to define your Property attributes one element
05:17at a time ever again.
Collapse this transcript
Creating alternate control UI with ControlTemplates
00:00There is one thing you can say about UX designers is that they love to play
00:04around with the boundaries of UI.
00:05Just look at all the changes in the simple button control over the last ten years.
00:09There have been flat buttons, pressable buttons, gel buttons and round buttons,
00:14and even pastel buttons.
00:16They are all buttons.
00:17They are all clickable and they all initiate an action, but they look different.
00:22Silverlight adds something called the control template, which formalizes this process.
00:27In Silverlight, every control has a default template, which is supplied by the
00:31control author, but you are not limited to this template.
00:34You can design your own, then instruct Silverlight to use your new template
00:39whenever it renders the control.
00:40This is much more powerful than styles as a style can only supply new
00:44property values, not new UI.
00:47I am going to use Expression Blend for this demo.
00:49If you are following along with the Exercise Files, you are going to want to
00:52open the ExploringControlTemplates solution.
00:56Then open the Views folder, then double-click on CreatButtonTemplate.xaml.
01:02In a minute or two, I am going to add a control template to this button and then
01:06apply that template to the rest of the buttons on this page.
01:09Before I do that however, let me show you a couple of examples of control templates.
01:13I will press F5.
01:17This application is a navigation application, so it has the buttons on the
01:20upper right-hand corner.
01:22I will click on the SampleButton.
01:24This is a free template available from the Expression Blend web site.
01:29Nice looking button.
01:30It also has a mouse-over effect.
01:32When I move my mouse into the button, it plays an animation and change the colors.
01:37For a simpler take on the button, click on the second sample button.
01:42This is a nice two tone button. Top half is a light gray, the bottom half is a dark gray.
01:46When I mouse into it, I get a blue glow.
01:49It's not just buttons that can have templates. Any control can have a substitute UI.
01:54Let me show you ProgressBars.
01:56The top progress bar uses a default template.
01:59The bottom progress bar uses a new control template and then the last button,
02:05brings up our page, the one that we are going to modify.
02:08We are going to close the browser and we turn back to Expression Blend.
02:11I am going to create a control template for this button.
02:15To do that, I can right-click on my button and choose Edit Template, or I can
02:21click on this button at the top of this window, there is little dropdown here,
02:26and I can choose Edit Template.
02:28My choices are to Edit a Copy.
02:30That will take the existing template that already exist on the control, make a
02:34duplicate copy of it and let me work with it.
02:36I can also create a brand new empty template.
02:39For today's demo, I will choose this option, Create Empty.
02:44I will provide an identifying key, OrbButton. Pick my scope.
02:49I am going to leave it at the default and then click OK. What happened?
02:56Well there have been some changes in the UI.
02:58Look at the upper left hand corner of my work area.
03:00It says Button and then it says Grid.
03:03The tooltips explains what's going on.
03:05This is the page and this is the Button template.
03:09So, if I want to go back to the page, I click here.
03:11If I want to edit the template, I click here.
03:14Since it's an empty template, there is nothing to show yet.
03:17I am going to change that by drawing an ellipse.
03:19I am going to go to my tool panel, find this area, press and hold and look for the Ellipse.
03:25I also could have pressed the L key.
03:26All right, I am going to come over and draw up ellipse within the boundaries,
03:34make sure that my margins are at 0, and then I am going to go paint some UI
03:43inside of this ellipse.
03:45I am going to go to the Brushes section.
03:46I am going to choose a Radial Gradient Brush and we click on the Radial Brush
03:51button and then on the Radial Button, then I am going to choose a red color for
03:56the first brush. We could have a light red color and for the second brush, I am
04:03going to choose a darker red.
04:05I like that, although I would like the first color to be a little bit
04:09lighter. There, I like that.
04:15Next, I will click on the Stroke, and make my StrokeThickness 4 pixels and then
04:22I will apply Gradient Brush to the border.
04:24Click on the Gradient Brush. Choose Linear Gradient Brush, which is the default.
04:28I need three gradient stops.
04:30So, I will slide the black gradient stop to the center.
04:34Then I will click over here to add another gradient stop.
04:37The center one is going to remain black.
04:39The two outside gradients will be a light gray.
04:41So, I will select this white one, dial in a light gray, and then I will copy
04:47the hexadecimal value, click on this other gradient band, and paste in the new value.
04:54Now let's zoom in and then pan over.
05:01Not quite happy yet, so what I am going to do is take this gray gradient and
05:04move it closer to the center for both of these.
05:07I want to have a more concentrated black band across the center.
05:10To fool our eye into thinking this is glass, I am going to draw another ellipse
05:14that's going to represent a reflection off a curved surface.
05:20I am going to draw two more ellipses.
05:23This ellipse I am going to make black, with no stroke.
05:26So, I will click on Stroke, and turn off the stroke.
05:29Set the border thickness to 0, and then I am going to fill this with a black color.
05:34You will see why in a minute.
05:37Click Solid Color Brush and then fill with black and then the next ellipse
05:41I am going to draw, I am going to draw over the top of this one and then I am
05:45going to bring the bottom of this new ellipse just about to the center of the
05:48button, right about there.
05:50This ellipse is going to be transparent.
05:51So I will choose Gradient Brush, Radial Gradient Brush.
05:57This gradient stop will be white and this gradient stop will be transparent.
06:02So, I will dial in the Alpha channel.
06:05That's nice. I like that.
06:06We will move this over a little bit, and a little bit bigger, and then I can
06:11move the location of the center dot by clicking on that Gradient tool or
06:15pressing G, and then I can move the white center over here near the left edge.
06:21The button is about the way I want to look except rest of this ellipse is
06:25sticking out beyond the boundaries of the button, so I need to trim that off.
06:29That's what this ellipse is for.
06:30Go over and select my Selection tool, and I am going to drag my ellipse over
06:34here and what I want to do is fill the center, the red area. I need to uncheck
06:40Snap to Grid lines, Ctrl+Z and try this again.
06:44Bring this over to center, drag the size down.
06:49I still have Snaplines turned on too.
06:51Okay, that makes it easier.
07:03You see what I am doing?
07:03I am filling the red area with this black circle.
07:07Just to review, on my machine, I had snapping to Gridlines enabled and also had
07:14snapping to snaplines, which was preventing me from getting right where I
07:17wanted the ellipse. So now I am ready.
07:20I have my black circle selected.
07:21I am going to go to Object > Path > Make Clipping Path.
07:28Blend then says, all right you have two ellipses, which one do you want me to
07:31apply the clipping path to?
07:33It would be the last ellipse that I added.
07:36And then I click OK.
07:38And now that trims off the excess that was outside the boundaries of the circle.
07:42It's starting to look nice.
07:43And here is the issue. Remember I said earlier that the button is content control?
07:46Do you remember that this button has text on it? Let's look.
07:52This button says Start Here, but I am not seeing that in my button.
07:56That's the job of an element called ContentPresenter.
07:59I will click on my Assets button, do a search for ContentPresenter, select it,
08:08and then I can add this to my designer surface.
08:11Before I do that, I need to go back to my template.
08:15Now I am going to double- click on the ContentPresenter.
08:17Can you see what happened?
08:19The content is now showing up.
08:21I need to center it.
08:22So, I will choose to set the HorizontalAlignment equal to Center and the
08:27VerticalAlignment equal to Center and I see that I have some margins on here.
08:31I am going to remove those.
08:32I will change this margin to 0 and this margin to 0, and now that
08:36ContentPresenter is centered.
08:38It's important to note that the ContentPresenter is not text.
08:41It's just a placeholder.
08:43The content can be an image. It could be another shape.
08:45It could a checkbox.
08:47This is just a place that it goes.
08:49One more thing that I want to show you in this template is something
08:51called template bindings.
08:53The issue I am trying to solve here is there might be properties on the
08:56control itself that I would like to use it in my template. Maybe the width and
09:00the height of the button I would like to apply to some of my elements in my template.
09:04I am going to change to red brush by applying a template binding.
09:08To do that I am going to go up to the Brushes section and I am going to select
09:12my red ellipse first, then I am going to go to the Property Peg click on the
09:18Property Peg and choose Template Binding.
09:21Expression Blend comes back and says, I see that there are these
09:24compatible properties on the underlying control, which one would you like?
09:27And I choose Background.
09:29Now it's picking up the value from the underlying control.
09:31I will scope out by clicking on this button, so I am no longer editing the
09:35template and then I am going to come down here and select one of these buttons
09:39and apply my template to see if it works.
09:41Do a search over here in the Property pane for template, click on the Property
09:45Peg, choose Local Resource and there is our template, OrbButton.
09:50Look at that. It worked.
09:51Let's see if it picks up the change in the brush.
09:53We will close out the template. I'll go to the background of this button and
09:57dial in a new color. It is working.
10:00I should be able to turn off the brush, apply a Solid Color Brush, apply a
10:04Gradient Brush, whatever.
10:06I am going to attempt to apply the template now to this button.
10:09It has picture content.
10:10So, I will click here, find my template, apply the new template, remember that
10:16it is called OrbButton, and it's also working.
10:21The content is being represented in the ContentPresenter.
10:24If I had to name my favor Silverlight feature, I would be hard pressed but I can
10:27say this without reservation. Control templates make my top five list.
Collapse this transcript
Dressing up your data with DataTemplates
00:00It is common in Silverlight to have controls bound to underlying data.
00:04That data doesn't have to look boring though.
00:06Silverlight data templates can be applied to any data type.
00:09This means that if you have an items control bound to a list of invoice Items,
00:13you can create a UI to wrap each instance of the invoice class as it is added to the control.
00:17I'm going to use Expression Blend for this demo.
00:19If you're following along with the Exercise Files you're going to want to open
00:22the DataTemplates solution, then double-click on MainPage.xaml.
00:28I'm going to zoom out so I can see my entire interface and center my UI.
00:32My data source for this example is going to be the CalendarDays class. Let's review.
00:37Double-click on CalendarDays. I have two classes in here.
00:42I have the CalendarDay class, which has a DayName and a Boolean property called IsWeekend.
00:47I also have a class called CalendarDays, which is list of CalendarDay.
00:52When I bind my list box to the CalendarDays class, each row in the list box will
00:58be an instance of the CalendarDay class.
01:01Therefore I will be applying a data template to CalendarDay. Back to the main UI.
01:07I need a data source. Blend makes this easy.
01:10I move to the Property panel, choose the Data tab, and click on the Add live data
01:15source then, I click on Define New Object Data Source.
01:20The class I'm looking for is called CalendarDays so I'll choose that and
01:23then click OK and as you can see, a CalendarDays(Array) has been added to
01:28the Data Source window.
01:29Now I can pick up this array and drag it to my work area and you can see
01:33from the tooltip that it says it's going to create a ListBox for me and bind
01:36it ItemsSource property to my underlying DataSource, which is precisely what I want.
01:40I'll let go of the Mouse button and then zoom in a little bit so you can see what happened.
01:44It created a ListBox and since Silverlight doesn't know what to do with my
01:49data it just created a TextBlock for the word Monday and a checkbox for the Boolean value.
01:56That's nice, but I want a template just to make it look nicer.
01:59To do that I'm going to right-click on the ListBox, move down to Templates and
02:05I'm going to choose this one called Edit Additional Templates and I'm going to
02:09choose Edit Generated Items (ItemTemplate).
02:12Each item in the ListBox is going to get it's own data template. That's the idea.
02:17I'll choose to Edit a Copy and I'll give it a name. DaysDataTemplate.
02:24I'm going to keep the default scope and click OK.
02:28Whenever you're editing a template in Expression Blend you can see that you're
02:31in the template by looking at this part of the UI.
02:33This is going back to the ListBox as is.
02:36This is editing the ItemTemplate.
02:38Notice what happens over here in the Object and Timeline. ListBox shows it's in
02:42the LayoutRoot and it's in the UserControl, ItemTemplate says there's an
02:46ItemTemplate and there's our StackPanel and a TextBlock and a CheckBox.
02:50I can edit this template just like any other part of my UI.
02:53First thing I'm going to do is select the StackPanel, choose Properties,
02:58Orientation and change that value to Horizontal.
03:02Next I'm going to take the TextBlock and give it a minimum width size.
03:09I'm thinking about 120 would be good.
03:12That's a little bit too much, maybe more like 80.
03:17I'll change the font to bold and then lastly, I can click on this checkbox and
03:25set it's Content property. Content.
03:33It's looking pretty good.
03:34Let me go out and scope out to the ListBox. I'll make my ListBox a little bit
03:38wider and then back to my template.
03:42The last thing I'm going to do is show you how you add any other UI.
03:45I'm going to take this picture, this Journal.png file, and now we are going to
03:49drag it over and add it to my template and then size it a little bit smaller.
03:57One last thing I'll give it a margin, slide it over just a little bit more and I'm done.
04:03Scope out and I'll run the application and you'll see that our data has a
04:08brand-new template applied to each row.
04:14As you can see, it's easy to make your data look beautiful, if you understand
04:17how data templates work.
Collapse this transcript
Explaining the VisualStateManager: A simple way to manage control state
00:00A control can have multiple appearances depending on what is happening in your UI.
00:04For example, changing the background color when a button is pressed or
00:07increasing the button size when the mouse hovers over the control.
00:10These are good examples of this scenario.
00:12In the Silverlight world, these different looks are known as states and they are
00:16supervised by the VisualStateManager.
00:19Using the VisualStateManager, you create one or more states for your control.
00:23For each state you specify a new UI look, and actually tell the VisualStateManager
00:27when to enable each state and Silverlight does the rest.
00:30For this example, I'm going to be using the Expression Blend.
00:33If you're following along with the Exercise Files, you're going to want to open
00:37the ExplainingVisualStateManager solution.
00:40Then double-click on the EditButtonVSM.xaml file.
00:44In this UserControl, I have 3 buttons. I though I would start by showing you one
00:49of Microsoft's VisualStateManagers.
00:51To do this I'm going to right- click on the button and choose to Edit
00:54the existing template.
00:55It'll ask me for a style. I'm going to accept the default value and then click OK
01:01and then I'm going to click on States.
01:03This is the VisualStateManager.
01:06Microsoft has defined 6 states for the button.
01:08Each state belongs in one and only one group.
01:11So, there is the CommonStates group and there is the FocusStates group.
01:15A button can have a FocusState set and a CommonState set.
01:19So, I could have a normal button, Unfocused and a normal button, Focused.
01:25I can have pressed button, Focused and a pressed button, Unfocused.
01:29What does the Focus do?
01:31Focus means that the user has moved to that control and it usually shows some sort
01:34of outline on the control to indicate that.
01:37When I click on the FocusState, Expression Blend enters recording mode.
01:40Now any changes that I make to this button will be saved in this state.
01:43I'm going to drill down into the template, find the FocusVisualElement, which is
01:48a rectangle, and then I'm just going to make it slightly smaller.
01:51Then I'm going to run the application, so you can see what it looks like.
01:56You might need to go to App.xaml. cs and ensure that you have the
02:01EditButtonVSM as the RootVisual.
02:06I'll press F5 and there is the FocusState and you see this has a different
02:13rectangle than the other 2 buttons.
02:17Returning back to Expression Blend, I'm now going to select the MouseOver state.
02:23I am going to zoom in a little bit closer to my button. Clicking on each one of
02:29these states, I can see what the button's going to look like.
02:31I want to alter the Pressed state and the MouseOver state.
02:36I'm going to click MouseOver, I'm going to drill down into the template again,
02:40into Background, into Grid, into something called the BackgroundAnimation.
02:45It's really not an animation.
02:47It's a border that has an animation applied to it.
02:50So I can change the border value here, and fill on this.
02:54I'm thinking of more of a yellow color like so, and then for the Pressed state,
03:01same place, only this time I'm thinking maybe something green.
03:07One more thing I wanted to do before I left.
03:09I want to change the default transition time to one second.
03:14That's how long it takes to transition between one state and another.
03:18Press F5 to run the application. Mouse in.
03:23It changes to yellow. Press and hold my mouse button.
03:26It changes to green. Let go.
03:28It goes back to the MouseOver and then when I move my mouse out, it goes back
03:32to the normal state.
03:34You can also use VisualStateManager in your own controls.
03:37For this example, I'm going to edit my App.xaml.cs and return this to the
03:42TrafficLightVSM as the RootVisual and then I'm going to go to Projects and
03:48choose TrafficLightVSM.
03:51This is a UserControl that mimics looking like a traffic light.
03:54I'm going to add my own StateManager. I will click on the States Tab, click here
04:00to Add a state group, set my Default transition time to one second and then I
04:05click on this button, Add state.
04:08I name each state. I'm going to call this the RedLight state and I'll add 2 more.
04:14This will be called the YellowLight state.
04:22Can you guess what the last one is going to be called?
04:28The GreenLight state.
04:30Again the recording is on for each state.
04:32I'm going to click on RedLight then choose my Ellipse and then I'm going to
04:38dial in some colors.
04:39I'm going to change these 2 red bands in the center to black.
04:43I'll click on this red band in the gradient and then grab the circle and slide
04:47down to the black area.
04:49Click here. I can also click directly in this area.
04:52I wasn't paying attention.
04:53I need to do this on the yellow ellipse.
04:55Then I'll select the green ellipse and change these two values, and that's my RedLight state.
05:09Then I click on YellowLight and do the same thing.
05:12Now I'm going to change the red one and the green ellipse and finally for the
05:26GreenLight.
05:37Bit tedious there but we're done.
05:39You can add easings to your animations, have a one second transition.
05:44I can click on the RedLight and choose to go either RedLight to YellowLight or
05:47RedLight to GreenLight.
05:48I'm going to create an easing to go from RedLight to GreenLight.
05:52When I'm sending the YellowLight easings, I can choose to go from the
05:55YellowLight to the GreenLight or the YellowLight to the RedLight.
05:58I'm going to create an easing transition to go from the YellowLight to the
06:01RedLight and for the GreenLight, I'm going to set up an easing transition to go
06:05from the GreenLight to the YellowLight.
06:06Then I can click on, let's choose yellow, and then I can click here and add an
06:12easing, so I'm going to choose something crazy like the Bounce.
06:15I have one more thing to do. I need to tell the VisualStateManager when to
06:19enable the state. To do that I need an event.
06:22I'll click on the lightning bolt.
06:24Choose the RedLight, I've already got some code written here,
06:29MouseLeftButtonUp, move my cursor here and hit Enter and then I'm going to type
06:33in the VisualStateManager.GoToState, so when the Mouse is up on the red
06:42ellipse, turn on my state.
06:44The first parameter represents this control, the second parameter is the name
06:48of the state, RedLight, and the third parameter is whether or not I want to use
06:54the transitions, true.
06:56To keep this shorter, I'm going to just uncomment these other two procedures and
07:02then we'll run the application.
07:06Here's my Traffic Light control.
07:08I'll click on the red ellipse and the transition happens.
07:10I'll click on the green ellipse and its transition happens at one second.
07:14Again on the yellow and you remember that I had a yellow to red transition with an easing.
07:20Let's see what happens when I click on the red ellipse now. I'm going
07:22from yellow to red.
07:25If I go from green to red, I don't see that transition.
07:30As you can see, the VisualStateManager does a great job of melding together the
07:33states of the UI into a central, easy to manage tool.
Collapse this transcript
12. Application-Level Concepts
Handling errors in a Silverlight application
00:00As a programmer you should write proactive error handling code in all of your
00:03procedures but in the real world things don't always go as planned.
00:07You might write some code that fails to trap a specific exception.
00:10In that case you should always write a general unhandled exception framework.
00:14Microsoft is good about including hooks for these general handlers in .NET,
00:18which makes our job easier.
00:19For this demo I'm going to use Visual Studio and I'm going to create a brand-new project.
00:24To do that I'm going to go to File > New > Project, I'm going to choose C# and
00:31Silverlight Application. The name is not important neither is the location, so just click OK.
00:36I do not need a companion new website, so I'll uncheck the checkbox and then click OK.
00:43I need a little bit of UI so I'm going to go to my MainPage.xaml, I'm going to
00:47add a Button class that says Fire Exception, and then I'll write a Click
01:04procedure, press Tab twice to stub in my code.
01:06And then and then I'll press F7 to switch to the code window.
01:12I'm not going to spend a lot of time in this video talking about the way
01:15.NET handles exceptions.
01:17Each language in .NET has its own way. In C# you can use the Try, Catch and
01:21Finally blocks in your code to trap exceptions and to throw exceptions, you can
01:26use the Throw keyword.
01:27So, I'm going to say throw new Exception.
01:31If something has gone wrong in our application, I'm throwing an exception.
01:34I can pass extra met data to the exception right here, Demo Exception, and I'm ready to test.
01:42I'm going to start by pressing F5.
01:44Now you recall that in F5 what happens is the browser starts up and then we
01:48attach a debugger to the browser, so I can debug my code.
01:51That's important, keep that in mind.
01:53Press F5 and I'm going to click on the button to see what happens.
02:00When you are in attach to the debugger we can switch the Visual Studio so we get
02:04a chance to just figure out what went wrong with our code.
02:06When it switches to Visual Studio, it shows a yellow line and this little
02:10Exception tool window.
02:11It lets us drill down and find out more information about the error.
02:14I can click on the View Detail and then drill down into the Exception.
02:19If it's a complex exception there might be multiple layers of inner exceptions
02:23available and I can drill down into the clicking on that Plus sign.
02:26I can also get call stack information and plenty of other information from this exception.
02:32I'd rather see what happens to the end user, not a developer, so I'm going to
02:36shut down the application and I'm going to start it up again this time by
02:40choosing Debug > Start Without Debugging.
02:43That means I will not attach a debugger.
02:47I'll click on the button again and it's subtle but if you look in the lower left
02:51hand corner you'll see there is the error flag down there that IE shows.
02:55This is because brand-new Silverlight Applications, if you're using the
02:58template that Visual Studio provides for you, automatically has a general exception handler.
03:03If there was no general exception handler, what would happen is your Silverlight
03:06application would cease to work and it would show a blank area where the
03:10Silverlight control is on the webpage.
03:12Let's go look at the code.
03:12I'm going to close the browser.
03:14To find the exception handling code, I'm going to go to the application level by
03:18moving to App.xaml.cs and opening this file.
03:24The first line that's important to us is line 22.
03:27At the application level, if there's ever an UnhandledException raised then
03:31call this code, Application_UnhandledException.
03:36When I scroll down to this area, make my view a little bit larger, full screen.
03:41The code here that Microsoft wrote says, if there is not a debugger
03:46attached, then say that we're handling the error, that way it won't re-raise
03:51the error, and then call BeginInvoke- delegate, which means it calls some other
03:56code, ReportErrorToDom.
03:58That calls this code down here and what this does is it goes out to the
04:02underlying browser whether it's IE or Firefox or whatever and it tells it to
04:07call this EVAL, throw new Error.
04:08That's why I get the little yellow icon in IE and in Firefox, you get the
04:13script error dialog.
04:15In the real world, what you would do here is you would also log your information.
04:24You don't know why you had an error.
04:26It's often good to log that to some source. Then you can open the logs later and
04:31read out the information.
04:33Let me restate that. The Visual Studio Silverlight project has a simple built-in
04:37handler, which ignores the error.
04:39You can write a better handler than that.
04:41I suggest logging the error so your development staff can learn more about
04:44what went wrong.
Collapse this transcript
Setting up application events
00:00In this movie I'm going to look at the Application class.
00:03The Application class has several useful events like the Startup and Exit events,
00:07and it also has some properties you might find useful in your application.
00:10We're going to create a brand-new project.
00:12So, let's go to File > New > Project, choose Visual C# and Silverlight Application.
00:18The name or the location is not important for this application. Click OK.
00:24I do not need a companion website, so I'll uncheck the checkmark and click OK.
00:28App.xaml is where you'll find most of your code dealing with the application.
00:32I'll open up App.xaml then double-click on App.xaml.cs.
00:38In another movie in this series we've talked about the exception handlers, so
00:41I'm going to go ahead and delete this code, plus this code down here.
00:50And I'll compile the application to make sure I haven't broken anything.
00:54This cleans up the code window a little bit.
00:57In the Application class you can sign up for several events.
00:59You type in this, dot, and then the event you want like
01:02CheckAndDownloadUpdateComplete and then you use typical C# += syntax.
01:08The two most common events that you work with are the Startup event and the Exit event.
01:12In the Startup event the main thing that you do in here is set your root visual.
01:17If you do not set your root visual in here, you'll end up with a
01:20blank Silverlight page.
01:22The loading screen will run and it will never get any farther than that.
01:26The Application_Exit fires when the application is shutting down.
01:30Let's put a breakpoint here and see what I mean.
01:32I'm going to press F9 while in Visual Studio, then I'm going to run the
01:35application by pressing F5. I'm now going to go to another site.
01:41I'm going to click on my Home button here, which will take me to another site,
01:44and the page that had the Silverlight application on it unloaded and that
01:47causes the Application_Exit to fire.
01:50You can use this event for saving application information and it's commonly used
01:54to save user settings before they leave the application.
01:57I'll remove this breakpoint by clicking on it with my mouse and press Stop Debugging.
02:02In the Application_Startup'I thought I would look at a few of the properties
02:06on the Application class.
02:08You can type in this.host. As it says here, it gets various details about the
02:12Silverlight application host, the host control on the page. Through this you can
02:17also gain information about your browser.
02:20You can find out whether you are running out of browser.
02:24In Silverlight 3, you can pull this Silverlight application out of browser and
02:29run it on your desktop.
02:30This property let's you know whether the Silverlight application is running
02:33out of the browser.
02:34Show you how I can use the Application class.
02:39I'm going to put a breakpoint right here by pressing F9. I'm going to press F5
02:42to run the application.
02:45It hit my breakpoint and I'm going to use a window in Visual Studio called the Watch window.
02:49I can go down to the Watch window and I can type in any code values I want and
02:54it'll show me the results inside the Watch window.
02:56So, I'm going to type in this.host and hit Enter.
03:01And now I can drill down and find out more information like the background color
03:05is currently set in the host, the content of that control, the source, I see
03:11that this is coming from a file called SilverlightApplication8.xap.
03:16And that is just a brief tour of what you can do with the Application class.
Collapse this transcript
Reporting incremental progress with a loading screen
00:00The first thing that your user sees in a Silverlight application is the loader screen.
00:04If you are lucky and have crafted a Silverlight application that has a small
00:07payload, it'll be quick to download.
00:10In that case, the user will hardly even know the application is downloading.
00:13If on the other hand you have a large XAP file, your user will have to wait.
00:18Silverlight automatically shows a progress screen if your application takes more
00:21than one half second to download.
00:23In the Flash world, it's typical to create custom loaders, and Silverlight
00:26supports this capability too.
00:28For this demo, I am going to use Visual Studio.
00:30If you're following along with the Exercise Files, you're going to want to open
00:33the SplashScreen solution.
00:34This Solution has two projects.
00:36It has a companion website and it has the Silverlight application.
00:40When testing SplashScreens, it's helpful to have a real website, instead of
00:44using the auto-generated HTML page in the Silverlight application.
00:47The Silverlight application itself is our PixelSmith application.
00:51Let's see what the application looks like when we run it the first time. Press F5.
00:58You may see a SplashScreen.
00:59In this case, we're seeing in about a one second SplashScreen, then you see the
01:02main UI of the application.
01:04That default screen was provided by Silverlight.
01:07When you're testing a Silverlight SplashScreen, there are two techniques that
01:10can help you see more details on that SplashScreen.
01:13Let me show you the first one.
01:14Go to your App.xaml file and double-click on the App.xaml.cs file.
01:21Scroll down to the Application_Startup and comment out the line of code that
01:25says it's the RootVisual for this application.
01:28Now when you run the application, the SplashScreen will run, but it will never
01:32load the main UI of the application.
01:33This gives you a chance to inspect the SplashScreen in more detail.
01:40At this point, it should have loaded the application, but as you can see,
01:43it leaves the SplashScreen on the screen.
01:45A more real world approach is to make your application bigger.
01:49So, let's go back in and uncomment this line.
01:53And then go to the SplashScreen and add a huge file.
01:56By adding a huge file, we're going to force the download to take longer.
01:59Let's go and add that file.
02:00Add > Existing Item, and then I am going to go look at my desktop.
02:05I have a file down here called BigMovie.avi.
02:08This is a file on my computer.
02:10It's not one that is included in the exercise files.
02:13You need to find a large file on your computer to substitute.
02:16Click Add and then next, I am going to click on the AVI file and choose
02:21Build Action > Content.
02:25That puts it into the XAP file.
02:27And then I am going to press F5 to run the application and it's going to take a
02:31while to do the compile and then run the application.
02:34So we're just going to skip to the end of the compile process here.
02:38And now you see the loader screen, and it's taking about five seconds for the
02:43loader screen to run.
02:44And there is our PixelSmith application.
02:46Subsequent runs of this will be faster, because Visual Studio can cache some of
02:50the files used during the compile process.
02:52Up to this point, we've seen the default behavior.
02:54Now what we want to do is change the SplashScreen to one of our own.
02:58I'll close the browser and then I am going to go to the website.
03:01It's important that your SplashScreen goes in the website, not in your
03:05Silverlight application.
03:07Your Silverlight application is going to be downloading, so you cannot have any
03:11files in it that you're going to use as your SplashScreen.
03:13So what you typically do is you create a standalone XAML file in the website.
03:18I have already got a couple of created here.
03:19I have got one called OrangeSplashScreen and the BetterSplashScreen.xaml.
03:23We'll start with the OrangeSplashScreen.
03:25I need to find my host page.
03:27In this case, it's the SplashScreenTestPage.html. Double-click on it to load it
03:31into the editor, and scroll down to the Silverlight control, right here.
03:38I am going to add a parameter here.
03:43The name of the parameter is lowercase splashScreenSource, and the value will
03:53be the name of my XAML file.
03:55So, I'll copy the XAML file name and paste it in here.
04:01So, I'll type in = "" and then close this tag. All right!
04:08Let's run the application now and see what happens.
04:14There is our custom SplashScreen.
04:17I suspect you're sitting there going, "I thought it would be more
04:20interesting than that!"
04:21Well, don't fear. We'll make it look nicer.
04:23In fact, we'll even have it update with the progress report.
04:26To do that, I am going to use this parameter.
04:29I have already typed this parameter in.
04:30This says, if there is a progress report coming from the download, this is the
04:36JavaScript code to run.
04:38If I move up my editor, you see that I have this function called
04:40onSourceDownloadProgressChanged.
04:43In the first three lines, I am going to uncomment this by doing a
04:47Ctrl+K, Ctrl+U. I am saying, go to the web page, find something called progressTextBlock.
04:54Set its text to the word 'Loading' and then go to the eventArgs, which I get
04:59from the downloading in progress, find out what the numerical value is.
05:03Multiply by 100 and then add the percent sign.
05:05So I'll go over to my OrangeSplashScreen, put in the progressTextBlock, and run
05:16the application one more time. That's nice!
05:21I now have an OrangeSplashScreen with progress reports.
05:24You're probably going to want to brand your UI with your company logo though or some other UIs.
05:28So, let's go look at our better source.
05:30I have this one called BetterSplashScreen.
05:32By the way, I didn't mention this yet.
05:34You're not allowed to use the user control as the root element for
05:38your SplashScreens.
05:39I pick StackPanel for this one and for the OrangeSplashScreen I had Grid.
05:44Here I am putting a StackPanel, a TextBlock, another TextBlock with the same
05:49name ProgressTextBlock, a Canvas and then a Rectangle, a second Rectangle.
05:57The name of this is ProgressBar, and then down here I have a RenderTransform on
06:02the Rectangle and the name of that is ProgressBarTransform.
06:06Now let's go look at my HTML page, and uncomment this line of code.
06:10This says to go find the ProgressBarTransform and change its ScaleX property
06:15based on the eventArgs process and 250 comes from the fact that I have made a
06:20250 pixel wide rectangle.
06:22So, I am multiplying it by that factor.
06:25I need to one more thing. I need to go down here and change this to
06:30say BetterSplashScreen.
06:31Now I'll press F5 to run.
06:37And as you can see, I am getting the loading statistics at the TextBlock level.
06:43The rectangle is resizing itself, and there is our company logo.
06:47So what you have seen is you can take control of the download process and show
06:50your own UI instead of the default.
Collapse this transcript
Storing data on the client
00:00Do you need to store data for your SilverLight application?
00:03If so, you have to decide where to store it.
00:05One common scenario when storing data on the server is to use a web service to
00:08shuttle to data back to the server.
00:10If you are storing the data on the client computer then, you have two options.
00:14If you can get the user's explicit permission, via the Open File dialog or that
00:18Save File dialog, SilverLight can write to local storage just like any other
00:23desktop application.
00:24At times however, applications write information to the local drive without
00:27interrupting the user with a file prompt.
00:29A classic example of this is reading and writing user configuration files.
00:34The application developer wants to write these files as an aid to making a
00:37better user interface.
00:39But granting Web applications unfettered access to the file system is sure to
00:42put your computer in jeopardy.
00:44To solve this dilemma SilverLight uses isolated storage.
00:46So to summarize, you can store in the local computer either directly to the
00:51local hard drive, with the users' permissions or you can store to the hard drive
00:54without the users' permissions, but you can only use isolated storage.
00:57I am going to show you demos of both.
00:59I am in Visual Studio.
01:01If you're following along with the Exercise Files, you're going to want to open
01:04the StoringDataOnClient solution and then you're going to want to double-click
01:08on MainPage.xaml.cs.
01:12I have three buttons I am going to use in this demo.
01:14I should probably show you what the user interface looks like.
01:16I'll press F5 and click on the File section and there is the buttons.
01:25I can save to isolated storage, I can read from isolated storage. When I save to
01:30local hard drive, I need the user's explicit permission. You are going to see a
01:33dialog pop-up and then they can choose where to save the file. Click on Cancel
01:38and I am going to close the browser.
01:42On this button here, SaveButton_Click, I am going to save to isolated storage.
01:46The way to think about isolated storage is it's a virtual file system.
01:49It's managed by the .NET framework.
01:52You ask the virtual file system to give you your store.
01:55It determines which store you get, but you do not know where the store is.
01:59The scope with the store is based on the URI of your application.
02:04So, that means that every Silverlight application gets its own isolated store.
02:08Once you have your isolated store, here's the line of code to do that.
02:12Declare a variable, call the static method, call this static method on the
02:16isolated storage file called GetUserStoreForApplication.
02:19This gets you your unique store, without giving you a clue of where it really
02:23is on the hard drive.
02:23It's also deliberately obstructed, so that other applications can't find it.
02:28Then I can start using the virtual file system that's part of the isolated store.
02:31I can say something like isoStore.DirectoryExists.
02:33So, I can see if this folder exists.
02:37I can check to see if a file exists.
02:39Is there a file called ourfile?
02:41If not I can create a file.
02:43There are lots of methods inside this isoStore class.
02:46Let's look at a few of them.
02:49DeleteFile, GetFileNames for a directory.
02:53Here's one other issue with the isolated store.
02:55By default, you only get a small limited size for the isolated store.
03:00If you are going to save a file and you are going to go over that limit.
03:03Silvelight can prompt the user to increase the quota.
03:06You ask for a new size, the usual, you'll see a dialog and if they approve, then
03:09you get more stories based for your application.
03:11Remember isolated store never interrupts the user to ask their permission in
03:15the normal saving and reading of the files, only when you need to increase your quota size.
03:21One of my favorite features of the isolated store is it's got built-in support
03:24for application settings.
03:25You can store a name, value of pairs in the ApplicationSettings property.
03:29You provide a name here and then you provide the value.
03:33So, what I am going to do is I am going to go out to my artboard and determine
03:36how many children, how many ellipses and how many rectangles it has, get its
03:41count and store that in isolated store.
03:44When you click on the OpenButton method, I am going to go in and read the
03:47settings using the same key.
03:49The shapeCount down here, it was shapeCount up here, and we are going to go
03:54in and cast it to a string and then store it in a text box I have on my user interface.
03:59This is very handy for storing application data, user specific information.
04:03If you also want to backup things like form data before you send it to the
04:06server, that will be a great use for the CreateFile.
04:10Here you could store more sophisticated data than just a simple string/value pair.
04:14Let's see what this looks like.
04:16I will run the application.
04:19I have a builder. What have I done wrong?
04:20I am going to double-click on this item to see -- Oh!
04:23Yes, I was showing you earlier that you could use ISO store, I was showing you
04:26some of the methods. I never deleted that code.
04:28So, I am going to go ahead and delete it, and then I'll press F5 to start again.
04:32I'll add a few ellipses, a couple of rectangles, I'll go to the File section
04:41and say Save to ISO and then I'll click on the Open from ISO and this text
04:46block down here shows that there are seven children on the artboard. Six of those are shapes.
04:51In the last demo, I'm going to save this information here to the hard drive in a text file.
04:57Let me show you what this looks like.
04:59Save to local. Because I am saving outside of isolated storage we have to prompt the user.
05:05I've set up a filter. I'll show you how to do this in a minute.
05:07I am going to type in shapeForDemo.ps and then click on Save.
05:14I am going to save this on my desktop.
05:18Now I am going to open up Notepad and then choose to open that file.
05:22File > Open, Desktop, change my filter to All Files and there is my shapesFor Demo, click Open.
05:33So you see what my code did is it walks through the canvas, found all the
05:38ellipses, and wrote out their position on the canvas.
05:44Let's go see the code.
05:50Here's the saveToLocalButton.
05:52I need to declare an instance of the Save File dialog, then I set the filter to
05:56show all files that end with .ps.
05:58So, the way this filter reads is everything on the right side of the pipe is
06:03what sent to the file system for filtering purposes.
06:06Everything on the left-side of the pipe is what the user sees.
06:08So, they see the word PixelSmith Files.
06:12Then I open the dialog by calling the ShowDialog.
06:14I checked to see that the user clicked on the OK button by testing for true.
06:19And then I used a normal .NET procedures for working with files.
06:22I call OpenFile. I store that in a stream.
06:25I create a writer to make it easy for me to write into the stream and I pass it
06:29to the stream reference.
06:31Once I have the writer, I walk through all the children in the artboard, get to
06:36each one in turn, check to see what its type is.
06:39If it's an ellipse, then I write into the file, Ellipse, the Canvas left position
06:47and the Canvas top position of the Ellipse.
06:50So, it will say Ellipse {20}, {30}.
06:53Then if the type is rectangle, I do the same thing.
06:55The only difference is I say Rectangle here.
06:57I am nearing the end of this movie.
07:00I would like to point out there's also an Open File dialog, which does the opposite.
07:04It allows the user to choose a file to open and then you can bring it into your
07:07application or upload to the server or whatever you need to do.
07:10In summary I would like to repeat something I said earlier in the movie.
07:14You can store in the local file system as long as you get your user's permission.
07:18If you don't have this permission, you can store only in isolated storage, which
07:22offers a small but safe location for your data.
Collapse this transcript
13. Managing Expression Blend Assets
Exploring the Expression Blend asset pane
00:00I find Expression Blend useful for a lot of design tasks.
00:03As you have seen in other movies in this course, you can make short work of
00:06editing animations, applying brushes or manipulating control templates.
00:11In this movie, I will show you another useful tool available in Blend, the Assets panel.
00:15The Assets panel has all of the controls, styles, media, behaviors, and effects
00:20that you could draw on the art board.
00:22If you are following along in the Exercise Files, you are going to want to open
00:25the ExploringAssets solution.
00:27This solution contains two projects, ContainsAssets and ExploringAssets.
00:31I am going to start with ExploringAssets and double-click on MainPage.xaml.
00:37Now that I have a designer open, I am going to go to the Assets tab. There are a
00:41number of sections in this tab.
00:43We will start by looking in Project.
00:45In Project, it lists all of the available items that I can drag to my artboard.
00:49Currently there is nothing useful in here.
00:51In the second section, I've got all of the controls that are available.
00:55This is similar to going to the Assets button on the tool panel, and adding a
00:58single button, the difference is you can see multiple controls inside the Asset panel.
01:02This means you can explore.
01:03Let us look at this one called BusyIndicator.
01:05This is from the Silverlight toolkit. We drag this over.
01:09This is a great tool for letting your user know that you are going to do some
01:12work for a few seconds.
01:13It has a property called IsBusy.
01:15Watch what happens when I select the IsBusy.
01:18You get a Please wait message.
01:19And then when you are done doing your task, you set IsBusy to false, and it is
01:23hidden. The user will not see the BusyIndicator when it is not active.
01:27For another interesting one, how about Rating?
01:32This gives you some stars.
01:33You can set a value, say 0.6, and it fills the number of stars based on that percentage.
01:41Let me add a button.
01:46If you go to the Effects section, there are two built-in effects.
01:48I have more details about effects in other movies in the course.
01:52But I want to show you how easy it is to add items using the Assets pane.
01:55I can take this BlurEffect and just drag it over and drop it on an item in my artboard.
02:00It instantly applies the pixel effect.
02:02Moving on, I would like to move back to the Projects and open the
02:06ContainsAssets project.
02:09And then I am going to open the MainPage.xaml file.
02:14See if you can note any difference this time when I open the Assets pane.
02:18You see the number 7 there in Project?
02:21That means that inside my project, I have a style, I have some UserControls, I
02:28have some pictures, they are available to my application.
02:31When I move to the Assets pane, Blend knows that it will show them to me.
02:35Let us open Project and see what we have.
02:38Well I have a picture called Curls.png, which I can drag over to create an image.
02:42Ctrl+Z to undo.
02:44I have my own custom behavior.
02:47I have a UserControl called WaterBackground.
02:49Let me zoom out a little bit on my artboard.
02:52I am going to double click on WaterBackground to put in this UserControl.
02:56It is easy to think of UserControls as only input devices, but UserControls have
03:01many other functions.
03:02For instance, here I am using it as the background for this window.
03:05Then I have got my Octopod here, which is another UserControl, which I can drag over.
03:10This is a more whimsical user interface than you typically see in a
03:13business application.
03:14But these are UserControls.
03:16They have properties.
03:17You can set the properties in the panel, they can be animated, they can have
03:21special effects applied to them.
03:22Everything you have learnt about other UserControls can be applied to
03:25these Octopods too.
03:27I am going to move this Octopod behind the other one.
03:31Here is another one of my assets, the BringToFrontBehaviour.
03:34I have another movie on behaviors.
03:36Behaviors allow me to add functionality to items.
03:38I am going to drag the BringToFrontBehaviour to the artboard.
03:41Then if you look down in my Objects and Timeline, you see it says
03:44BringToFrontBehaviour is available.
03:45When I run the application, you will get to see what the
03:48BringToFrontBehaviour accomplishes.
03:50First though, I need to make sure I have the right project set as the startup. I do.
03:56And I believe my MainPage is the startup. Press F5. Can you see that?
04:06It is a bit subtle.
04:08When I click on this Octopod, it moves to the front. When I click on the second
04:11Octopod, it moves to the front.
04:12The key here is that the behavior is applying this, not my Octopod classes.
04:17They do not know how to do this on their own.
04:21There is lots of more to learn about the Assets pane. I cover that in other
04:24movies in this chapter.
Collapse this transcript
Adding behaviors to elements with Blend Behaviors
00:00Behaviors are a simple way to take any existing item in the Expression Blend artboard
00:04and modify it so it behaves in a new way.
00:07For example, think about the rectangle class.
00:09It is a shape, so it has properties like width, but it does not have a lot of
00:12behaviors built into it.
00:14If you want to enable a mouse drag on the rectangle, you would typically write
00:18code to detect mouse events, then change the position of the rectangle in code.
00:22Writing this type of code is not something that your average designer wants to do.
00:26To mitigate this problem and make the design team and the code team happy,
00:30Microsoft created behaviors.
00:32They permit a developer to create an action that is decoupled from the object.
00:37The developer envisions a generalized action and codes it into a .NET class.
00:42Then the Expression Blend makes it easy to find the behaviors and add them to existing items.
00:48For today's demo, I am going to use Expression Blend.
00:50If you are following along with the Exercise Files, you are going to want to
00:53open the AddingBehavior solution and then double-click on MainPage.xaml.
00:57I am going to mouse scroll out and center my artboard.
01:02I have several ellipses in my artboard but I want to add MouseDragBehavior to them.
01:07Currently they are not draggable on the screen.
01:09To do this, I am going to go to the Assets tab and then click on Behaviors.
01:13Then I am going to find the MouseDragElementBehavior and drag it to my ellipse.
01:20As soon as I let go of the mouse button, you will see inside the Objects and
01:23Timeline, underneath the Ellipse, the behavior has been added.
01:27If there are any custom properties that you can set on this behavior, they will
01:30be listed up here in the Property panel.
01:31Let me do the same thing for the other two ellipses, one for the blue
01:35ellipse, and one for the red ellipse, and then I am going to press F5 to run the application.
01:43Now I should be able to pick up the red ellipse and move it to a new location.
01:47I cannot drag these two rectangles however because they do not have the behavior.
01:51In previous movies, you might have noticed that you can click on the Add Ellipse
01:55or Add Rectangle, and then be able to move these items around on the screen.
02:00The reason this works is I programmatically added the behaviors in the code behind page.
02:05Let us see how that is done.
02:06I am going to close the application, return to Expression Blend, find my C# file,
02:12MainPage.xaml.cs, and double click on it.
02:15I will then scroll down to line 56. Here I am declaring an instance of the
02:21MouseDragBehavior, and storing it as a variable, and then I am calling its
02:25Attach method and saying apply this Behavior to this shape, which is either a
02:29Rectangle or an Ellipse.
02:32You can write your own Custom Behaviors, I will show you how to do that in
02:35another movie in this chapter.
02:36There are other developers out there who have written custom behaviors. I am
02:39going to show you a couple of their examples.
02:41I am going to start by looking at Pete Blois's Explode behavior.
02:44Pete works on the Expression Blend team.
02:47You can add his Explode behavior to any panel, and then when you click on it, it explodes.
02:52How cool is that?
02:57You can also reset, put it back where it belongs.
03:01If you want to explore more of these behaviors, you can find them on the
03:04Microsoft Expression Community site.
03:06I have navigated to that site.
03:08If you click here on this link, Behaviors, you will see there is about 50
03:12different free behaviors for you to try out in your own projects.
03:16For my last demo, I am going to use behaviors that are a lot of fun.
03:18They are available here at this web site, Physics Helper for Silverlight.
03:22I have already installed them on my machine.
03:24Let me switch back to MainPage.xaml, and then click on the Assets pane, open
03:30Behaviors and then click on the Physics subsection.
03:33The idea behind these behaviors is that I can turn any canvas in my application
03:37into a physical world.
03:39To do that, select my canvas, find the PhysicsControllerBehavior, and drag that
03:43over and drop it on my canvas.
03:46Now I can set some properties. How much Gravity strength do I want?
03:49I think today I am feeling like 10.
03:52Do I want to be able to drag the items around on the screen?
03:55If so, I choose MousePickEnabled.
03:58Once I have the physical canvas, I can choose which elements within the canvas
04:02respond to the physical world behavior.
04:05This is done by using the PhysicsObjectBehavior.
04:07I will drag the PhysicsObjectBehavior onto the three rectangles, one, two, and three.
04:15Select the Behavior in the Objects and Timeline, then you can set things like
04:19their Mass amount, and Inertia, and other properties.
04:23Be sure and choose this long rectangle, and set its PhysicsObjectBehavior to Static.
04:28That way it will not be affected by gravity.
04:30Let us run the application and see the difference.
04:35Gravity is taking effect on the two rectangles.
04:41They bounced off the static rectangle.
04:43I can pick them up with my mouse.
04:45I get a little rubberband item on it.
04:46I can knock the other item around, see if I can knock it off the screen. Hey, great job.
04:51And then what should I do with this one?
04:53Let us get rid of it.
04:55Behaviors are really cool, and they make it so simple for designers to add the
04:59functionality that they need in Expression Blend.
Collapse this transcript
Creating custom behaviors
00:00We have seen in other movies how you can download custom behaviors that
00:03other people have built.
00:04In this movie, I will show you how to create your own custom behaviors.
00:07If you are following along with the Exercise Files, you are going to want
00:09to open the CustomBehavior solution and then double click on the
00:13MainPage.xaml file.
00:16In this example, I have several items on my work area and I am going to add a
00:21behavior called the ChangeZIndexBehavior.
00:24I think it will be nice if I could create a behavior, doing to make it very
00:26simple for a designer to move an item to the front without having to write any code.
00:31To do that, you need to create a class.
00:33I have created a class called ChangeZIndexBehavior.cs.
00:37Let's double click on that file.
00:39And then in Expression Blend, you can press F4 to move the tool panels away.
00:42We start by creating the class and deriving from Behavior.
00:47This is a generic class and you specify which elements you can apply this behavior to.
00:52In my case, it will be any panel type.
00:54When the behavior is attached to an object, it will run this code,
00:57the OnAttached Event.
00:59What I am doing in there is I am wiring up an event procedure saying
01:02AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded).
01:07I will show you that code in just a second.
01:08I am also detaching that event procedure in the OnDetaching method.
01:12This is called when the behavior is being detached from its AssociatedObject.
01:15The AssociatedObject would be if I drag the behavior onto our rectangle that is
01:18the AssociatedObject.
01:19From my example, I am going to drag it onto a panel.
01:23When my behavior loads, it is going to walk through all the children in the
01:27panel and wire up a MouseEvent procedure.
01:30When you MouseLeftButtonUp, I am going to have you call this code here.
01:33I am going to check a custom property that I wrote called BringToFront.
01:41If BringToFront is true, then I am going to call a method called MoveToFront.
01:45MoveToFront gets to panel, calls a method called GetHighestZIndex, which walks
01:51through all the children and sees which one has the highest ZIndex.
01:53ZIndex indicates which item is higher in the Z axis.
01:56So, if I have two rectangles, and one has a Z axis of ten, and one has a Z axis
02:01of twenty, and they overlap, the one with Z axis of twenty will be shown in
02:05front of the one with Z axis of ten.
02:07My code here goes through the items, analyzes which one has the highest one,
02:10and swaps their order.
02:11I also have a method called MoveToback that does the opposite.
02:14All right, enough about this code.
02:16Let's see how it works in action.
02:17I am going to go over to my main page, and I will compile my application, press
02:24F4 to bring back my tool panels, go to my Assets section, look in my project and
02:29see if there is a ChangeZIndexBehavior, and there is.
02:30I will drag this over and drop it on my Canvas panel, and now I get to choose
02:37BringToFront as my custom property that I wrote.
02:40Run the application to test, and now I'll click on the Octopod, now I'll click on
02:47the Image, and I'll click on these two Octopods back and forth. Maybe on this book.
02:52It brings it to the front.
02:53Remember the key part of this is the designer who is using Blend does not have
02:58to know anything about code.
03:00They just drag the behavior to the panel, and set the property, and they are done.
03:03Let me show you what happens if I uncheck the BringToFront.
03:08Run the same application, click on the object, and now it gets sent to the back of the stack.
03:16This behavior is pretty simple.
03:18You can write as complex a behavior as you want, and Expression Blend makes it
03:21really easy for designers to add to the project.
Collapse this transcript
Using the built-in shader effects
00:00Shader effects energized the PC gaming industry by making it easy to apply high
00:04speed transformative effects to portions of the screen.
00:08Traditional shaders are implemented in the graphic processor unit or GPU on your computer.
00:13Essentially a pixel shader is an algorithm that modifies one pixel.
00:18The GPU applies that algorithm to thousands of pixels in parallel, which
00:21makes it amazingly fast.
00:23The types of effects you can apply are enormous.
00:25Ripple effects, color corrections, fractal generators or grayscale are just some
00:31of the possibilities.
00:32Both Silverlight and WPF have shaders.
00:35The difference is that WPF exploits the GPU whereas Silverlight renders
00:39the shader in the CPU.
00:41Even though Silverlight does not use the GPU in their implementation, it is
00:45still faster than other mechanisms available.
00:47I am going to use Expression Blend for this demo.
00:49If you are following along, you are going to want to open the Shaders solution
00:54and then double click on the MainPage.xaml file.
00:57I am going to use my mouse wheel to move out, and then I am going to drag two
01:00images to my work area.
01:02Click on Assets, clicked on Project and then I am going to drag Curls over here,
01:08press V, and then resize the image, and then Ctrl+C to copy it, and Ctrl+V to
01:14paste it, and then drag the image down.
01:16It's very easy to apply effects in Blend.
01:20You go to the Assets pane, click on the Effects section, and use one of the
01:23two built in effects.
01:25The BlurEffect, you drag it over, and it blurs the pixels.
01:29If you want to change the amount of the blur, modify the Radius value.
01:34The second effect is called the DropShadowEffect.
01:36I will drag that over and drop it on this picture.
01:39It has more properties.
01:40I will make the ShadowDepth a little bit deeper.
01:43You can change its opacity or color.
01:45You can change the direction of the shadow, and you can also change its BlurRadius.
01:50It is also possible to add effects in code.
01:55Let me delete these two items, and I will show you how to do that.
01:59Click on Projects, and open your UserControl section, and then double click on
02:05ImageUserControl.xaml.cs.
02:10Scroll to the bottom of the window. In the blurButton_Click procedure, I am
02:16going to add an effect, var effect = new Blur, set the Radius value equal to 5,
02:32and then I am going to apply the effect to the picture.
02:45I tried to build the application.
02:46It's telling me that it's missing a namespace.
02:48I will scroll to the top and add a using statement.
02:53Using statements simplify the body of my code.
02:55So, I'll say using System.Windows. Media.Effects and then a semicolon.
03:02I think I am ready to try running this.
03:06Press F5, click on the Images section, add a couple of pictures, select one of
03:15the pictures, and then click Add Blur.
03:16Let me add one more effect.
03:20I will copy the code.
03:21Place it in my other procedure.
03:24This time I am going to say I want the DropShadowEffect. Change the ShadowDepth.
03:29Now run the application and see what this one looks like.
03:32Press F5, click on the Image section, add an Image Element, there is the blur
03:41that I added before, and now I can add a drop shadow as well.
03:44Once again, you see that Blend makes it really easy to add shaders to your application.
03:49If you are surprised that there are only two shaders, don't worry.
03:52You can create your own custom shaders, and that is a topic for another movie.
Collapse this transcript
Creating custom shaders
00:00In Silverlight, you can create your own custom shaders.
00:02In this demo, I am going to be using several tools. I am going to be using
00:06Visual Studio, I am going to be using Expreession Blend, and I am going to be
00:08using a shader utility called Shazzam.
00:11The shader utility is available on the internet at shazzam-tool.com.
00:17I wrote this utility to make it easier to generate the shader code that you need
00:20in your Silverlight application.
00:22All you need to do is go to this web site, click on Installing Shazzam, then
00:26click on this link, and then click the Install button.
00:29Once it is installed, you can run it from your computer.
00:32It's a click once application, so it will check to see if there are any updates.
00:38In order to make your own custom shader, you need to create three separate files.
00:42The first file you need to create is called an HLSL file.
00:45This is a text file that contains Direct X code.
00:48Here is a sample file.
00:49It looks sort of like C.
00:51Next, you need to generate a C# or a Visual Basic class.
00:54That is one of the things that Shazzam does for you.
00:57Let me go load a more interesting shader. I am going to choose Sample Shaders,
01:02and before I make the shader, let us make a couple of settings.
01:04Go to your Settings button and type in a namespace of your choosing and also
01:08make sure you check the Silverlight Target framework.
01:11Then go to Shader Loader and choose BandedSwirl.
01:15This is what the shader effect looks like.
01:17Let me turn off the shader for a second, Remove Shader. Here is a picture.
01:21There is different sample pictures across the top here you can look at.
01:24I will select this picture here and then I will go over to tools, and apply the shader.
01:29When I did that, Shazzam compiled this HLSL code and turned it into this C# class.
01:36For the demo, make sure you also include CustomShaders in your namespace here at the top.
01:40It generated the C# class for me, which is a .NET class that derives from the
01:44ShaderEffect class, and for every input into this shader, it created a
01:48Silverlight dependency property. Input, Center, Band, Strength, and AspectRatio.
01:53Those are all defined over here on my HLSL code.
01:56I can click over here on the Change Shader Settings and manipulate those values live.
02:01I can see what it will look like if the center point was moved. I can change
02:05the number of bands.
02:07Notice how fast shaders are. Change the strength of the shader, and so on.
02:13I have also added Animation capability so I can type in 5 seconds here and
02:17then click the Animate button, and watch the number of bands animate
02:22smoothly in and out.
02:27This comes with more than one shader, and there is plenty of samples for you to look at here.
02:30This is the Bloom shader, here is the Embossed shader, here is the Gloom shader,
02:37makes it look kind of dark and gloomy. InvertColor.
02:41Other fun ones, Magnify, Cartoon shader. It comes with about twenty shaders.
02:48But the one we are going to work with is the BandedSwirl.
02:52The next thing you need to do is you need to compile this shader.
02:57Compile the shader. That generates a ps file.
02:59This is a binary file.
03:01So, what you need to do is add a C# file and a ps file to your
03:04Silverlight project. Let's go do that.
03:06Up to tools.
03:07Click on Explore Compiled Shaders.
03:10You need to take this BandedSwirl.ps file and copy it to your project.
03:16Copy this and move over to Visual Studio.
03:20If you are following along with the Exercise Files, you are going to want
03:22to open the CustomShaders solution, and then I am going to paste that into my project.
03:27Then I am going to return back to the File Explorer, move into my C# folder,
03:32find BandedSwirl effect here, copy it, and bring it into my project.
03:40Let's look at the C# code briefly.
03:42There is one line I want you to see down here. The pixelShader source is coming
03:48from that ps file, BandedSwirl.ps.
03:51That is why you have to have both files.
03:52All right, we are ready to use this shader.
03:55We will compile the application, verify that it compiles correctly.
04:01Then I am going to switch to Expression Blend, where I have the same project loaded.
04:05Blend detects I have made some changes since I was last in here.
04:08I am going to choose Yes.
04:10I am going to double click on my MainPage.xaml and add an image here.
04:18Go to Assets > Curls.
04:21I am going to drag the Curls over here.
04:24It's too big so I am going to press V and size it down a little bit smaller.
04:30Next I am going to go over and grab the BandedSwirl effect and drag it over
04:33and drop it on my image.
04:35As you can see, it's picking up the BandedSwirls.
04:38It's doing the right thing also by adding properties to my Properties panel.
04:42Here I can change the number of bands or the center point.
04:45I can also add this programmatically.
04:48Let me show you how to do that. I am going to click on Projects, scroll up to
04:54the UsersControl folder, double click on the ImageUserControl.xaml.cs file, and
05:01then scroll down to the bottom where I have this customButton.
05:04I will copy a little bit of code from up here to save me a little bit of time.
05:15And then we are going to set a property, effect... I did something wrong.
05:20I need to change this to say BandedSwirl.
05:24Now I should be able to see bands.
05:27Set that equal to 5.
05:28And then a semicolon, and let's give this a test.
05:31Press F5 to run the application, click on Images section, add an image, drag it
05:40up here where we can see it, and then click the Add Custom Shader, and there you go.
05:44These three tools, Visual Studio, Expression Blend, and Shazzam, all make it
05:48very easy to add custom shaders to your application.
Collapse this transcript
Conclusion
Goodbye
00:00 Thanks for watching Silverlight 3 Essential Training.
00:03Now that you have learnt about the core features of Silverlight, you are ready
00:06to create your own fabulous applications.
00:09Silverlight 3 is a big platform, and has many topics not included in this series.
00:13That gives you a great opportunity to dig in and continue learning.
00:17Don't forget these movies are always available here at lynda.com.
00:21So, if you need a quick refresher session, you can come back to the site and
00:24review the topic again.
00:25Once again, I am Walt Ritscher.
00:28I write about Silverlight and Windows Presentation Foundation on my blog at
00:32blog.wpfwonderland.com.
00:36You can follow me at Twitter.com/ waltritscher or learn more about my Shazzam
00:41pixel shader utility at shazzam-tool.com. Shazzam is spelled with two Zs.
00:47I always have something cooking when it comes to Silverlight, so look me up at
00:50the next convention or invite me over to speak at your user group.
00:53There is nothing like sharing some tech talk about RIA applications with your friends.
00:57Good luck, and have a great time creating Silverlight applications.
Collapse this transcript


Suggested courses to watch next:

Silverlight 4 New Features (3h 0m)
Walt Ritscher

C# Essential Training (6h 44m)
Joe Marini


ASP.NET Essential Training (6h 24m)
David Gassner


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,755 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our