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>
相关推荐
明耀2 小时前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风4 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技1 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆2 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退2 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu2 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退6 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆6 天前
WPF中的XAML详解
wpf·xaml
ithouse6 天前
使用WPF实现一个快速切换JDK版本的客户端工具
java·开发语言·wpf