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>
相关推荐
苏克贝塔几秒前
WPF11-附加属性
wpf
苏克贝塔2 分钟前
WPF13-MVVM进阶
wpf
吾与谁归in3 小时前
C#实现本地Deepseek模型及其他模型的对话
人工智能·c#·wpf·deepseek
lisenustc1 天前
WPF学习之Prism(二)
学习·wpf
百里与司空3 天前
WPF基本布局基础
学习·wpf
qq_382391333 天前
WPF框架学习
学习·wpf·1024程序员节
CE贝多芬4 天前
WPF的页面设计和实用功能实现
c#·wpf
酷炫码神4 天前
WPF布局控件
wpf
code_shenbing4 天前
WPF 实现虚拟键盘
c#·wpf
code_shenbing5 天前
WPF实现打印机控制及打印
wpf