Resource and content generation operations in WPF

Source: Internet
Author: User
Tags pack relative

Add two images content.jpg and resource.jpg to the project, setting their build action properties to content and resource, respectively.
Add two image controls Imgcontent and Imgresource to the interface, setting the source path to Content.jpg and Resource.jpg, respectively, in XAML. After running the Imgresource can display the picture normally, but the Imgcontent control is not displayed. Copy the content.jpg picture to the application's debug directory, and the Imgcontent control can display the picture.
The build action is set to resource, and the resource is added to the assembly when it is generated. You can try to delete the original picture, the picture is displayed normally. Replace the original image with a new picture, still showing the original picture. Only the rebuild action is selected to update the picture.
The build action is set to content and is not added to the assembly, and resources can be accessed only if the resource is placed in the program's running directory. Program Run directory debugging is the debug directory, after installation is the program's installation directory. Unlike placing a resource directly in the program's running directory, adding a content resource file adds a custom attribute to the assembly (Assemblyassociatedcontentfile) that records whether the file exists and its relative location. I found that VS2008 has a small bug that changes the content.jpg in the solution manager to the Con.jpg,bug directory. At this point the compiler error: File Con.jpg is not part of the project or its "Build Action" property is not set to "Resource". But press F5 to run, the build succeeds and displays normally. It can be seen that when changing content resources, the customization features in the assembly do not change accordingly.
Delete the Source property in XAML and set the source property for two picture controls in the background.   There are three ways of doing this. First Kind
Imgcontent.source = new BitmapImage (New Uri ("Content.jpg", urikind.relative));
Imgresource.source = new BitmapImage (New Uri ("Resource.jpg", urikind.relative)); The second Kind
Imgcontent.source = new BitmapImage (The new Uri ("Pack://application:,,,/content.jpg"));
Imgresource.source = new BitmapImage (The new Uri ("Pack://application:,,,/resource.jpg"));

Third Kind
Imgcontent.source = new BitmapImage (The new Uri ("Pack://siteoforigin:,,,/content.jpg"));

The first and second types of resource and content resources that can access relative paths. The third way is to access the content resource files under the Web site run directory and the completely loose files. Completely loose files refer to files that are not added to the project, but are copied in the program directory. The application does not know it exists at all.
Pack://application:,,,/content.jpg represents a resource for the current project. It is pack://application:,,,/dllname; A shorthand for component/content.jpg. You can access resources for other assemblies by replacing dllname with other assemblies.
Pack://siteoforigin:,,,/content.jpg represents access to files from the deployment location.
The pack URI format is part of the XML file specification in the following format: Pack://packageuri/partpath. Packageuri actually puts a URI in the URI, which turns the backslash into a comma. Packageuri can aspire to an XPS document, such as file:///C:/document. XPS will be encoded as file:...c:,document.xps. There are two types of URI systems in WPF programs that are specially handled:
siteoforigin:///encoded after Siteoforigin:,,,
application:///encoded after application:,,,
The 3 comma is actually a backslash code.

Reprinted from: http://blog.sina.com.cn/s/blog_12ae43bfd0102vary.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.