我试图在我的WPF项目中使用我在一个类库程序集中创建的自定义控件。然而,当我启动应用程序时,我不断地收到"无法定位到资源 '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>
和ContentTemplate的资源字典,这是在Visual Studio中创建"Custom Control"文件时的默认设置。
在我的"WPF应用程序(SoftwareThemeDesignTester)"中,引用了我的"软件主题设计师(SoftwareThemeDesigner)"库,在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”文件夹中定义所有内容可能不可行。
请问有人能提供建议吗?我已经花费数小时在谷歌上寻找解决方案,但都没有成功。感觉好像少了点什么。
提前感谢!
根据@ 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>