Pack URI 不适用于 generic.xaml 个合并词典
Pack URI not working for generic.xaml merged dictionaries
我做了一个自定义控件库,在根路径下做了一个名为"FlipView"的控件。然后我删除了 Generic.xaml
中的样式并将其移动到根路径中名为 FlipView.xaml
的自己的 Resource 字典中。现在,我使用以下代码将该资源字典合并到 Generic.xaml
中:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
然后我在另一个 wpf 项目中使用了该控件,但它抛出了 XamlParseException
和 InnerException
说
Cannot locate resource 'flipview.xaml'.
为什么不能呢?资源字典位于控件库项目的根路径中。
如果我将 Source
属性 setter 替换为 "pack://application:,,,/MyCustomControls;component/FlipView.xaml"
(MyCustomControls
是我的自定义控件库的名称),它可以正常工作。
Generic.xaml:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyCustomControls;component/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
为什么会这样?
对于 WPF 项目,这似乎是多余的,因为 pack://application,,,/
指的是根路径,而 WpfAssemblyName;component/
再次指的是根路径。为什么需要 Generic.xaml
?
编辑:我看到了 this 问题,但它没有解释 为什么 。
因为你使用了另一个程序集中的资源文件,需要指向一个程序集名称。
The pack URI for a resource file that is compiled into a referenced assembly uses the following authority and path:
Authority: application:///.
Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format:
AssemblyShortName[;Version][;PublicKey];component/Path
- AssemblyShortName: the short name for the referenced assembly.
- ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
- ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
- ;component: specifies that the assembly being referred to is referenced from the local assembly.
- /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.
以下示例显示了位于引用程序集项目文件夹根目录中的 XAML 资源文件的包 URI。
pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml
更新:
为避免在同一程序集中声明样式的地址中出现冗余词,可以在不指向库的情况下声明样式文件:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
克莱门斯的回答:
再看第三张图here。应用程序中的包 URI 是相对于应用程序程序集的,即使它们在库中使用也是如此。您的资源位于引用的程序集中,因此您必须指定其名称。
我做了一个自定义控件库,在根路径下做了一个名为"FlipView"的控件。然后我删除了 Generic.xaml
中的样式并将其移动到根路径中名为 FlipView.xaml
的自己的 Resource 字典中。现在,我使用以下代码将该资源字典合并到 Generic.xaml
中:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
然后我在另一个 wpf 项目中使用了该控件,但它抛出了 XamlParseException
和 InnerException
说
Cannot locate resource 'flipview.xaml'.
为什么不能呢?资源字典位于控件库项目的根路径中。
如果我将 Source
属性 setter 替换为 "pack://application:,,,/MyCustomControls;component/FlipView.xaml"
(MyCustomControls
是我的自定义控件库的名称),它可以正常工作。
Generic.xaml:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyCustomControls;component/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
为什么会这样?
对于 WPF 项目,这似乎是多余的,因为 pack://application,,,/
指的是根路径,而 WpfAssemblyName;component/
再次指的是根路径。为什么需要 Generic.xaml
?
编辑:我看到了 this 问题,但它没有解释 为什么 。
因为你使用了另一个程序集中的资源文件,需要指向一个程序集名称。
The pack URI for a resource file that is compiled into a referenced assembly uses the following authority and path:
Authority: application:///.
Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format:
AssemblyShortName[;Version][;PublicKey];component/Path
- AssemblyShortName: the short name for the referenced assembly.
- ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
- ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
- ;component: specifies that the assembly being referred to is referenced from the local assembly.
- /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.
以下示例显示了位于引用程序集项目文件夹根目录中的 XAML 资源文件的包 URI。
pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml
更新:
为避免在同一程序集中声明样式的地址中出现冗余词,可以在不指向库的情况下声明样式文件:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
克莱门斯的回答:
再看第三张图here。应用程序中的包 URI 是相对于应用程序程序集的,即使它们在库中使用也是如此。您的资源位于引用的程序集中,因此您必须指定其名称。