博客
关于我
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加强(5)~DML 增删改操作和 DQL 查询操作
查看>>
mysql加强(6)~子查询简单介绍、子查询分类
查看>>
mysql加强(7)~事务、事务并发、解决事务并发的方法
查看>>
MySQL千万级多表关联SQL语句调优
查看>>
mysql千万级大数据SQL查询优化
查看>>
MySQL千万级大表优化策略
查看>>
MySQL单实例或多实例启动脚本
查看>>
MySQL压缩包方式安装,傻瓜式教学
查看>>
MySQL原理、设计与应用全面解析
查看>>
MySQL原理简介—1.SQL的执行流程
查看>>
MySQL参数调优详解
查看>>
mysql参考触发条件_MySQL 5.0-触发器(参考)_mysql
查看>>
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySQL各个版本区别及问题总结
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>