Static resources are defined inside the application code and they are available to the controls as "static source of design" which means they are defined on compile time by the programmer and cannot be changed on run time. On the other hand, "Dynamic Resources" can be changed on run time.
To elaborate a static resource, let's make a simple application in UWP. UWP stands for Universal Windows Platform which has been introduced in Windows 10 application development.
Open Visual Studio and make a Windows 10 Universal app as in the following screenshot:
Open your MainPage.xaml from Solution Explorer and go to the XAML code behind XAML Designer.
Now, we're going to add a button inside the Default GridView. In order to add two buttons use the following code:
- <Button Content="Click Me 1" Margin="119,191,0,417" ></Button>
- <Button Content="Click Me 2" Margin="119,267,0,341" ></Button>
Its time to add a static resource for these buttons. Use the following code:
- <Page.Resources>
- <Style TargetType="Button">
- <Setter Property="Background" Value="Red"></Setter> <Setter Property="Foreground" Value="White"></Setter>
- </Style>
- </Page.Resources>
After inserting the code for resource your buttons will look like the following:
There are a few properties and XAML objects (tags) that need explanation.
- Property TargetType asks us the Control type that we're going to design using this resource. In this case we're applying our resource on button. You can also apply it in any control that accept resources.
- In the tag of Setter we are using two properties
1. Property = "Background"
This feature asks us to write the the property that we're going to apply on controls. In our case we're going to apply background color on buttons, so we write background.
2. Value = "Red"
Once you write the property that will be applied to controls you need to mention its value as well. In this case I just mentioned it with "Red" color.
- You might notice that the styles that we've set for the buttons is applying to all the button that we're using on current scope of project. If you want to apply resources on specific controls you can use Controls with their names.
- If you have any problem just put your comments below the post. I'll respond to you there.