Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Microsoft Silverlight 5 is a rich application framework for creating high-performance, cross-platform desktop and mobile applications. In this course, author Walt Ritscher demonstrates how to build a variety of applications in Silverlight, with particular focus on building compelling business applications and delivering premium video and audio content. Developers will work with the C# programming language and Visual Studio Professional, as well as Expression Blend, a tool that simplifies creation of the interactive user interfaces expected in modern-day applications.
When you investigate a visual element like the button or list box, you see a lot of properties. You might be surprised to learn that most of these properties in Silverlight elements rely on specially UI system known as the Dependency Property System. This system was first conceived and created by the WPF team. This new system enhances the underlying .NET framework and permits elements to plug into useful services like Animation, data binding or styles. It is also important for Silverlight to be conservative with critical resources like memory. To provide these services, Silverlight relies on the Dependency System.
There are two major parts of the Dependency system. In this movie, I will discuss the Dependency property. There is another movie in this chapter that covers Attached Properties. A core piece of the Dependency System is something called the Dependency Property. When you create a user control, you can choose whether to expose .NET properties or Dependency Properties. The correct approach is to do both. First, create a Dependency Property, so that your property can partake in Silverlight services like data binding. Also create a .NET property so that you will find it easy to set and read property values in code.
I'll show you how to write custom dependency properties in the next movie. As I said earlier, most of the properties on an element are dependency properties. The Buttons FontSize property is a good example. You can set the font size directly in code or a XAML, or the value of the font size could be set by one of the Silverlight services. Let me go to Visual Studio and show you how to set a property in code. I'm inside the DiggingIntoDependencyProperties project. And I'm going to go to this file called SettingDPInCode. Then I'm going to press F7 to switch to the code window.
These first two lines of code show how use the normal .NET properties. This is a .NET property called FontSize. I'm assigning a value of 40 to this FontSize. On line 26, I'm reading the value of the FontSize and assigning it to a variable. These are also exposed as Dependency properties, so I can go in code and say SetValue and GetValue. Now this syntax in SetValue, after I've pass the value which is the name of the property that I want to change and then the value that I want to assign to that property.
And the second line calling GetValue again passing in the property that I want to read the value from and then I need to do a cast because the data comes back typed as object. In my case I want to cast it to a double. As I mentioned earlier, one of the most notable features of Dependency Properties are that many different Silverlight services might be attempting to set the Dependency Property value. Silverlight takes a look, prioritizes each service and the highest priority service wins. I've listed on this page the priority resolution.
At the bottom, is the lowest priority and at the top is the highest priority. So let's take an example. I've set the font size in local value. That means I have assigned it in my XAML or in my code behind. I have set the font size to 40, but I have an active animation running. That's changing the font size from 20 to 80 and then back to 20 again. So, if I query the button and say what is your current font size? The Dependency System is going to look at the animation, sees that it has a higher priority value than the local, sees what the current animated value is and return that value to me.
In this example I'm going to work with all the controls that are in this folder, the Controls folder and I'm going to load those into this control called OrderOfPrecedence2. Let me show you a quick example of what this looks like when it's running. I'll press F5. When I click on these buttons, up at the top of the screen, it loads one of my user controls in. The first one shows with the Default Value is for the Fill value for this rectangle. And the second example shows setting it locally in XAML, and the third one shows setting it in code and so on.
The last one shows running in Animation. So it starts out at blue which is a relatively high priority setting and then the animation takes precedence over that. Returning to Visual Studio and opening this one here DefaultValue.xaml. This is my rectangle. Notice that nowhere in my XAML am I setting the Fill value. That means that the Dependency Systems are going to return the default. In the next example, I will look at setting it locally.
Local means setting it in XAML or setting it in code. In this case, I've specified in my Rectangle, a Fill value of LightBlue. That takes precedence over the default value. The third example sets it in code. So I start off by setting it locally, again to a LightBlue color. Then in my code behind page, press F7 to get there, when I click on this button I create a SolidColorBrush and assign that to the Fill.
That is also local, but it takes precedent over the one assigned in the XAML. If I ever want to remove the locally assigned value, I have to use the ClearValue function. Setting it to null does not accomplish what I want. There is a special function called ClearValue and you basically tell the dependency system, look up the default value and reassign that. Next I will look at setting it by style that's one of the higher priority items. Now in this example I have a Rectangle with no Fill value, but I've specified the Style attribute.
And I have told to use a markup extension to go look up a style that I have defined elsewhere, which you can see right up here is called the flamingoStyle and it's setting the Fill value to Pink. So styles take precedent over default. Another example with Styles shows that I have a style set here using that same flamingoStyle but I also have set it locally. If you look up the precedent order, styles are lower priority than local values, which is why the rectangle is orange.
If you have access to the exercise files, I encourage you to spend some time going through the examples yourself, so you can get more comfortable with the priority levels available within Dependency properties.
Find answers to the most frequently asked questions about Silverlight 5 Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.