From the course: Microsoft XAML: 3 Type Converters and Resources

Explore the concepts of type converters

From the course: Microsoft XAML: 3 Type Converters and Resources

Start my 1-month free trial

Explore the concepts of type converters

- [Instructor] XAML is an XML variant, so it follows the basic rules of XML. The part of XML that I'm focusing on in this video is the attribute, and how the values of the attribute are resolved by the XAML compiler. The steps I'll outline are accurate for WPF and Silverlight. The process for universal Windows applications is slightly different, but the overall principles are the same. I'm working with two files during this demonstration, I have this mainwindow.XAML file, which contains my XAML, and I'm also working with this class here called rating.XAML. This is a custom user control, and I'll talk more about that in a second. So if you over at my XAML, you'll see that I have a grid that contains the text block, and it also contains an instance of my ratings control. Now the idea of this rating control is that it allows the users to rate something from one star to five stars. When I build the application, this XAML file is processed by the XAML parser. It's the parser's job to turn this XML into a binary representation of the XML text. This is similar to what happens to C sharp code when compiled. It's turned into a binary file containing intermediate language, or IL. That IL is eventually processed by the dot net runtime, which instantiates the types. For WPF XAML, the binary file that is created is called the binary XAML file, also known as the BAML file, B-A-M-L. That BAML file is processed at runtime to build the visual tree from my application UI. And now we get to the crux of the problem encountered during the compile by the XAML parser. Here's the issue: look at line 16. I have a text block, and I have a text property, so the parser's job is to verify that there's a type called text block, then to verify that there's a text property on that, then to take this value here and assign it to the text property, and store that in the binary XAML file, the BAML file. Now, to look at this in further depth, let's look at my class that I created. Over here in the C sharp file, you can see that I have several dot net properties. Now this is a UI control, I should have created these as dependency properties, but for simplicity I just created them as dot net properties. So the first one on line 30 is a public string named tip. Now I'll go over here and see that I set that where tip equal for the demo. The crux of the problem is this value is a string. It's always going to be a string, and so there has to be some way of turning this string into the correct type, what is over here on this side. And this first example, tip is of type string, so there's no conversion necessary, but we do start running into issues when we get to number of stars, and value and the rest of these items here. So if you look at number of stars, we see that that is of type int. If you were writing C sharp code and dealing with this, and you had a string, and you wanted to turn it into an integer, you would use int.parse. The way it's handled in XAML is, there needs to be a type converter in place. It's a type converter's job is to take the string, and turn it into the correct dot net type. The good news is, all the primitive types and the classic dot net types, there's already type converters created for you by Microsoft. So in my case, there is an int here, this value of five, that is a string, there is a type converter that will turn that into an int. There's also one for value, that's of type double. On line 19 in my XAML file, this is allowed. Now let's drill down in these two properties, I've got a star background and a star outline, and they're both of type brush. Now these are not primitive types, they are part of the WPF framework. The good news is Microsoft's also created a type converter for brush, so when you see a value like red here, that gets converted into a solid-color brush of red color. You might recall that if you go here and press the space bar, you'll see a drop-down list that shows that these are what are called the known colors, those known colors will get turned into the correct solid color brush. Same thing happens here for the star outline. Now let's look at this last example here. So it's called star border, and I am using a class that I created that doesn't have a type converter. I called it borderline, and the idea's I want some extra space around the outside of my stars, so I created a borderline class and I have a top, this is the border around the top, bottom, left, and right. And I set that as the data type for this property here. Now if I come over to my XAML, you'll see that when I hover over the blue squiggle it says the type converter for borderline does not support converting from a string. So that could mean one of two things. That means that I wrote my type converter wrong and it doesn't know how to take a string and turn it into a borderline, or what's really wrong here is that I didn't create a type converter, I didn't associate a type converter with my borderline class, and since I didn't do that extra step, the XAML parser doesn't know how to deal with it. It doesn't know how to turn this string value into a borderline. I'll need to write a type converter, and I'll show you how to do that in this chapter. The next thing I want to talk about though is how to use some basic type converters.

Contents