博客
关于我
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/

你可能感兴趣的文章
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>