Wednesday, July 2, 2008

About pack uri style

As a WPF developer you might have seen the resource reference in the form

pack://application:,,,/somefile.xaml

and you might have used this style too.Actually what is this pack uri ?


This is a new method introduced in WPF to locate files in an assembly or in the current folder.Those files may be images,videos,audio and even another xaml file.

Parts

The first part identifies whether it points to an application or to the current folder where the file is located and the second part is the relative path to that file.

First part can be any one of the following

  • application -> Tells that the file is located in the assembly as Content or Resource
  • pack://application:,,,/ResourceFile.xaml
  • siteoforigin -> Its in the current executing folder
  • pack://siteoforigin:,,,/SiteOfOriginFile.xaml

      Second part tells the assembly where the file is located.If the file locate in same assembly there is no need to mention it other wise use assembly name

      Same assembly - pack://application:,,,/ResourceFile.xaml
      Different assembly - pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

      Third part is the version and this is optional

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

      Fourth part is the path to file.

      pack://application:,,,/Subfolder/ResourceFile.xaml

      Advantage of pack uri style

      • You may separate your styles from application and keep as dll.
      • Design time support
      What msdn says

      1 comment:

      1. Hello Joy,
        Here's a problem that I am facing. If you have a solution, let me know. Problem steps below
        1. Create A Windows Froms Application using VS2008.
        2. Create a WPF user control named UserControl1 (a user control with just a text box inside is sufficient)
        3. Create another user control named user control 2 which includes user control1. i.e, one of the element in usercontrol2 is usercontrol1
        4. insert user control 2 to the form1 in the application
        5. Rebuild application and try to reload the form 1 in visual studio designer. The designer crashes with a URI related message. do you know why

        ReplyDelete