In the previous Post, I discussed resources in WPF from the perspective of resource compilation behavior. However, both Resource and Content declare resources during compilation. If we break this restriction, we do not want to specify a fully confirmed resource address. WPF provides an abstraction similar to IE address location, which is determined based on the location of the application deployment.
WPF conceptually abstracts the origin location of the application. If our application is located at http://yilinglai.cnblogs.com/testdir/test.application. The origin of our application is http://yilinglai.cnblogs.com/testdir/, so we can define the resource location in the application:
<Image Source = "pack: // siteoforigin:,/Images/Test. JPG"/>
With this encapsulated Uri, resource reference is more flexible. So, what are the advantages of this method similar to the resource packaging Uri of Internet applications?
1) After the application Assembly is created, the file can also be replaced.
2) files can be downloaded only when necessary.
3) when compiling an application, we do not need to know the content of the file (or the file does not exist at all ).
4) if some files are embedded in the Assembly of the application, WPF cannot load them. For example, the HTML content in the Frame, Media file.
Pack: // is actually a URI (Uniform Resource Identifiers) syntax format. Pack: // <authority> <absolute_path>. The authority is an embedded URI. Note that this URI complies with the RFC 2396 documentation. Because it is embedded in the URI, some reserved characters must be ignored. In our previous example, the slash ("/") is replaced by a comma. Other characters such as "%" and ","?" All must be ignored.
In the previous example, siteoforigin can be considered as a special case of authority. WPF uses it to abstract the original site where the application is deployed. For example, our application is in C: \ App, and there is a Test under the same directory. JPG file. to access this file, we can use hard-coded URI file: // c:/App/Test. JPG. Another method is this Abstraction: pack: // siteoforigin:,/Test. JPG. The convenience of this access method is self-evident! In the XPS document specification, URI is better described. If you are interested, you can download it here.
You may see some problems with this understanding. Don't worry too much. As you become more familiar with WPF, you will have more experiences. For beginners of WPF (I am also), do not be overly entangled in this point. This is because the WPF Application class provides some useful methods:
Application. GetResourceStream (Uri relativeUri );
Application. GetContentStream (Uri relativeUri );
Application. GetRemoteStream (Uri relativeUri );
By using these functions, the URI locating details are hidden. From the names of these functions, we can see that they correspond to the three types I introduced earlier: Content, Resource, and SiteofOrigin.
Finally, let's briefly describe another method of using resources to directly define resources without using any attributes. The specific usage can be understood in the example below:
<StackPanel Name = "sp1">
<StackPanel. Resources>
<Ellipse x: Key = "It1" Fill = "Red" Width = "100" Height = "50"/>
<Ellipse x: Key = "It2" Fill = "Blue" Width = "200" Height = "100"/>
</StackPanel. Resources>
<StaticResource ResourceKey = "It1"/>
<StaticResource ResourceKey = "It2"/>
</StackPanel>