Content and resource of buildaction)

Source: Internet
Author: User

Many new Windows Phone 7 developers often encounter problems such as the inability to display images and the inability to access multimedia files due to improper setting of the buildaction attribute of resource files. In Windows Phone 7, aspx "target =" _ blank "> the buildaction attribute of a resource file usually has three optional values: content, resource, and none, so which one is suitable? Next we will make a simple exploration of this issue.

Question 1: What is the difference between resources whose buildaction attribute is set to content/resource/none?

We can solve this problem through a simple test. For ease of observation, we chose two large video files (about 10 MB each) as the resource file for testing.

, We add two video files video1.wmv and video2.wmv to the medias folder in the project.

Set the buildaction attribute to content and resource respectively.

Press F6 to compile the project. Find the compiled xap file in the bin/debug folder of the project and change its suffix to zip (friends who know Silverlight should be clear that an xap file is actually a zip package ).

Open the ZIP file with the compression software. In the medias folder, you can see that the vedio1.wmv file exists in the compressed package as an independent file (right figure ).

So where is the vieo2.wmv file? Observe the size of the democode1.dll file in the left figure --- more than 10 MB !!! You guessed it was right. vieo2.wmv was embedded into this DLL file.

So what about setting it to none? After you try it, you will find that the resource file set to none is neither directly packaged in the xap file nor embedded in the DLL in the xap, and the resource file will be completely ignored during the compilation process. In the following discussion, setting buildaction to none is ignored.

Through the above tests, we can summarize as follows:

  • Files whose buildaction attribute is set to conten will be directly packaged in xap files as independent files.
  • Files whose buildaction attribute is set to resource will be embedded into the DLL file in the xap package.
  • Files whose buildaction property is set to none will not be stored in any form in the xap package
Question 2: choose content or resource for buildaction?

Based on the current usage experience, the following is a brief summary.

In most cases, both methods can be used, but content is more convenient in the following situations:

  • In general, when using content, the resource file URI is more concise, while the resource is relatively cumbersome.
    • E.g. For example, video1.wmv files can be accessed through Uri "/medias/video1.wmv,
      Video2.wmv requires "[project name]; component/medias/video1.wmv" Access (except in XAML)
  • When using content, files are directly packaged in the project. Therefore, you can only use content to generate xap files in batches.
  • For multimedia files, you must use the content format for better performance.
    • Original msdn document:
      "Media Processing on Windows Phone 7 is optimized to use files and network streams, but not in-memory streams. this means that any media files are stored in the application, such as sound effects, shocould have their build action set to content and notresource."
      "The multimedia processing on Windows Phone is optimized for file and network streams, but the memory stream does not. This means that any media files, such as sound effects, should be included in the application, and the buildaction attribute should be set to content rather than resource ."
    • For more information, see wpmind's WP7 Dev 101 [7] Select buildaction

Resource usage may be more appropriate in the following scenarios:

  • To avoid asynchronous loading of resource files, use resource
    • E.g. A typical scenario is the background image of the panorama control. If it is set to content, it will flash when the page is displayed for the first time. Original msdn document:
      "If a panorama control is using an image for the background, its build action shoshould be set to resource; otherwise, it will not appear immediately when the application is first displayed. setting the build action to content wocould cause it to be loaded asynchronously."
      "If the panorama control uses an image as the background, its buildaction attribute should be resource; otherwise, the image will not be displayed immediately when the application manual is displayed. Setting buildaction to the content attribute will result in asynchronous loading ."
    • Many of the images used as the background are similar to the previous example.
  • Resource can better protect resource files in your xap package than content (such as images designed by yourself ).
    • This is easy to understand, because the resource file is directly stored in the xap package in the form of a file when the content is obtained, and your resource file is obtained after the xap is decompressed. The resource is embedded in the DLL file, which is relatively troublesome (but not completely secure ).
  • Resource files (such as XML files) that need to be dynamically accessed from the C # code must be set to resource. For more information, see the scenario described in the next lesson.
     
Extended knowledge

In fact, not only can the buildaction attribute be set in the resource file, but all files in the vs project have the buildaction attribute, such as The XAML file and CS file, in general, we do not need to modify the buildaction attribute of these files. Click here to learn more about buildaction.

Original article:

Http://blog.sina.com.cn/s/blog_667102dd0100zkws.html

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.