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>
相关推荐
Zhen (Evan) Wang1 天前
.NET 6 + Dapper + User-Defined Table Type
sqlserver·c#·.net·wpf
界面开发小八哥2 天前
界面控件DevExpress WPF v25.1预览 - 支持Windows 11系统强调色
windows·wpf·界面控件·devexpress·ui开发·.net 9
军训猫猫头2 天前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
Pasregret2 天前
缓存与数据库一致性深度解析与解决方案
数据库·缓存·wpf
Java林间4 天前
Zookeeper是什么?基于zookeeper实现分布式锁
分布式·zookeeper·wpf
zizisuo4 天前
1.微服务拆分与通信模式
微服务·wpf
程序员秘密基地4 天前
基于c#,wpf,ef框架,sql server数据库,音乐播放器
sql·sqlserver·c#·.net·wpf
Zhen (Evan) Wang4 天前
.NET 6 WPF 利用CefSharp.Wpf.NETCore显示PDF文件
.net·wpf·.netcore
冰茶_5 天前
WPF特性分析
学习·microsoft·c#·wpf
qq_196055876 天前
最快打包WPF 应用程序
wpf