WPF-图片,音频,资源加载

WPF 资源管理

图片加载

1.建立assert包引用资源

第一种:将图片属性设置为资源重新生成。(不能建resource包,有冲突)

第二种:使用相对路径,将属性设置为始终复制。pack://siteoforigin:,,,代表应用名称。

xml 复制代码
  <Image Source="/Assert/image/Earth.jpg" />
  <Image Source="pack://siteoforigin:,,,/Assert/image/Earth.jpg" />

第三种:跨程序集引用,需要添加资源的类库,以及对类库的引用
pack://application:,,,/程序集名称;[版本号;][公钥;]component/Images/zhaoxi.png

xml 复制代码
<Image Source="pack://application:,,,/zhiyu.assert;component/image/Earth.jpg" />

字体图标加载

常用图标库:阿里图标库

1.将选好的图标添加到购物车

2.在购物车处,添加到项目当中,并下载代码

  1. 解压代码,找到.ttf文件,复制到项目的资源库当中

  2. 去图标网站上项目当中 copy 图片代码。

  3. 将代码 copy 进内容,设置 FontFamily 属性 后续需要跟上 #iconfont

xml 复制代码
 <TextBlock Width="40" Height="120" Text="&#xe60f;" FontFamily="assert\front\iconfont.ttf#iconfont"></TextBlock>
  1. 实现结果

如何设置所有的TTF文件

只用将所有 ttf 文件(文件名需要不同)放在同一个目录资源下,然后将ttf文件名称路径给去除。

xml 复制代码
 <TextBlock Width="40" Height="120" Text="&#xe60f;" FontFamily="assert\front\#iconfont"></TextBlock>

音频视频加载

只能使用本地资源,音频的属性设置只能使用较新复制 或者始终复制

xml 复制代码
<MediaElement Source="pack://siteoforigin:,,,/assert/audio/1.mp3"></MediaElement>

资源字典

  1. 新添加一个 WPF 的资源库
  2. 在资源库中定义 Style
xml 复制代码
<Style TargetType="Button">
    <Setter Property="Content" Value="你好"></Setter>
    <Setter Property="Background" Value="Yellow"></Setter>
    <Setter Property="Height" Value="100"></Setter>
    <Setter Property="Width" Value="100"></Setter>
</Style>
  1. 引用资源库 使用 ResourceDictionary
xml 复制代码
  <Window.Resources>
      <ResourceDictionary Source="assert/res/buttonstyleres.xaml"></ResourceDictionary>
  </Window.Resources>
   <Grid>
     <Button ></Button>    
 </Grid>
  1. 多个资源库合并使用 MergedDictionaries 关键字
xml 复制代码
 <Window.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="assert/res/buttonstyleres01.xaml"></ResourceDictionary>
             <ResourceDictionary Source="assert/res/buttonstyleres02.xaml"></ResourceDictionary>
             <!--跨程序集的引用-->
             <ResourceDictionary Source="/zhiyu.asset;component/Assets/Res/TextBoxStyles.xaml"/>
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Window.Resources>
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘18 小时前
开发WPF项目时遇到的问题总结
wpf
hqwest2 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars2 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest2 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0073 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业6 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103586 天前
WPF之绑定!
c#·wpf
DataIntel6 天前
wpf问题记录
wpf
蓝点lilac7 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像