[WPF] Static resources, dynamic resources, and wpfstaticresource
I. Overview
This demonstration introduces the basic usage of WPF static and dynamic resources, and makes a simple comparison between them.
StaticResource refers to the one-time usage of resources when the program loads the memory, and the resource is no longer accessed. Dynamic Resource) it means that resources are accessed during the running of the program.
Ii. Define and use resources
<Window x: Class = "Demo010.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" Title = "MainWindow" Height = "149" Width = "296"> <Window. resources> <TextBlock x: Key = "Res1" Text = ""/> <TextBlock x: key = "Res2" Text = "moon in the sea"/> </Window. resources> <StackPanel> <Button Margin = "5, 5, 5, 0" Content = "{StaticResource Res1}"/> <Button Margin = "5, 5, 5, 0 "Content =" {DynamicResource Res2} "/> <Button Margin =" 5, 5, 0 "Content =" Update "Click =" UpdateRes_Click "/> </StackPanel> </Window>
Ii. update resource content
Private void UpdateRes_Click (object sender, RoutedEventArgs e) {this. resources ["Res1"] = new TextBlock () {Text = ""}; this. resources ["Res2"] = new TextBlock () {Text = ""};}
Iii. Changes Before and After resource update