Silverlight resources URI with Different build action

Source: Internet
Author: User

In this post we will look at how application data files can be referenced

Using Relative Uris from within XAML or code in your Silverlight application.

Silverlight provides you with a luxury of choices for how you declare your application data files. these are the non-code artifacts of your app, such as images, videos, fonts etc. the build action for any file-I. e. how you declare it in your project file or within Visual Studio or blend-is consequential to how it can be referenced in XAML or code via a URI. while care was taken to ensure defaults that gel with the programmers intuitiveness while referencing them in Uris, there are always subtle issues that the intrepid Silverlight designer or developer must be aware. if you come to Silverlight with previous WPF experience, the contents of this post shoshould be old news to you. if not, fear not; Silverlight is designed to be easy to learn and easy to master.

Let's take image files as an example as we walk through the varous cases. once you understand how this works, you can extrapoate behavior for mediaelement (audio/video) or multiscaleimage (Seadragon in Silverlight) or perhaps even fonts. while examples below show XAML syntax, the same applies to code.

Files marked as resource:

    • Get embedded in the app assembly within the xap
    • Are accessible using a relative Uri, relative to the XAML file from where they are being referenced. For e.g. <image source?#foo.jpg "/>
    • When not found, there is no fallback. The imagefailed event is raised.

Tip: Using Assembly qualified Uris is most reliable both from within and outside that Assembly. E. g. <Image Source = "/{assemblyshortname}; component/foo.jpg"/>
Tip: You can programmatically extract any application resource using application. getresourcestream (URI). stream.

Files marked as content:

    • Get added to the xap at the conceptual application root
    • Are accessible using a relative Uri, relative to the application root. This requires the use of a leading slash. For e.g. <Image Source = "/foo.jpg"/>
    • When not found, the resource loading mechanic falls back to the application's site of origin and looks for the image under the same (Web root) dir as the xap is located
    • When not found, the imagefailed event is raised.

Tip: This is a no-brainer when you have a resource that is commonly used from within 2 assemblies in the xap

Files marked as none (with copytooutputdirectory set appropriately ):

    • Don't get copied into the xap, but the copytooutputdirectory metadata will ensure it gets copied alongside the xap
    • Are accessible using a relative Uri, relative to the application root. This requires the use of a leading slash. For e.g. <Image Source = "/foo.jpg"/>
    • This is the fallback path of the above

Tip: in most cases you want to keep your video files out of the xap. they are not encumbered by X-domain download restrictions. you do not get compression benefits from keeping them in the xap. rather you can use streaming or progressive download if they are outside.

Files marked as none, as well as image and audio/video files not marked at all in the project (I. e. external to the Project), can always be referenced using absolute Uris.

Files marked as embeddedresource:

Get embedded into the assembly in a manner that Silverlight cannot use for Uri references either from XAML or code
Shocould be changed to resource, and used as detailed abve.
Hopefully this demystifies URI usage in Silverlight for you. If we can make improvements in the runtime or in tools to make this easier for you, please post a comment here. Feedback is always appreciated.

Original address: http://nerddawg.blogspot.com/2008/03/silverlight-2-demystifying-uri.html.

Conclusion:

When the build action of a file is set to Resource, force the XAML related URI or assembly related URI mode is used, that is, the specified source does not need to add "/".

    • If the referenced resource is at the same level of The XAML file: <image source?#foo.jpg "/>;
    • If the referenced resource is located at the upper level of the XAML file: <Image Source = "../foo.jpg"/>;

    When the build action of a file is set to content, the application root related URI method is forcibly used, that is, the specified source must be added with "/": <Image Source = "/foo.jpg"/>.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.