Freigeben über


Understanding Windows Presentation Foundation Application Data Files

Most of the time WPF applications depend on files such as XAML, images, video, and audio. WPF provides support for configuring, identifying, and using these types of files. We basically have 3 types of data files in WPF

  • Resource Files: Data files that are compiled into either an executable or library WPF assembly.
  • Content Files: Standalone data files that have an explicit association with an executable XAML assembly.
  • Site of Origin Files: Standalone data files that have no association with an executable XAML assembly.

Resource and content file are design time concepts but Site of Origin is something not known until runtime to refer these data files WPF uses uses the Pack uniform resource identifier (URI) Scheme

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

Using Resource files in Code

 

    Uri uri = new Uri("/ResourceFile.xaml", UriKind.Relative);

    StreamResourceInfo objinfofile = Application.GetResourceStream(uri);

    System.Windows.Markup.XamlReader objreader = new System.Windows.Markup.XamlReader();

    Page page = (Page)reader.LoadAsync(objinfofile.Stream);

    this.pageFrame.Content = page;

 

Site of Origin Files

When to use Site of origin Files

  • A file doesn't exist when at compile time.
  • You don't what files your assembly will require until run time.
  • You want to be able to update files without recompiling the assembly that they are associated with.

 

Here is a sample how you will call these files in XAML

<Image Source="file:///C:/imagefile.bmp"></Image>

<Image Source="https://www.microsoft.com.com/imagefile.bmp"></Image>

Here is the sample to load files directly in a frame

<GroupBox Header="Absolute Pack URIs">

<StackPanel>

<Frame Source="pack://application:,,,/ResourceFile.xaml" />

<Frame Source="pack://application:,,,/Subfolder/ResourceFile.xaml" />

<Frame Source="pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml" />

<Frame Source="pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml" />

<Frame Source="pack://application:,,,/ContentFile.xaml" />

<Frame Source="pack://application:,,,/Subfolder/ContentFile.xaml" />

<Frame Source="pack://siteoforigin:,,,/SiteOfOriginFile.xaml" />

<Frame Source="pack://siteoforigin:,,,/Subfolder/SiteOfOriginFile.xaml" />

</StackPanel>

</GroupBox>