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>
相关推荐
beyond谚语21 小时前
第一章 WPF概述
wpf
necessary6532 天前
从工行“余额归零”事件看CAP定理:当金融系统在一致性与可用性之间做出选择
分布式·金融·wpf·可用性测试
棉晗榜2 天前
WPF隐藏控件后,怎么让其上部的控件空间自动撑高
wpf
壹佰大多3 天前
【Redisson分布式锁源码分析-3】
数据结构·分布式·mysql·spring·spring cloud·wpf·lua
LateFrames3 天前
以小白视角尝试 WPF / WinUI3 / MAUI / MAUI Blazor 构建 Windows 桌面程序
windows·wpf·maui·mauiblazor·winui3
偶尔的鼠标人3 天前
Avalonia/WPF 打开子窗口,并且跨页面传值
c#·wpf·mvvm·avalonia
玖笙&3 天前
✨WPF编程进阶【6.1】:图形原则(附源码)
c++·c#·wpf·visual studio
lixy5793 天前
WPF检测网络状态切换
wpf
纸照片3 天前
WPF中为Button设置IsMouseOver和IsPressed事件中改变背景颜色不起作用
c#·.net·wpf
Aevget3 天前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(四)
ui·.net·wpf·devexpress·wpf控件