work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 文件级资源:定义在资源字典的XAML文件中,添加“资源字典(Resource Dictionary)”类型的项

文件名为Dictionary1.xaml

Dictionary1.xaml
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3 <SolidColorBrush Color="Red" x:Key="redBrush"></SolidColorBrush>
4 </ResourceDictionary>

以下是WIndow1.xaml文件

Window1.xaml
 1 <Window x:Class="WpfWindowAndDialog.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen">
5 <Window.Resources>
6 <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
7 </Window.Resources>
8 <Grid>
9 <Button Height="23" HorizontalAlignment="Left" Background="{StaticResource ResourceKey=redBrush}"
10      Margin="10,10,0,0" Name="button1" VerticalAlignment="Top"  >Button</Button>
11 </Grid>
12 </Window>

类似与使用CSS文件,然后引用CSS文件.可以有多个资源文件,这样便于管理

2.对象(控件)级资源:定义在某个ContentControl中,作为其子容器、子控件共享的资源

定义在资源字典的XAML文件中,添加“资源字典(Resource Dictionary)”类型的项

文件名为Dictionary1.xaml

Dictionary1.xaml
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3 <SolidColorBrush Color="Red" x:Key="redBrush"></SolidColorBrush>
4 </ResourceDictionary>

Window2.xaml文件

Window2
<Window x:Class="WpfWindowAndDialog.Window2"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.Resources>
<ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
</Grid.Resources>
<Button Height="23" HorizontalAlignment="Left" Background="{StaticResource ResourceKey=redBrush}" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" >Button</Button>
</Grid>
</Window>

3.将图形作为一种资源来使用

Window3.xaml
 1 <Window x:Class="WpfWindowAndDialog.Window3"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen">
5 <Window.Resources>
6 <Ellipse x:Key="myEllipse" Fill="Yellow" Height="100"/>
7 </Window.Resources>
8 <StackPanel>
9 <StaticResource ResourceKey="myEllipse"/>
10 </StackPanel>
11 </Window>

(2)重用绘图资源

Windows4.xaml
 1 <Window x:Class="WpfWindowAndDialog.Window4"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen">
5 <Window.Resources>
6 <GeometryDrawing x:Key="drawing" Brush="Green">
7 <GeometryDrawing.Geometry>
8 <EllipseGeometry RadiusX="200" RadiusY="10"/>
9 </GeometryDrawing.Geometry>
10 </GeometryDrawing>
11 <DrawingBrush x:Key="dbrush" Drawing="{StaticResource drawing}"></DrawingBrush>
12 </Window.Resources>
13 <StackPanel>
14 <Rectangle Width="250" Height="50" Fill="{StaticResource dbrush}">
15 </Rectangle>
16 <Rectangle Width="250" Height="50" Fill="{StaticResource dbrush}">
17 </Rectangle>
18 </StackPanel>
19 </Window>

4.皮肤与主题

两款非常简单的皮肤文件

BlueSkin.xaml
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3 <Style TargetType="{x:Type Button}">
4 <Setter Property="Background" Value="Blue"/>
5 <Setter Property="Foreground" Value="White"/>
6 </Style>
7 <SolidColorBrush x:Key="appBackground" Color="#EEF"/>
8 </ResourceDictionary>
GreenSkin.xaml
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3 <Style TargetType="{x:Type Button}">
4 <Setter Property="Background" Value="Green"/>
5 <Setter Property="Foreground" Value="White"/>
6 </Style>
7 <SolidColorBrush x:Key="appBackground" Color="#EEF"/>
8 </ResourceDictionary>

引用皮肤的文件

Window1.xaml
 1 <Window x:Class="Chapter12.<Window x:Class="Chapter12.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Window1" Height="300" Width="300" Background="{DynamicResource appBackground}">
5 <Grid>
6 <Grid.RowDefinitions>
7 <RowDefinition/>
8 <RowDefinition/>
9 <RowDefinition/>
10 </Grid.RowDefinitions>
11 <RadioButton x:Name="chooseGreenSkin" Grid.Row="0" Content="Green"/>
12 <RadioButton x:Name="chooseBlueSkin" Grid.Row="1" Content="Blue"/>
13 <Button Grid.Row="2">Hello</Button>
14 </Grid>
15 </Window>"
16 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18 Title="Window1" Height="300" Width="300" Background="{DynamicResource appBackground}">
19 <Grid>
20 <Grid.RowDefinitions>
21 <RowDefinition/>
22 <RowDefinition/>
23 <RowDefinition/>
24 </Grid.RowDefinitions>
25 <RadioButton x:Name="chooseGreenSkin" Grid.Row="0" Content="Green"/>
26 <RadioButton x:Name="chooseBlueSkin" Grid.Row="1" Content="Blue"/>
27 <Button Grid.Row="2">Hello</Button>
28 </Grid>
29 </Window>

Window.xaml的后台代码

Window.xaml.cs
 1 using System;
2 using System.Collections.ObjectModel;
3 using System.Windows;
4
5 namespace Chapter12
6 {
7 /// <summary>
8 /// Window1.xaml 的交互逻辑
9 /// </summary>
10 public partial class Window1 : Window
11 {
12 public Window1()
13 {
14 InitializeComponent();
15 EnsureSkins();
16 chooseBlueSkin.Click += SkinChanged;
17 chooseGreenSkin.Click+=SkinChanged;
18 }
19
20 static ResourceDictionary greenSkin;
21 static ResourceDictionary blueSkin;
22
23 void EnsureSkins()
24 {
25 if(greenSkin == null)
26 {
27 greenSkin = new ResourceDictionary();
28 greenSkin.Source = new System.Uri("GreenSkin.xaml", UriKind.Relative);
29
30 blueSkin = new ResourceDictionary();
31 blueSkin.Source = new System.Uri("BlueSkin.xaml", UriKind.Relative);
32 }
33 }
34
35 void SkinChanged(object o, EventArgs e)
36 {
37 if (chooseGreenSkin.IsChecked.Value)
38 {
39 ApplySkin(greenSkin);
40 }
41 else
42 {
43 ApplySkin(blueSkin);
44 }
45 }
46
47 void ApplySkin(ResourceDictionary newSkin)
48 {
49 Collection<ResourceDictionary> appM = Application.Current.Resources.MergedDictionaries;
50 if (appM.Count != 0)
51 {
52 appM.Remove(appM[0]);
53 }
54 appM.Add(newSkin);
55
56 }
57 }
58 }