During the development process, we encountered the problem that the image loader and the video player could not normally read media resources.
InCodeThe path of the image in is correct, and the image cannot be read normally. While the video part adopts the same code, but it can be read normally.
The code for reading images is as follows:
The code for reading a video is as follows:
The two addresses are similar to:/assets/xxxx/XXX. XXX.
The error message for reading images is as follows:
And then compare the video and image files.
The build action settings of the original image and video are different.
The video's content image is rescoure.
After reading msdn, we found that the final resource acquisition for the two generation operations is very different and has a great impact on the performance.
First introduce build action
Action |
Description |
None |
Resources are neither integratedProgramAnd is not packaged into the xap package. However, we can set the copytooutputdirectory option to make it automatically copy to the directory where the xap package is located. In this case, the relative URI of the image to be accessed must start. Applicable scenarios: In most cases, we want to put the video/audio file out of the xap, because this type of file is generally large and will affect the loading of the Silverlight application, in addition, generally, video and audio files are compressed and put into xap will not reduce their file size. Resource file generation operations such as image and video are the same if they are none and they are not added to the project. They can be referenced using absolute Uri. |
Compile |
Not suitable for resource files. The class file must be generated using "compile", that is, the. CS or. VB file in the project. |
Content |
Resources are packaged in the xap package. In this case, the relative URI of the image to be accessed must start. In this way, if the image file is not found in xap, Silverlight will automatically find the required image file from the folder where the current xap application is located, if not found, the imagefailed event is triggered. This method is applicable when multiple assemblies reference the same file. |
Embedded Resource |
This method will embed the file into the assembly, and Silverlight cannot reference this file in XAML and C # Through Uri, microsoft does not recommend that you embed resources in a collection in Silverlight in this way. If you have such requirements, you can use the reflection. Assembly. getexecutingassembly (). getmanifestresourcestream (string path) method to obtain the stream reference of the file. |
Applicationdefinition |
The Silverlight program's entry XAML file (App. XAML by default) should be set to this "application definition ". This is not suitable for other files. |
Page |
Not suitable for resource files. All user controls, pages, and child forms (usercontrol/page/childwindow) should be generated. Otherwise, the code file corresponding to the background cannot be linked to this XAML file. When "page" is used to build an action, errors in XAML will cause the project to fail to be correctly generated. |
Codeanalysisdictionary |
Code Analysis and usage, which can be ignored in Silverlight |
Resource |
Resources are packaged in the Assembly. After this method is selected, the resource file will be embedded in the application set, that is, the generated xap cannot be opened. You can use the relative URI of the current XAML file to access the file, <Image Source = "sl.png"/> or <Image Source = ". /sl.png "/>, you can use <Image Source =" in the subfolders ". /images/sl.png "/>. The safest way is to use the unique Assembly Resource URI for access. The format is <Image Source = "/{assemblyshortname}; component/sl.png"/>, this method can also reference images in other xap assembly. System resources generated in this way can be directly obtained using application. getresourcestream (URI). stream in the code. |
Splashscreen |
"Splashscreen" is used in the WPF startup screen. Other methods are used to start the loading screen of Silverlight. Therefore, do not use this method in Silverlight. |
Entitydeploy |
This is the generation method used by entityframework, Which is useless in Silverlight. |
It is important to note that media resources are usually usedContentAndResourceThere are two different methods.
TakeContent,Resources are packaged in the xap package. In this case, the relative URI of the image to be accessed must start. In this way, if the image file is not found in xap, Silverlight will automatically find the required image file from the folder where the current xap application is located, if not found, the imagefailed event is triggered. This method is applicable when multiple assemblies reference the same file.
To obtain media resources, you can directly use/folders/files to obtain the resource files.
UseResource,Resources are packaged in the Assembly. After this method is selected, the resource file will be embedded in the application set, that is, the generated xap cannot be opened. You can use the relative URI of the current XAML file to access the file, <Image Source = "sl.png"/> or <Image Source = ". /sl.png "/>, you can use <Image Source =" in the subfolders ". /images/sl.png "/>. The safest way is to use the unique Assembly Resource URI for access. The format is <Image Source = "/{assemblyshortname}; component/sl.png"/>, this method can also reference images in other xap assembly. System resources generated in this way can be directly obtained using application. getresourcestream (URI). stream in the code.
Method for obtaining media resources:/{assemblyshortname}; component/sl.png, where assemblyshortname is the Assembly name.
In this issue, resource is used for image files, while content is used for video files. So there is a problem.
In msdn, for performance considerations, media resources must be set to content as follows:
Optimize Media Processing on Windows Phone to use file and network streams instead of memory streams. This means that any media files (such as sound effects) included in the application should"Generation operation"Set"Content"Instead"Resources". When a media file is compiled as the content, it is stored together with the application file (. xap) as a loose file, rather than in the application file. When a media file is compiled as a resource, it is usually accessed by retrieving the file stream, which may reduce the performance. In addition, when you compile a media file as the content, it is played directly. When a media file is compiled as a resource, you need to copy the content to the file on Windows Phone before playing the video, which reduces the performance.
Reference address: http://msdn.microsoft.com/zh-cn/library/ff967560 (V = vs.92). aspx
Http://www.cnblogs.com/listenfly/archive/2011/11/03/2235059.html