Welcome back.
Guys, in this lesson, we're going to be looking at XAML markup extensions.
Now, a lot of the time we're going to have XAML that we want to standardize.
Right?
So let's take, for instance, going back to main page.
We have different labels and there might be times we have multiple labels all over the particular control
and you'd want all of them to be the same size.
No.
If you have five labels and you set the size five times, then it would be tedious to change it five
times and then multiply that by a 50th thing to get where I'm going.
So markup extensions help us to set like a static value in the code behind and then reference that static
value.
So if we have to make a change, we made the change one time.
So let us see what that takes.
So if we go back to the code behind for our main page XAML, the first thing that we can do is define
our value that we would want.
So I'm going to see public const double font size.
Is equal to C 22.
Right.
So that is the fun size that we know we want.
And I'm going to follow a proper naming convention since it is a public or it's a property.
I'm going to make it have capital letters or start with a capital letter.
And then inside of this very class, I need to define or instead of the same file with that namespace
I went after, define another class of public class and then I'm going to see global font size.
And then the naming convention here requires that I append it with the word extension and then I'm going
to let it inherit from I am markup extension, which will then tell me that I need to implement the
interface.
So the interface implementation for that comes with a method called provide value and it takes in a
service provider injected in.
And then we have to specify whatever we want here.
So I'm just going to say return mean page.
The turn mean page.
And that's not the case here.
I mean just make sure knows or I spelt return incorrectly return main page not.
And then we would say font size.
All right.
So, in essence, when this is referenced, it just needs to return the font size to the main page control,
and then the main page would be able to access it accordingly.
So in our XAML, what we want to do, first of all, is define the namespace of top.
So we need to let it know that a new namespace is here.
So you see x email and s x amount namespace colon and then we give it some name.
So here I can see my font size, right?
And then that's going to be equal to and then I say C, R hyphen name, space, colon, and then the
name space that goes with where it was defined.
So it was defined in the new space.
My first miui up.
So that is the new space value that's provided there.
Right.
So all of that is just to wire it up.
So no or main page knows how to make reference to the font size.
Now when I want to make reference to the font size and let's use this label as an example instead of
writing the value, like we said, font size, what I'll do is make reference to one the namespace alias
here that I set.
So I'll say go to that namespace and then get me the global font size.
All right.
So you see, that was autocomplete ID for me.
I didn't have to type, that's all.
Because it's already identifying that from this namespace, I can get that value.
What is global font size?
Once again, global font size is what we define.
So I just F12 and global font size extension is the name of the class.
However, we don't need the word extension when we're making reference to it in the code.
So just by doing that, we can actually just have this bit of code anywhere that we want to use that
specific font size.
As usual, we'll go to the upsell and we change out the main page.
And when we test that, you see that or literally a showing of properties.
So once again, the point of that exercise is to show you how we can set those static values for more
global access and reach so that if you need to me the change, you only have one place to the change
and it can affect multiple controls.