WPF之资源
1.资源的定义
2.资源的使用(绑定为静态资源或者动态资源)
<Window x:Class="WPF之资源.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF之资源"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="color1" Color="Red"/>
<SolidColorBrush x:Key="color2" Color="blue"/>
<SolidColorBrush x:Key="color3" Color="Pink"></SolidColorBrush>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<SolidColorBrush x:Key="color3" Color="Pink"></SolidColorBrush>
</StackPanel.Resources>
<Button Height="100" Width="100" Background="{StaticResource color1}"></Button>
<Button Height="100" Width="100" Background="{DynamicResource color2}"></Button>
<Button Height="100" Width="100" Click="Button_Click"></Button>
<Button Height="100" Width="100" Background="{DynamicResource color3}"></Button>
<Button Height="100" Width="100" Background="{DynamicResource color4}"></Button>
<Button Height="100" Width="100" Background="{DynamicResource color5}"></Button>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF之资源
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Resources["color1"] = new SolidColorBrush(Colors.Yellow);
this.Resources["color2"] = new SolidColorBrush(Colors.Yellow);
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="color4" Color="Black"></SolidColorBrush>
</ResourceDictionary>
<Application x:Class="WPF之资源.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF之资源"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary>
<ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary>
<!--pack://是用于标识URI方案的协议前缀-->
<!--application: 指定资源在应用程序范围内-->
<!--,,,
作用:在URI中,三个逗号用于分隔URI的不同部分,表示没有指定应用程序名称、区域性或公共密钥标记。
细节:
第一个逗号后没有应用程序名称,表示资源在默认应用程序范围内。
第二个逗号后没有区域性,表示资源不特定于任何特定区域设置。
第三个逗号后没有公共密钥标记,表示资源不特定于任何特定强名称。-->
<!--/WpfControlLibrary1
作用:指定包含资源的程序集名称。-->
<!--;component/
作用:表示资源位于程序集的组件资源部分。-->
<!--Dictionary1.xaml
作用:指定要加载的XAML资源字典文件的名称。-->
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="color5" Color="Green"></SolidColorBrush>
</ResourceDictionary>
WPF中的资源,以键值对的形式存在
一 静态资源的使用
静态资源只会赋值一次,后面不会随着改变
1.定义窗口资源
<Window.Resources>
<SolidColorBrush x:Key="color1" Color="Red"/>
</Window.Resources>
2.使用静态资源
<StackPanel>
<Button Height="100" Width="100" Background="{StaticResource color1}"></Button>
</StackPanel>
二 动态资源的使用
实时生效,随着改变而改变
<Window x:Class="WPF之资源.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF之资源"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="color1" Color="Red"/>
<SolidColorBrush x:Key="color2" Color="blue"/>
</Window.Resources>
<StackPanel>
<Button Height="100" Width="100" Background="{StaticResource color1}"></Button>
<Button Height="100" Width="100" Background="{DynamicResource color2}"></Button>
<Button Height="100" Width="100" Click="Button_Click"></Button>
</StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Resources["color1"] = new SolidColorBrush(Colors.Yellow);
this.Resources["color2"] = new SolidColorBrush(Colors.Yellow);
}
三 找资源顺序:从内到外,一直找到app.resource,再到系统资源
<Window.Resources>
<SolidColorBrush x:Key="color1" Color="Red"/>
<SolidColorBrush x:Key="color2" Color="blue"/>
<SolidColorBrush x:Key="color3" Color="Pink"></SolidColorBrush>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<SolidColorBrush x:Key="color3" Color="Pink"></SolidColorBrush>
</StackPanel.Resources>
<Application x:Class="WPF之资源.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF之资源"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush x:Key="color3" Color="Pink"></SolidColorBrush>
</Application.Resources>
</Application>
四 资源字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="color4" Color="Black"></SolidColorBrush>
</ResourceDictionary>
五 引用资源
<Application x:Class="WPF之资源.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF之资源"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
六 跨项目调用资源
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="color5" Color="Green"></SolidColorBrush>
</ResourceDictionary>
<Application x:Class="WPF之资源.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF之资源"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary>
<ResourceDictionary Source="/Dictionary1.xaml"></ResourceDictionary>
<!--pack://是用于标识URI方案的协议前缀-->
<!--application-->
<!--,,,
作用:在URI中,三个逗号用于分隔URI的不同部分,表示没有指定应用程序名称、区域性或公共密钥标记。
细节:
第一个逗号后没有应用程序名称,表示资源在默认应用程序范围内。
第二个逗号后没有区域性,表示资源不特定于任何特定区域设置。
第三个逗号后没有公共密钥标记,表示资源不特定于任何特定强名称。-->
<!--/WpfControlLibrary1
作用:指定包含资源的程序集名称。-->
<!--;component/
作用:表示资源位于程序集的组件资源部分。-->
<!--Dictionary1.xaml
作用:指定要加载的XAML资源字典文件的名称。-->
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Button Height="100" Width="100" Background="{DynamicResource color5}"></Button>