博客
关于我
Silverlight学习笔记:资源的位置
阅读量:428 次
发布时间:2019-03-06

本文共 3095 字,大约阅读时间需要 10 分钟。

    在 Web 项目中,我们免不了使用一些诸如图片、音频、视频、字体之类的在我们的程序中非可执行的数据文件,习惯称之为资源文件。在Silverlight中,使用这些资源文件的方法有很多,比如官方的说法:

 

  • 作为应用程序包中的单个文件。

  • 作为按需检索的单个文件。

  • 作为嵌入应用程序包的程序集中的文件。

  • 作为嵌入外部库包的程序集中的文件。

  • 作为程序集中嵌入的按需检索的文件。

 

    对于这个说法,我觉得很晦涩,所以亲自实践了一下。对于 Silverlight 来说,我们可以将资源发布到 xap 的包中,也可以部署到其所在的网站,控制这个的一个重要的选项就是我们在 Build 工程时的一个 build action 属性。

 

下面讨论三种在工程中引用资源的方法:资源 Resource、内容 content 和 none。

 1、默认情况下 mainPage.xaml 的 Build action 是 Page,而加入的资源文件则是 Resource。这样,我们加入到 应用的根目录下的图片可以这样引用。

<
UserControl 
x:Class
="_009_uri.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
 
    mc:Ignorable
="d"
 d:DesignWidth
="640"
 d:DesignHeight
="480"
>
  
<
Grid 
x:Name
="LayoutRoot"
>
        
<
Image 
Source
="./blend.jpg"
></
Image
>
  
</
Grid
>
</
UserControl
>

 编译后,可以看到图片。

资源(Resource):这个build action选项会将文件嵌入项目的程序集中。这个选项意味着,如果你添加了一个视频,那么你生成的xap会比你想象中的要大一些。

2、 按照内容的方式进行 build。我们先看一下代码:

<
UserControl 
x:Class
="_009_uri.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
 
    mc:Ignorable
="d"
 d:DesignWidth
="640"
 d:DesignHeight
="480"
>
  
<
Grid 
x:Name
="LayoutRoot"
>
        
<
Image 
Source
="./blend.jpg"
 Width
="250"
 Margin
="0,0,300,0"
></
Image
>
        
<
MediaElement 
Source
="./old6.mp4"
 Margin
="250,50,0,0"
 Width
="200"
></
MediaElement
>
  
</
Grid
>
</
UserControl
>

 虽然引用的方式没有变化,但是此时我们必须将 jpg 和 mp4 文件放到网站的 ClientBin 或者其他和我们的应用同级的目录中,才能够正常的访问,而此时,我们生成的 xap 又变成了一个小巧的文件包。

如果我们不适用相对的路径,仍然可以用绝对的路径来访问我们的应用。

<
UserControl 
x:Class
="_009_uri.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
 
    mc:Ignorable
="d"
 d:DesignWidth
="640"
 d:DesignHeight
="480"
>
  
<
Grid 
x:Name
="LayoutRoot"
>
        
<
Image 
Source
="./blend.jpg"
 Width
="250"
 Margin
="0,0,300,0"
></
Image
>
        
<
MediaElement 
Source
="http://localhost:7323/009_uri.Web/ClientBin/old6.mp4"
 Margin
="250,50,0,0"
 Width
="200"
></
MediaElement
>
  
</
Grid
>
</
UserControl
>

 我认为,这种方法使我们日常项目中经常用到的。

 

另外,如果我们使用前导斜杠(/)的相对URI,则表示我们要基于应用程序跟的位置来寻找资源。

<
UserControl 
x:Class
="_009_uri.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
 
    mc:Ignorable
="d"
 d:DesignWidth
="640"
 d:DesignHeight
="480"
>
  
<
Grid 
x:Name
="LayoutRoot"
>
        
<
Image 
Source
="./blend.jpg"
 Width
="250"
 Margin
="0,0,300,0"
></
Image
>
        
<
MediaElement 
Source
="/../Assets/old6.mp4"
 Margin
="250,50,0,0"
 Width
="200"
></
MediaElement
>
  
</
Grid
>
</
UserControl
>

 

3、build action 为 none的时候,我们可以按照2的方式来进行引用。

    

 

参考资料:

1、。

2、

转载地址:http://anrkz.baihongyu.com/

你可能感兴趣的文章
MySQL查询报错ERROR:No query specified
查看>>
mysql查询数据库储存数据的占用容量大小
查看>>
MySQL查询数据库所有表名及其注释
查看>>
MySQL查询数据表中数据记录(包括多表查询)
查看>>
MySQL查询结果排序
查看>>
MYSQL查询语句优化
查看>>
mysql查询语句能否让一个字段不显示出来_天天写order by,你知道Mysql底层执行原理吗?
查看>>
MySQL查询语句:揭秘专家秘籍,让你秒变数据库达人!
查看>>
mysql查询超时对PHP执行的影响
查看>>
mysql查询输出到excel文件_如何保存mysql查询输出到excel或.txt文件?
查看>>
mysql查询过程
查看>>
MySQL模拟Oracle序列sequence
查看>>
Mysql模糊查询like效率,以及更高效的写法
查看>>
MySQL死锁套路:一次诡异的批量插入死锁问题分析
查看>>
Mysql死锁问题Deadlock found when trying to get lock;try restarting transaction
查看>>
mysql每个数据库的最大连接数_MySQL数据库最大连接数
查看>>
Mysql流程控制结构,if函数、case结构、if结构、循环结构
查看>>
mysql添加外网访问权限
查看>>
mysql添加用户
查看>>
MySQL添加用户、删除用户与授权
查看>>