C# &引用;找不到资源“;themes.xxx.xaml“&引用;运行时错误-另一个程序集中的WPF自定义组件
我试图使用在WPF项目的一个类库程序集中创建的自定义控件。但是,在启动应用程序时,我不断遇到运行时错误“找不到资源'themes/rcttextbox.xaml'” 在我的自定义控件库“SoftwareMethedesigner”中,“Themes”文件夹中有以下Generic.xaml代码: 我所尝试的:C# &引用;找不到资源“;themes.xxx.xaml“&引用;运行时错误-另一个程序集中的WPF自定义组件,c#,wpf,xaml,resourcedictionary,C#,Wpf,Xaml,Resourcedictionary,我试图使用在WPF项目的一个类库程序集中创建的自定义控件。但是,在启动应用程序时,我不断遇到运行时错误“找不到资源'themes/rcttextbox.xaml'” 在我的自定义控件库“SoftwareMethedesigner”中,“Themes”文件夹中有以下Generic.xaml代码: 我所尝试的: 将每个资源字典的所有生成操作从“页面”更改为“资源” 添加/softwaremedesinger;component/为Generic.xaml文件中的每个资源字典添加前缀 将RctText
/softwaremedesinger;component/
为Generic.xaml文件中的每个资源字典添加前缀
编译器似乎找不到该资源,因为它没有被编译,或者被错误的URI引用
首先,确保每个资源字典都有buildaction=Page
和customtool=MSBuild:Compile
然后,确保每个ResourceDictionary
的Source
在两个项目中都设置了完整的包URI:
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesinger;component/Themes/Generic.xaml" />
注1:您可以安全地省略pack://application:,,,
零件
注2:如果资源字典仅在本地程序集中使用,则包含程序集名称是可选的。但是,如果您在另一个程序集中引用一个资源字典,则必须在源项目和目标项目中包含程序集名称。对此我没有找到明确的解释,但我认为这种行为是由于BAML reader将字典合并在一起,所以最终它会尝试在相对路径中查找资源(在本例中,它是SoftwareMetedesinger项目根目录)
你可以阅读更多关于包URI的信息这是否回答了你的问题?也请阅读@Bizhan谢谢你的快速回复。我也尝试过应用程序打包,但没有成功。样式在库本身工作吗?是的,样式在库本身工作。为了简单和其他尝试,我将修改此问题d actions.Perfect.不知道如果要在外部程序集中使用源程序集,就必须引用源程序集本身。我将用您答案中的更新代码更新我的问题。非常感谢您的帮助,@Bizhan
<!-- 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>
<!-- 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>
<!-- 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>
<ResourceDictionary Source="pack://application:,,,/SoftwareThemeDesinger;component/Themes/Generic.xaml" />