WPF 文件级资源(类似与使用CSS文件,然后引用CSS文件)

简介:

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

文件名为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文件

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

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文件

复制代码
<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.将图形作为一种资源来使用

复制代码
 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)重用绘图资源

复制代码
 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.皮肤与主题

两款非常简单的皮肤文件

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

引用皮肤的文件

复制代码
 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的后台代码

复制代码
 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/archive/2011/07/30/2122149.html,如需转载请自行联系原作者

目录
相关文章
|
8月前
|
前端开发
WebView2 控件(基于 Microsoft Edge (Chromium) 的嵌入式浏览器控件),保存资源(图片、脚本、CSS)
WebView2 控件(基于 Microsoft Edge (Chromium) 的嵌入式浏览器控件),保存资源(图片、脚本、CSS)
409 65
|
前端开发
在Webpack配置文件中,如何配置loader以处理其他类型的文件,如CSS或图片
在Webpack配置文件中,通过设置`module.rules`来配置loader处理不同类型的文件。例如,使用`css-loader`和`style-loader`处理CSS文件,使用`file-loader`或`url-loader`处理图片等资源文件。配置示例:在`rules`数组中添加对应规则,指定`test`匹配文件类型,`use`指定使用的loader。
|
前端开发 UED
Webpack 中处理 CSS 和图片资源的多 Loader 配置
【10月更文挑战第12天】 处理 CSS 和图片资源是 Webpack 配置中的重要部分。通过合理选择和配置多个 Loader,可以实现对这些资源的精细处理和优化,提升项目的性能和用户体验。在实际应用中,需要不断探索和实践,根据项目的具体情况进行灵活调整和优化,以达到最佳的处理效果。通过对 Webpack 中多 Loader 处理 CSS 和图片资源的深入了解和掌握,你将能够更好地应对各种复杂的资源处理需求,为项目的成功构建和运行提供坚实的基础。
447 58
|
前端开发 JavaScript
开发过程找不到css源文件?试试配置vite的css源文件映射
【8月更文挑战第3天】开发过程找不到css源文件?试试配置vite的css源文件映射
472 0
开发过程找不到css源文件?试试配置vite的css源文件映射
|
前端开发 JavaScript
文本,wangEditor5展示HTML无样式,wangEditor5如何看源码,Ctrl + U看CSS文件,代码高亮,Prism.js可以实现,解决方法,参考网页源代码的写法
文本,wangEditor5展示HTML无样式,wangEditor5如何看源码,Ctrl + U看CSS文件,代码高亮,Prism.js可以实现,解决方法,参考网页源代码的写法
|
C# 开发者 Windows
WPF与PDF文档:解锁创建和编辑PDF文件的新技能——从环境配置到代码实践,手把手教你如何在WPF应用中高效处理PDF,提升文档管理效率
【8月更文挑战第31天】随着数字文档的普及,PDF因跨平台兼容性和高保真度成为重要格式。WPF虽不直接支持PDF处理,但借助第三方库(如iTextSharp)可在WPF应用中实现PDF的创建与编辑。本文通过具体案例和示例代码,详细介绍了如何在WPF中集成PDF库,并展示了从设计用户界面到实现PDF创建与编辑的完整流程。不仅包括创建新文档的基本步骤,还涉及在现有PDF中添加页眉页脚等高级功能。通过这些示例,WPF开发者可以更好地掌握PDF处理技术,提升应用程序的功能性和实用性。
951 0
|
C# 开发者 UED
WPF开发者必备秘籍:深度解析文件对话框使用技巧,打开与保存文件原来如此简单!
【8月更文挑战第31天】在WPF应用开发中,文件操作是常见需求。本文详细介绍了如何利用`Microsoft.Win32`命名空间下的`OpenFileDialog`和`SaveFileDialog`类来正确实现文件打开与保存功能。通过示例代码展示了如何设置文件过滤器、初始目录等属性,并使用对话框进行文件读写操作。正确使用文件对话框能显著提升用户体验,使应用更友好易用。
661 0
|
开发者 C# 存储
WPF开发者必读:资源字典应用秘籍,轻松实现样式与模板共享,让你的WPF应用更上一层楼!
【8月更文挑战第31天】在WPF开发中,资源字典是一种强大的工具,用于共享样式、模板、图像等资源,提高了应用的可维护性和可扩展性。本文介绍了资源字典的基础知识、创建方法及最佳实践,并通过示例展示了如何在项目中有效利用资源字典,实现资源的重用和动态绑定。
507 0
|
前端开发 JavaScript Linux
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
218 0
|
C#
WPF 静态资源(StaticResource)和动态资源(DynamicResource)
WPF 静态资源(StaticResource)和动态资源(DynamicResource)
495 0