"Cannot locate Resource "themes.xxx.xaml"" 运行时错误 - 另一个程序集中的 WPF 自定义组件
"Cannot locate Resource "themes.xxx.xaml"" runtime error - WPF Custom Components in Another Assembly
我正在尝试使用我在我的 WPF 项目中的一个 class 库程序集中创建的自定义控件。但是,当我启动我的应用程序时,我不断收到运行时错误 "Cannot locate resource 'themes/rcttextbox.xaml'"。
在我的自定义控件库 "SoftwareThemeDesigner" 中,我的 "Themes" 文件夹中有以下 Generic.xaml 代码:
<!-- Original Generic.xaml file in "Themes" folder -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/RctTextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
/Themes/RctTextBox.xaml
是一个带有 <Style>
的资源字典,根据在 Visual Studio.[=20 中创建时的默认 "Custom Control" 文件,它有一个 ContentTemplate =]
在引用我的 "SoftwareThemeDesigner" 库的 "SoftwareThemeDesignTester" WPF 应用程序中,在 App.xaml 文件中,我有以下代码:
<!-- WPF App's App.xaml file -->
<Application x:Class="SoftwareThemeDesignerTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SoftwareThemeDesigner;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我尝试过的:
- 将每个资源字典的所有构建操作从 "Page" 更改为 "Resource"
- 为 Generic.xaml 文件中的每个资源字典添加
/SoftwareThemeDesinger;component/
前缀
- 将 RctTextBox.xaml 代码移至 Generic.xaml 并删除了资源字典文件和对它的引用(见下文)。
<!-- Modified Generic.xaml file after removing references to other resource dictionaries -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type local:RctTextBox}"
BasedOn="{StaticResource {x:Type TextBox}}>
<!-- RctTextBox Style details placed here -->
</Style>
</ResourceDictionary>
这行得通,但奇怪的是我不能使用单独的资源字典,而且必须在 "Themes" 文件夹中的 generic.xaml 文件中定义所有内容可能不切实际.
有哪位大侠可以指点一下吗?我花了几个小时在谷歌上搜索解决方案,none 成功了。我觉得我错过了什么。
提前致谢!
根据@Bizhan 的回答更新了代码
<!-- Generic.xaml file in "Themes" folder -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesigner;component/Themes/RctTextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- WPF App's App.xaml file -->
<Application x:Class="SoftwareThemeDesignerTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesigner;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
编译器似乎找不到资源,因为它没有编译或使用错误的 URI 引用。
首先,确保每个资源字典都有Build Action = Page
和Custom Tool = MSBuild:Compile
然后,确保每个 ResourceDictionary
的 Source
在 两个项目:
中设置了完整的 Pack URI
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesinger;component/Themes/Generic.xaml" />
注意 1:您可以安全地省略 pack://application:,,,
部分。
注意 2:如果资源字典仅在本地程序集中使用,则包括程序集名称是可选的。但是如果您在另一个程序集中引用资源字典,则必须在源项目和目标项目中包含程序集名称。我还没有找到对此的明确解释,但我认为这种行为是由于 BAML reader 将字典合并在一起,最终它试图在相对路径中找到资源(在本例中是 SoftwareThemeDesinger 项目根目录)
您可以阅读有关 Pack URI 的更多信息here
我正在尝试使用我在我的 WPF 项目中的一个 class 库程序集中创建的自定义控件。但是,当我启动我的应用程序时,我不断收到运行时错误 "Cannot locate resource 'themes/rcttextbox.xaml'"。
在我的自定义控件库 "SoftwareThemeDesigner" 中,我的 "Themes" 文件夹中有以下 Generic.xaml 代码:
<!-- Original Generic.xaml file in "Themes" folder -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/RctTextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
/Themes/RctTextBox.xaml
是一个带有 <Style>
的资源字典,根据在 Visual Studio.[=20 中创建时的默认 "Custom Control" 文件,它有一个 ContentTemplate =]
在引用我的 "SoftwareThemeDesigner" 库的 "SoftwareThemeDesignTester" WPF 应用程序中,在 App.xaml 文件中,我有以下代码:
<!-- WPF App's App.xaml file -->
<Application x:Class="SoftwareThemeDesignerTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SoftwareThemeDesigner;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我尝试过的:
- 将每个资源字典的所有构建操作从 "Page" 更改为 "Resource"
- 为 Generic.xaml 文件中的每个资源字典添加
/SoftwareThemeDesinger;component/
前缀 - 将 RctTextBox.xaml 代码移至 Generic.xaml 并删除了资源字典文件和对它的引用(见下文)。
<!-- Modified Generic.xaml file after removing references to other resource dictionaries -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type local:RctTextBox}"
BasedOn="{StaticResource {x:Type TextBox}}>
<!-- RctTextBox Style details placed here -->
</Style>
</ResourceDictionary>
这行得通,但奇怪的是我不能使用单独的资源字典,而且必须在 "Themes" 文件夹中的 generic.xaml 文件中定义所有内容可能不切实际.
有哪位大侠可以指点一下吗?我花了几个小时在谷歌上搜索解决方案,none 成功了。我觉得我错过了什么。
提前致谢!
根据@Bizhan 的回答更新了代码
<!-- Generic.xaml file in "Themes" folder -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesigner;component/Themes/RctTextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- WPF App's App.xaml file -->
<Application x:Class="SoftwareThemeDesignerTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesigner;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
编译器似乎找不到资源,因为它没有编译或使用错误的 URI 引用。
首先,确保每个资源字典都有Build Action = Page
和Custom Tool = MSBuild:Compile
然后,确保每个 ResourceDictionary
的 Source
在 两个项目:
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesinger;component/Themes/Generic.xaml" />
注意 1:您可以安全地省略 pack://application:,,,
部分。
注意 2:如果资源字典仅在本地程序集中使用,则包括程序集名称是可选的。但是如果您在另一个程序集中引用资源字典,则必须在源项目和目标项目中包含程序集名称。我还没有找到对此的明确解释,但我认为这种行为是由于 BAML reader 将字典合并在一起,最终它试图在相对路径中找到资源(在本例中是 SoftwareThemeDesinger 项目根目录)
您可以阅读有关 Pack URI 的更多信息here