Static resources are defined inside the application cdoe and they are available to the controls as "static source of design" which means they defined on compile time by the programmer and cannot be changed on run time. On the other hand "Dynamic Resources" can be change on runt time.
To elaborate a static resource lets make a simple application in UWP. UWP stands for Universal Windows Platform which has been introduced in Windows 10 application development.
There are few properties and XAML objects ( tags ) that needs explanation.
To elaborate a static resource lets 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 shown in figure.
Open your MainPage.xaml from Solution Explorer and go to the XAML code behind XAML Designer.

Now we're going to add a button in side the Default Grid view. In order to add two buttons use following code.
Its time to add a static resource for these buttons. use following see the following code and snapshot.
After inserting the code for resource your buttons will look like ..

- <Button Content="Click Me 1" Margin="119,191,0,417" ></Button>
- <Button Content="Click Me 2" Margin="119,267,0,341" ></Button>
- <Page.Resources>
- <Style TargetType="Button" >
- <Setter Property="Background" Value="Red"></Setter>
- <Setter Property="Foreground" Value="White"></Setter>
- </Style>
- </Page.Resources>
There are few properties and XAML objects ( tags ) that needs 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 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've to mention its value as well. In this case I just mentioned it with "Red" color. - You might noticed 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 you their.
No comments:
Post a Comment