17

WPF APP项目添加资源文件夹

 4 years ago
source link: http://blog.devwiki.net/index.php/2019/12/31/add-resources-Dictionary-in-wpf-application-project.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

0x01. 概要

一般我们会在项目中某个目录中统一管理二进制资源文件, 如图片, 文档, 音频等等.

新建的WPF APP项目默认是没有类似 Android res 这样的文件夹来管理资源文件. 如何建立资源文件夹并正常使用呢?

0x02. 创建过程

1. 创建资源文件夹

对项目右键, 然后 新建 -> 文件夹 :

MR3aiyV.png!web

文件夹名称命名为自己想要名称, 比如: Resources .

ZzyYZff.png!web

2. 创建分类文件夹

可根据需要在上一步的 Resources 文件夹内创建分类文件夹, 比如:

  • Images
  • Audios

VzYRZbj.png!web

3. 创建资源字典文件

Resources 文件右键, 新建 -> 资源字典(ResourceDictionary)文件,并填写名称.

vqmY3ie.png!web

此处填写的是 DemoRes.xaml , 但是到这一步项目还不能识别到该文件.

63iENrJ.png!web

4. 添加资源字典到项目配置

打开项目中的 App.xaml 文件, 在 ApplicationResource 节点中添加 ResourceDictionary 节点:

<Application x:Class="WPFDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="../MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="../Resources/DemoRes.xaml"/>
    </Application.Resources>
</Application>

这样项目就可以识别该资源字典文件了.

5. 测试识别结果

Resources\Images 文件夹中添加一个图片文件, 并尝试在布局文件中添加引用.

j2QnMjB.png!web

然后在布局文件中引用:

<Image x:Name="PreImage" Width="300" Height="300" Stretch="Uniform"
       Source="{StaticResource HeaderImage}">

然后编译测试, 可以通过~.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK