Wpf資源
阿新 • • 發佈:2019-02-14
上面一個簡單的DEMO示範了資源的三種不同用法:StaticResource靜態資源,DynamicResource動態資源,資源字典中的靜態資源。
1.StaticResource靜態資源
<Window.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"></SolidColorBrush>
<sys:String x:Key="textContent">文字內容(StaticResource)</sys:String>
</Window.Resources>
<StackPanel Margin="5">
<TextBlock Text="{StaticResource textContent}" Margin="5"></TextBlock>
<Button Background="{StaticResource RedBrush}" Margin="5" FontSize="14" Content="使用Static Resource,無法動態改變樣式"/>
</StackPanel>
2.DynamicResource動態資源
<Window.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"></SolidColorBrush>
<sys:String x:Key="textContent">文字內容(StaticResource)</sys:String>
</Window.Resources>
<StackPanel Margin="5">
<TextBlock Text="{StaticResource textContent}" Margin="5"></TextBlock>
<Button Background="{StaticResource RedBrush}" Margin="5" FontSize="14" Content="使用Static Resource,無法動態改變樣式"/>
<Button Background="{DynamicResource RedBrush}" Margin="5" FontSize="14" Content="使用Dynamic Resource,點選改變樣式" Click="On_Click"/>
private void On_Click(object sender, RoutedEventArgs e)
{
this.Resources["RedBrush"] = new SolidColorBrush(Colors.Yellow);//改變動態資源:背景色變為黃色
}
單擊按鈕,背景色從紅色變成了黃色。
3.資源字典中的靜態資源
先新建一個資源字典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="blueBrush" Color="Blue" ></SolidColorBrush>
<FontWeight x:Key="fontWeight" >Bold</FontWeight>
</ResourceDictionary>
將資源字典合併到應用程式的字典集合,以便應用程式的其他視窗使用,在App.xaml中新增:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDic/TestResourceDic.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
使用資源字典,使用了資源字典中的blueBrush:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"></SolidColorBrush>
<sys:String x:Key="textContent">文字內容(StaticResource)</sys:String>
</Window.Resources>
<StackPanel Margin="5">
<TextBlock Text="{StaticResource textContent}" Margin="5"></TextBlock>
<Button Background="{StaticResource RedBrush}" Margin="5" FontSize="14" Content="使用Static Resource,無法動態改變樣式"/>
<Button Background="{DynamicResource RedBrush}" Margin="5" FontSize="14" Content="使用Dynamic Resource,點選改變樣式" Click="On_Click"/>
<Button Margin="5" Content="資源字典ResourceDictionary" FontSize="14" Background="{StaticResource blueBrush}" Foreground="Red"></Button>
</StackPanel>
</Window>
4.多個工程或應用程式之間共享資源
在需要應用資源字典的工程的App.xaml中,引入工程外的資源字典