首页 > 其他分享 >WPF开发中的资源引用

WPF开发中的资源引用

时间:2023-09-08 10:11:17浏览次数:52  
标签:Assets 资源 开发 引用 WPF Resources 字典

文章主要内容:

项目资源管理

常用资源引用

控件模板

动态资源变更

项目资源管理

资源直接直接引用

在项目中直接添加一张图片,并将属性的生成操作改为"资源",在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);
 }

运行效果如下:

标签:Assets,资源,开发,引用,WPF,Resources,字典
From: https://www.cnblogs.com/ZHIZRL/p/17685702.html

相关文章

  • 结构化文档开发笔记
    一些浅薄的见解有错误请指出1.结构化文档编写和发布的基本流程看了一些工具的文档,发现从编写xml到输出为pdf、html的步骤是固定的,从编写XML文件到输出为PDF文档的一般流程包括以下步骤:编写XML文件:根据需要的文档结构和内容,使用XML标记语言编写XML文件。XML文件应包含所需的数......
  • HybridApp(混合应用)开发框架的优缺点分析
    写在前面HybridApp作为一种既能够在原生应用程序环境中运行,也能够在Web浏览器中运行的应用程序。它主要使用Web技术进行开发,如HTML、CSS和JavaScript,并使用一个中间层将其封装在原生应用程序中。当然技术的持续推进,HybridApp相关的前端框架也应运而生。准备比较......
  • ONLYOFFICE 明文核心代码 API级别调用 可进行二次开发
    本次改造基于V7.1.1进行,已经更新进入docker。这部分东西需要付费购买,请加我的wei:cao_rui_jian_xiong项目核心sdk_all.js等全部改造为明文,可以方便阅读和二次开发下面是改造后的代码截取。(function(window,undefined){(function(window){varMAX_ACTION_TIME=20;......
  • 1.2 Java 开发环境配置
       ......
  • 【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
    (文章目录)前言WPF控件是WindowsPresentationFoundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见......
  • 【效率提升】手把手教你如何使用免费的 Amazon Code Whisperer 提升开发效率堪比 GitH
    说明GitHubcopilot虽然很强,但是一个月10美金的费用拿来吃个小火锅他不香吗?而身为云计算博主将向你推荐一款可以平替GitHubcopilot并且免费的支持多种编程语言的AI编程助手AmazonCodeWhisperer。亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术......
  • 十大功能特性,助力开发者玩转API Explorer
    伴随着我国API生态逐渐成熟、市场发展不断完善,API已广泛应用在以网页、移动应用、后端系统集成为主的众多开发场景中。同时,开发者对API的主要诉求已由获取数据能力转变为获取技术能力、甚至业务能力,开发者渴望更加高效便捷的调用方式,除关注API产品本身性能外,也愈发关注优质的服务和......
  • iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)
    1.创建新项目 为项目添加图标 2.将TableViewController添加到界面中 将箭头移动到TableView上来,代表它是首页(根页面).选中ViewController,点击Delete,对它进行删除.将代码ViewController.swift也删除掉. 新建一个CocoaTouchClass.  将TableViewControlle......
  • 鸿蒙开发基础知识和环境搭建详解
    鸿蒙开发学习方案:学习基础知识:了解鸿蒙的基本概念和特点,包括其分布式架构、能力和开发理念。学习鸿蒙的开发环境搭建,包括安装开发工具和配置开发环境。学习鸿蒙应用开发:学习鸿蒙应用开发框架,包括应用程序生命周期、界面设计和布局、事件处理等。学习鸿蒙应用的数据存储和管理,包括文......
  • 米联客MPSOC MLK-F20-CM02-2CG-3EG-4EV开发板硬件手册
    1整体概述MLK-F20-CM02-2CG-3EG-4EV(MLK-8X)系列开发平台是米联客ZynqUltraScale+系列开发平台的全新高端产品。其核心模块集成电源管理:0.85V核心电源,最大输出12A。用户基于核心模块设计功能底板(提供功能底板设计方案)。降低项目功能底板设计难度和生产成本,加速项目开发。其应用......