Beruflich Dokumente
Kultur Dokumente
Resource Files
If an application data file must always be available to an application, the only way to guarantee availability is to compile it into an application's main executable assembly or one of its referenced assemblies. This type of application data file is known as a resource file. You should use resource files when: You don't need to update the resource file's content after it is compiled into an assembly. You want to simplify application distribution complexity by reducing the number of file dependencies. Your application data file needs to be localizable (see WPF Globalization and Localization Overview).
Note
msdn.microsoft.com/en-us/library/aa970494(d=printer,v=vs.110).aspx 1/7
12/19/13
The resource files described in this section are different than the resource files described in XAML Resources and different than the embedded or linked resources described in Managing Application Resources.
< P r o j e c t" x m l n s = h t t p : / / s c h e m a s . m i c r o s o f t . c o m / d e v e l o p e r / m s b u i l d / 2 0 0 3 ". . .> . . . < I t e m G r o u p > < R e s o u r c eI n c l u d e = " R e s o u r c e F i l e . x a m l "/ > < / I t e m G r o u p > . . . < / P r o j e c t >
Note In Microsoft Visual Studio, you create a resource file by adding a file to a project and setting its Build Action to Resource. When the project is built, MSBuild compiles the resource into the assembly.
12/19/13
The following example is the markup equivalent of the preceding example. XAML < F r a m eN a m e = " p a g e F r a m e "S o u r c e = " P a g e R e s o u r c e F i l e . x a m l "/ >
< P r o j e c tx m l n s = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / d e v e l o p e r / m s b u i l d / 2 0 0 3 ". . .> . . . < I t e m G r o u p > < P a g eI n c l u d e = " M a i n W i n d o w . x a m l "/ > < / I t e m G r o u p > . . . < / P r o j e c t >
Note In Visual Studio, you add a new Window, NavigationWindow, Page, FlowDocument, or ResourceDictionary to a project, the Build Action for the markup file will default to Page. When a project with Page items is compiled, the XAML items are converted to binary format and compiled into the associated assembly. Consequently, these files can be used in the same way as typical resource files. Note If a XAML file is configured as a Resource item, and does not have a code-behind file, the raw XAML is compiled into an assembly rather than a binary version of the raw XAML.
Content Files
A content file is distributed as a loose file alongside an executable assembly. Although they are not compiled into an assembly, assemblies are compiled with metadata that establishes an association with each content file. You should use content files when your application requires a specific set of application data files that you want to be able to update without recompiling the assembly that consumes them. msdn.microsoft.com/en-us/library/aa970494(d=printer,v=vs.110).aspx
3/7
12/19/13
< P r o j e c tx m l n s = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / d e v e l o p e r / m s b u i l d / 2 0 0 3 ". . .> . . . < I t e m G r o u p > < C o n t e n tI n c l u d e = " C o n t e n t F i l e . x a m l " > < C o p y T o O u t p u t D i r e c t o r y > P r e s e r v e N e w e s t < / C o p y T o O u t p u t D i r e c t o r y > < / C o n t e n t > < / I t e m G r o u p > . . . < / P r o j e c t >
Note In Visual Studio, you create a content file by adding a file to a project and setting its Build Action to Content, and set its Copy to Output Directory to Copy always (same as Always) and Copy if newer (same as PreserveNewest). When the project is built, an AssemblyAssociatedContentFileAttribute attribute is compiled into the metadata of the assembly for each content file. [ a s s e m b l y :A s s e m b l y A s s o c i a t e d C o n t e n t F i l e ( " C o n t e n t F i l e . x a m l " ) ] The value of the AssemblyAssociatedContentFileAttribute implies the path to the content file relative to its position in the project. For example, if a content file was located in a project subfolder, the additional path information would be incorporated into the AssemblyAssociatedContentFileAttribute value. [ a s s e m b l y :A s s e m b l y A s s o c i a t e d C o n t e n t F i l e ( " R e s o u r c e s / C o n t e n t F i l e . x a m l " ) ] The AssemblyAssociatedContentFileAttribute value is also the value of the path to the content file in the build output folder.
12/19/13
C# / /N a v i g a t et ox a m lp a g e U r iu r i=n e wU r i ( " / P a g e C o n t e n t F i l e . x a m l " ,U r i K i n d . R e l a t i v e ) ; S t r e a m R e s o u r c e I n f oi n f o=A p p l i c a t i o n . G e t C o n t e n t S t r e a m ( u r i ) ; S y s t e m . W i n d o w s . M a r k u p . X a m l R e a d e rr e a d e r=n e wS y s t e m . W i n d o w s . M a r k u p . X a m l R e a d e r ( ) ; P a g ep a g e=( P a g e ) r e a d e r . L o a d A s y n c ( i n f o . S t r e a m ) ; t h i s . p a g e F r a m e . C o n t e n t=p a g e ; While calling GetContentStream gives you access to the Stream, you need to perform the additional work of converting it to the type of the property that you'll be setting it with. Instead, you can let WPF take care of opening and converting the Stream by loading a resource file directly into the property of a type using code. The following example shows how to load a Page directly into a Frame (pageFrame) using code. C# U r ip a g e U r i=n e wU r i ( " / P a g e C o n t e n t F i l e . x a m l " ,U r i K i n d . R e l a t i v e ) ; t h i s . p a g e F r a m e . S o u r c e=p a g e U r i ; The following example is the markup equivalent of the preceding example. XAML < F r a m eN a m e = " p a g e F r a m e "S o u r c e = " P a g e C o n t e n t F i l e . x a m l "/ >
12/19/13
application (XBAP) that was launched from the Internet or intranet, and it requests only the set of permissions that are allowed for applications launched from those locations, loose files can only be loaded from the application's site of origin (launch location). Such files are known as site of origin files. Site of origin files are the only option for partial trust applications, although are not limited to partial trust applications. Full trust applications may still need to load application data files that they do not know about at build time; while full trust applications could use file:///, it is likely that the application data files will be installed in the same folder as, or a subfolder of, the application assembly. In this case, using site of origin referencing is easier than using file:///, because using file:/// requires you to work out the full path the file. Note Site of origin files are not cached with an XAML browser application (XBAP) on a client machine, while content files are. Consequently, they are only downloaded when specifically requested. If an XAML browser application (XBAP) application has large media files, configuring them as site of origin files means the initial application launch is much faster, and the files are only downloaded on demand.
< P r o j e c tx m l n s = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / d e v e l o p e r / m s b u i l d / 2 0 0 3 ". . .> . . . < N o n eI n c l u d e = " P a g e S i t e O f O r i g i n F i l e . x a m l " > < C o p y T o O u t p u t D i r e c t o r y > A l w a y s < / C o p y T o O u t p u t D i r e c t o r y > < / N o n e > . . . < / P r o j e c t >
Note In Visual Studio, you create a site of origin file by adding a file to a project and setting its Build Action to None. When the project is built, MSBuild copies the specified files to the build output folder.
12/19/13
C# / /N a v i g a t et ox a m lp a g e U r iu r i=n e wU r i ( " / S i t e O f O r i g i n F i l e . x a m l " ,U r i K i n d . R e l a t i v e ) ; S t r e a m R e s o u r c e I n f oi n f o=A p p l i c a t i o n . G e t R e m o t e S t r e a m ( u r i ) ; S y s t e m . W i n d o w s . M a r k u p . X a m l R e a d e rr e a d e r=n e wS y s t e m . W i n d o w s . M a r k u p . X a m l R e a d e r ( ) ; P a g ep a g e=( P a g e ) r e a d e r . L o a d A s y n c ( i n f o . S t r e a m ) ; t h i s . p a g e F r a m e . C o n t e n t=p a g e ; While calling GetRemoteStream gives you access to the Stream, you need to perform the additional work of converting it to the type of the property that you'll be setting it with. Instead, you can let WPF take care of opening and converting the Stream by loading a resource file directly into the property of a type using code. The following example shows how to load a Page directly into a Frame (pageFrame) using code. C# U r ip a g e U r i=n e wU r i ( " p a c k : / / s i t e o f o r i g i n : , , , / S i t e O f O r i g i n F i l e . x a m l " ,U r i K i n d . A b s o l u t e ) ; t h i s . p a g e F r a m e . S o u r c e=p a g e U r i ; The following example is the markup equivalent of the preceding example. XAML < F r a m eN a m e = " p a g e F r a m e "S o u r c e = " p a c k : / / s i t e o f o r i g i n : , , , / S i t e O f O r i g i n F i l e . x a m l "/ >
See Also
Concepts
Pack URIs in WPF
msdn.microsoft.com/en-us/library/aa970494(d=printer,v=vs.110).aspx
7/7