Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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 write C# code in .NET, you need to understand the type system. Because C# is a strong type language, you also need to understand type casting. Type casting is an important technique in C# and it is also necessary to understand in XAML files. First, let's look at this C# example. On line 22 in this example, I am declaring a variable of type list of double. Then on line 23, I am trying to call the DateTime.Now function which returns a date time. I am trying to assign that to the numbers variable.
This is not allowed in C# because they are not compatible types. I will get an error called Cannot implicitly convert type System.DateTime to System.Collections.Generic.List of double. Another example of illegal code is on line 29. I have declared a variable of type double, and I am trying to assign a string value to this. This is not allowed. The correct way to handle this is by using the parse method, in this case, the double.Parse method on line 32. Here I say double.Parse and I pass in the string value 150, and then I pass that parsed value over to the other side and to the variable called the weight.
At other times you use the casting operator like I am doing on line 33. The int value inside the open and close parentheses is a casting variable. Now, this also applies to XAML. I'll show you what I mean. I am inside Visual Studio and I am inside the XamlTypeConverters project. I am going to open up this MainPage.xaml and I am going to scroll down here to this section. On line 30, I have a TextBox that has a Background property. In XML, this property attribute is a string.
So this is a string value that's being passed to this property. If I press F1 while I am on this property named Background to launch the help system, I can see here that the Background property is typed as Brush, not as String. That means that there has to be a casting operation to happen. Silverlight uses something called a Type converter to do this. The Type converter takes the string and turns it into the correct type, in this case, to Brush type.
Let's look at some more examples. Here in the example where the Text property is of type string. So this is a string to string assignment, no casting necessary. This is a string to brush. This is a numeric value, over here, of the Width, but I am passing a string. So this has to go through a conversion and it's even more complicated than that because I can put suffixes on the end of this string. The cursor is an enumerated type, press F1 to take a look at this, and this is of type Cursor which is an enumerated type, so this gets passed through the enumeration type converter.
So I can type the string name but it gets turned into the enumerated value. Here's another example, Margin='245,0' this is a delimited set of numbers that get passed to the Margin type. The thing about Type conversions is they happen at compile time. So when I go up and I do Build>Solution, it's going to run this number through the type converter and statically store it in my compiled version of the application. If I want to do runtime conversions, for runtime code, I want to use something called a markup extension.
Here is the syntax for this and I do cover this in another movie. The last demo I wanted to show you is a control that I wrote on my own. This means I also have to write my own type converters. So I'll write a custom user control called the WeatherControl, here it is, very plain looking UI. And then in my Code behind, I created a property on it, WeatherBackground, that returns a Brush. This is my property, WeatherBackground. If I assign a string to this, I need to convert it to the correct brush.
So I wrote a type converter class, over here, WeatherTypeConverter, that derives from TypeConverter. And the important two functions here are CanConvertFrom, here I say, if I get a string passed into the type converter, I know how to convert that. And then this function ConvertFrom is the actual code that does the work. What I do inside this function is I get the incoming value, which is passed to me right here. That will be the string name. I store it in the string, then I have a switch statement down here that says, if I get the string Sunny, return a Yellow brush, and if I get the string Foggy, return a Gray brush and so on. Okay.
Now, we go back over to MainPage, I am instantiating an instance of my WeatherControl, I am saying WeatherBackground='Sunny', that comes in and that should give me a yellow brush. If I change this with one of the other ones, Rainy, it changes to a blue brush. Here I have shown you how type converters are called whenever you assign a value to XAML that is a non-string property.
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.