[.Net码农]WPF 文件级资源(类似与使用CSS文件,然后引用CSS文件)

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 }
复制代码

作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值