引用地址:https://www.cnblogs.com/ZHIZRL/p/17685702.html
项目资源管理
资源直接直接引用
在项目中直接添加一张图片,并将属性的生成操作改为"资源",在XAML中直接引用文件路径
源站点资源引用
将图片属性的生成操作改为"无",复制到输出目录改为"始终复制"。重新编译后会在源站点生成和程序中的一样路径的文件。
使用"pack://siteoforigin:,,,"访问源站点文件,在程序运行过程中生成的资源会被占用。
<StackPanel>
<!--源站点方式引用资源-->
<Image Source="pack://siteoforigin:,,,/Assets/Images/Tom.jpg" Width="100" Height="100"/>
<StackPanel>
跨程序集引用
创建WPF类库用于存放公共资源,添加Images文件夹并添加一张图片,图片属性的生成操作改为"资源"
程序中使用"pack://application:,,,/Assets;[版本号;][公钥;]component/"方式引用
WPF默认使用"pack://application:,,,"
<StackPanel>
<!--源站点方式引用资源-->
<Image Source="pack://siteoforigin:,,,/Assets/Images/Tom.jpg" Width="100" Height="100"/>
<!--跨项目引用方式-->
<Image Source="pack://application:,,,/Assets;component/Images/Tom.jpg" Width="100" Height="100"/>
<!--WPF默认"pack://application:,,,"-->
<Image Source="/Assets;component/Images/Tom.jpg" Width="100" Height="100"/>
<StackPanel>
常用资源引用
字体
创建字体图标库
使用iconfont矢量图标库创建字体图图标文件,并添加到项目中
项目中使用字体图标
iconfont文件属性的生成操作改为"资源"
在WPF的Window.Resources加载资源或者在APP中使用ResourceDictionary加载
代码中使用"pack://application:,,,/Assets;component/"方式引用
<Window.Resources>
<FontFamily x:Key="iconfont">pack://application:,,,/Assets;component/Fonts/#iconfont</FontFamily>
<Window.Resources>
<Application x:Class="Start.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Start"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Res/Th_cn.xaml"/>
</ResourceDictionary.MergedDictionaries>
<FontFamily x:Key="iconfont">pack://application:,,,/Assets;component/Fonts/#iconfont</FontFamily>
</ResourceDictionary>
</Application.Resources>
</Application>
多个iconfont文件引用需要注意文件内部定义的字体名称
XAML(资源字典)
资源字典引用
在Assets中创建资源字典,并添加"ButtonStyles"与"TextBoxStyles"
在WPF的Window.Resources中加载资源
代码中直接引用即可
ButtonStyles
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=System.Runtime">
<sys:Double x:Key="width_d">200</sys:Double>
<SolidColorBrush Color="Peru" x:Key="brush"/>
<Style TargetType="Button" >
<Setter Property="Background" Value="{StaticResource ResourceKey=brush}"/>
<Setter Property="BorderBrush" Value="{StaticResource brush}"/>
</Style>
<Style TargetType="Button" x:Key="BtnStyle">
<!--<Style.Setters>-->
<Setter Property="Width" Value="{StaticResource width_d}"/>
<Setter Property="Height" Value="200"/>
<Setter Property="Background" Value="Green"/>
<Setter Property="Content" Value="Button"/>
<!--</Style.Setters>-->
<!--触发器-->
<Style.Triggers>
<Trigger Property="Background" Value="Orange">
<!--<Trigger.Setters>-->
<Setter Property="Content" Value="Orange"/>
<!--</Trigger.Setters>-->
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<!--<Trigger.Setters>-->
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Gray"/>
<!--</Trigger.Setters>-->
</Trigger>
<!--多重条件-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsPressed" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Foreground" Value="Red"/>
</MultiTrigger.Setters>
</MultiTrigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.5" To="250"
Storyboard.TargetProperty="Width"/>
<DoubleAnimation Duration="0:0:0.5" To="250"
Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.5"
Storyboard.TargetProperty="Width"/>
<DoubleAnimation Duration="0:0:0.5"
Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
TextBoxStyles
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBox" x:Key="TbStyle">
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Background" Value="Orange"/>
</Style>
</ResourceDictionary>
WPF
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/Assets;component/Res/ButtonStyles.xaml"/>
<Window.Resources>
<Button Style="{StaticResource BtnStyle}"/>
资源字典合并
在WPF的Window.Resources中加载多个资源字典会产生报错"Resources只能设置一次",使用ResourceDictionary.MergedDictionaries合并资源字典。在ResourceDictionary下添加项目自己的样式。
<Window.Resources>
<ResourceDictionary x:Name="eee">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Assets;component/Res/ButtonStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Assets;component/Res/TextBoxStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TabItem">
<Setter Property="Width" Value="90"/>
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBox" x:Key="tbs">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<TabControl>
<TabItem Header="AAAA"/>
<TabItem Header="BBB"/>
<TabItem Header="CCC"/>
</TabControl>
资源字典动态修改
在主程序项目中添加"Th_cn.xaml"和Th_cn.xam"资源字典。并在WPF中添加TextBlock控件和Button控件,代码如下:
WPF中的资源引用关键字:
StaticResource:静态加载,程序启动时确定资源,加载后不在修改
DynamicResource:动态加载,程序运行过程中可通过修改引用的资源地址实现动态修改
<TabControl>
<TabItem Header="AAAA"/>
<TabItem Header="BBB"/>
<TabItem Header="CCC"/>
</TabControl>
<TextBlock Height="40"
Text="{DynamicResource Title}"
Background="{DynamicResource TitleBackground}"
Foreground="{DynamicResource TitleForeground}"/>
<Button Content="切换主题/语言" Click="Button_Click"/>
Th_cn.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=System.Runtime">
<sys:String x:Key="Title">番茄杀手</sys:String>
<SolidColorBrush x:Key="TitleBackground">#DDD</SolidColorBrush>
<SolidColorBrush x:Key="TitleForeground">#333</SolidColorBrush>
</ResourceDictionary>
Th_cn.xam
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=System.Runtime">
<sys:String x:Key="Title">ZRL</sys:String>
<SolidColorBrush x:Key="TitleBackground">#006CBE</SolidColorBrush>
<SolidColorBrush x:Key="TitleForeground">#FFF</SolidColorBrush>
</ResourceDictionary>
双击Button创建一个单击事件
private void Button_Click(object sender, RoutedEventArgs e)
{
//获取主程序Resources资源列表
var rd = Application.Current.Resources;
//修改Resources的Uri地址
rd.MergedDictionaries[0].Source = new Uri("Assets/Res/Th_en.xaml", UriKind.Relative);
}
运行效果如下: