29

What I add a Image.Source I have to type the following:

playIcon.Source = new BitmapImage(new Uri(@"pack://application:,,,/TempApplication2;component/Images/play.png"));

I'm moving from web development to WPF C# and I don't get why setting a Path has extra stuff in it, where in CSS I simply add a Path string.

Can someone explain why there is Uri, pack, and the ":,,,", Application2:component?

I'm new to WPF C#.

KMC
  • 19,548
  • 58
  • 164
  • 253

2 Answers2

56

The pack uri is used to identify & locate resources, files in application and remote. The pack uri in WPF uses 'pack://authority/path' format. And this is the line from MSDN which explains this format, 'The authority specifies the type of package that a part is contained by, while the path specifies the location of a part within a package'

WPF supports two authorities: application:/// and siteoforigin:///. The application:/// authority identifies resource files, content files. The siteoforigin:/// authority identifies site of origin files.

":///" is written ":,,," because the "/" character must be replaced with the "," character, and reserved characters such as "%" and "?" must be escaped and URI that points to a package and must conform to RFC 2396.

For more info please read "Pack URIs in WPF"

I'm also learning WPF. This is what i have understood about Pack Uri in WPF till now.

JP P.
  • 173
  • 2
  • 14
Code0987
  • 2,598
  • 3
  • 33
  • 51
1

Explaining the semicolon too:

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

If you want to refer to the resource using the Referenced dll, you might use this. Remember the ReferencedAssembly refers to the name of the Assembly. Also remember if you place the ResourceFile.xaml in your root directory of referencedAssembly you should specify the path using component/ResourceFile.xaml. Therefore you can see, every file that is referenced through the ReferencedAssembly should always be relative to component folder. So for instance pack://application:,,,/MyCustomdll;component/MyResource.xaml.

pack://application:,,,/ReferencedAssembly;component/subfolder/ResourceFile.xaml

Similar to one specified above, if the resource is placed within a subfolder within the assembly, you need to again reference that relative to component folder.

Source: http://www.abhisheksur.com/2010/04/pack-uri-to-reference-component.html

Sergioet
  • 1,108
  • 13
  • 27