说来也简单:首先,我在WPF项目中建立了一个用户自定义控件(CustomControl),VS模板为我们自动生成了

CustomControl1和Theme文件夹(里边包含一个Generic.xaml):

WPF中的Pack URI

接着,我想把它移动到一个新的类库(DLL)里去:

WPF中的Pack URI

然后我添加了对类库的引用在WPF项目中,我开始尝试使用该自定义控件,结果,发现显示的结果始终不对,但是也

没有报错。究其原因,肯定是xaml没有加载到,致使界面没有渲染模板。最后开始搜索MSDN,发现WPF应用程序外

部包引用一般使用pack://规范。

PACK URI

   表 1:标记中的绝对 Pack URI

 

文件

绝对 pack URI

资源文件 — 本地程序集

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

子文件夹中的资源文件 — 本地程序集

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

资源文件 — 所引用的程序集

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

所引用的程序集的子文件夹中的资源文件

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

所引用的版本化程序集中的资源文件

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

内容文件

"pack://application:,,,/ContentFile.xaml"

子文件夹中的内容文件

"pack://application:,,,/Subfolder/ContentFile.xaml"

源站点文件

"pack://siteoforigin:,,,/SOOFile.xaml"

子文件夹中的源站点文件

"pack://siteoforigin:,,,/Subfolder/SOOFile.xaml"

 

    表 2:标记中的相对 Pack URI

 

文件

相对 pack URI

本地程序集中的资源文件

"/ResourceFile.xaml"

本地程序集的子文件夹中的资源文件

"/Subfolder/ResourceFile.xaml"

所引用的程序集中的资源文件

"/ReferencedAssembly;component/ResourceFile.xaml"

所引用的程序集的子文件夹中的资源文件

"/ReferencedAssembly;component/Subfolder/ResourceFile.xaml"

内容文件

"/ContentFile.xaml"

子文件夹中的内容文件

"/Subfolder/ContentFile.xaml"

在代码中使用 Pack URI

下面的示例说明了这一点。

Uri uri = new Uri("pack://application:,,,/File.xaml");

 

Uri 类的实例时会引发异常。

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

 

UriKind 的参数,使您可以指定 pack URI 是绝对

URI 还是相对 URI。

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

 

如果您不了解

RelativeOrAbsolute。

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

 



SO,外部资源文件,比如视频、图片等,路径的引用都需要使用pack uri。
另外,还有,为什么在普通类库中右键添加新建项,没有wpf 自定义控件选项,只能手动建,或者从WPF项目建立后移植过来。

相关文章:

  • 2021-06-16
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-10-13
相关资源
相似解决方案