WPF之资源

WPF之资源

静态资源

静态资源只会赋值一次,后面不会随着源改变而改变

<Window x:Class="WpfApp3.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:WpfApp3"
        xmlns:vm="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush  x:Key="MyB1" Color="Red"/>
        <SolidColorBrush  x:Key="MyB2" Color="Black"/>

    </Window.Resources>
    
    <Grid Margin="10">

        <StackPanel>
            <StackPanel.Resources>
                <SolidColorBrush  x:Key="MyB3" Color="Green"/>

            </StackPanel.Resources>

            <Button Content="A" Height="30" Margin="5" Background="{StaticResource MyB1}"/>
            <Button Content="A" Height="30" Margin="5" Background="{StaticResource MyB2}"/>

            <Button Content="A" Height="30" Margin="5" Background="{StaticResource MyB3}"/>

        </StackPanel>
    </Grid>
</Window>

在这里插入图片描述

二 动态资源

using System.Text;
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 WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["MyB1"] = new SolidColorBrush(Colors.Yellow); 
            this.Resources["MyB2"] = new SolidColorBrush(Colors.Gray);

        }
    }
}
<Window x:Class="WpfApp3.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:WpfApp3"
        xmlns:vm="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush  x:Key="MyB1" Color="Red"/>
        <SolidColorBrush  x:Key="MyB2" Color="Black"/>

    </Window.Resources>
    
    <Grid Margin="10">

        <StackPanel>
            <StackPanel.Resources>
                <SolidColorBrush  x:Key="MyB3" Color="Green"/>

            </StackPanel.Resources>

            <Button Content="A" Height="30" Margin="5" Background="{StaticResource MyB1}"/>
            <Button Content="A" Height="30" Margin="5" Background="{DynamicResource MyB2}"/>

            <Button Click="Button_Click"   Height="30" Margin="5" />

        </StackPanel>
    </Grid>
</Window>

在这里插入图片描述
在这里插入图片描述

代码找资源
在这里插入图片描述

找资源顺序,从内到外,一直找到app.resource,再到系统资源

在这里插入图片描述

在这里插入图片描述

资源字典

在这里插入图片描述

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush  x:Key="Red" Color="Red"/>
    <LinearGradientBrush x:Key="MyLGB" StartPoint="0.5 0" EndPoint="0.5 1">
        <GradientStop Color="Red" Offset="0"></GradientStop>
        <GradientStop Color="Blue" Offset="1"></GradientStop>
    </LinearGradientBrush>


</ResourceDictionary>
<Application x:Class="WpfApp3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp3"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
                <!--<ResourceDictionary Source="Dictionary2.xaml"></ResourceDictionary>-->

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
         
    </Application.Resources>
</Application>


<Window x:Class="WpfApp3.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:WpfApp3"
        xmlns:vm="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>

    </Window.Resources>
    
    <Grid Margin="10" Background="{StaticResource MyLGB}">

    </Grid>
</Window>

在这里插入图片描述

跨项目调用资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="Black" Color="Black"/>
</ResourceDictionary>
<Application x:Class="WpfApp3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp3"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
                <!--<ResourceDictionary Source="Dictionary2.xaml"></ResourceDictionary>-->
                <ResourceDictionary Source="pack://application:,,,/WpfLibrary1;component/Dictionary1.xaml"></ResourceDictionary>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
         
    </Application.Resources>
</Application>

<Window x:Class="WpfApp3.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:WpfApp3"
        xmlns:vm="clr-namespace:WpfApp3.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>

    </Window.Resources>
    
    <Grid Margin="10" Background="{StaticResource Black}">

    </Grid>
</Window>

在这里插入图片描述

Uri调用资源

        public MainWindow()
        {
            InitializeComponent();

            Uri uri = new Uri("pack://application:,,,/WpfLibrary1;component/Dictionary1.xaml");
            ResourceDictionary resources = new ResourceDictionary();
            resources.Source = uri;

            SolidColorBrush solidColorBrush = (SolidColorBrush)resources["Black"];

        }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值