WPF:靜態、動態資源以及資源詞典
靜態資源與動態資源
我們常常會使用樣式或者控制元件模板放在Window.Resources中,比如這樣:
靜態資源與動態資源使用如下:
<Window.Resources>
<SolidColorBrush x:Key="SolidColor" Color="#FF0000" />
</Window.Resources>
<Grid>
<StackPanel>
<Button Height="40" Margin="10" Content="Button1" Click="Button_Click" />
<Button Height="40"
Margin="10"
Content="Button2"
BorderBrush="{StaticResource SolidColor}"
BorderThickness="4" />
<Button Height="40"
Margin="10"
Content="Button3"
BorderBrush="{DynamicResource SolidColor}"
BorderThickness="4" />
</StackPanel>
</Grid>
區別:動態資源是在介面中根據指令可以變化的,靜態則不會

資源詞典
當樣式多了,這個時候我們需要單獨建立一個資源去管理他,這個時候我們就有了資源詞典:
將Window.Resources中的程式碼移植過來:

這個時候還得在載入時將這個資源詞典載入過來,在app.xaml中載入

在main中也可以查詢對應的資源,如下:

結果依然可以得到:
