The pack URI in WPF

Source: Internet
Author: User


Original: Pack URI in WPF


Problem



It's easy to say: First, I built a user-defined control (CustomControl) in a WPF project, and the VS template automatically generated



CustomControl1 and Theme folder (contains a generic.xaml):






Next, I want to move it to a new class library (DLL):






Then I added a reference to the class library in the WPF project, I started trying to use the custom control, and I found that the result was always incorrect, but also



No error. The reason, of course, is that the XAML is not loaded, causing the interface to have no rendering template. Finally, start searching for MSDN and discover outside the WPF application



The PACK://specification is generally used for part-package references.


PACK URI


Table 1: Absolute Pack URIs in tags


File

Absolute Pack URI

Resource file-local assembly

"Pack://application:,,,/resourcefile.xaml"

Resource files in subfolders-Local assemblies

"Pack://application:,,,/subfolder/resourcefile.xaml"

Resource file-referenced assembly

"Pack://application:,,,/referencedassembly;component/resourcefile.xaml"

Resource files in subfolders of referenced assemblies

"Pack://application:,,,/referencedassembly;component/subfolder/resourcefile.xaml"

Resource files in the referenced versioned assembly

"Pack://application:,,,/referencedassembly;v1.0.0.0;component/resourcefile.xaml"

Content Files

"Pack://application:,,,/contentfile.xaml"

Content files in subfolders

"Pack://application:,,,/subfolder/contentfile.xaml"

Source site Files

"Pack://siteoforigin:,,,/soofile.xaml"

Source site files in subfolders

"Pack://siteoforigin:,,,/subfolder/soofile.xaml"




Table 2: Relative Pack URIs in tags


File

Relative Pack URI

Resource files in the local assembly

"/resourcefile.xaml"

Resource files in subfolders of the local assembly

"/subfolder/resourcefile.xaml"

Resource files in the referenced assembly

"/referencedassembly;component/resourcefile.xaml"

Resource files in subfolders of referenced assemblies

"/referencedassembly;component/subfolder/resourcefile.xaml"

Content Files

"/contentfile.xaml"

Content files in subfolders

"/subfolder/contentfile.xaml"

Using the Pack URI in your code


In code, you can specify the pack URI by instantiating the URI class and passing the pack URI as an argument to the constructor. This is illustrated in the following example.


Uri uri = new Uri ("Pack://application:,,,/file.xaml");





By default, the URI class treats the pack URI as an absolute pack URI. Therefore, an exception is thrown when an instance of the URI class is created with a relative pack URI.


Uri uri = new Uri ("/file.xaml");





uri (String, UriKind) overload of the uri class constructor accepts a parameter of type urikind to the Specify whether a pack URI is either absolute or relative." > Fortunately, the,uri  class constructor  uri (String, UriKind)   overloads can accept a type of  urikind The   parameter allows you to specify that the pack URI is absolute



The URI or relative URI.


// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);





absolute or relative when You were certain that the provided pack URI was one or the other. " > When you are able to determine whether the provided pack URI is a relative pack URI or an absolute pack URI, you should specify only  Absolute  or  relative.  relativeorabsolute instead." > If you don't know



The type of pack URI used (for example, when the user enters the pack URI at run time), use Relativeorabsolute instead.


// Relative or Absolute URI provided by user via a text box
TextBox userProvidedUriTextBox = new TextBox();
Uri uri = new Uri(userProvidedUriTextBox.Text, UriKind.RelativeOrAbsolute);




So, external resource files, such as videos, images, and so on, the path references need to use the pack URI.
Also, why right-adding new items in the normal class library, no WPF custom control options, can only be built manually, or migrated from WPF project creation.


The pack URI in WPF


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.