Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;找不到资源“;themes.xxx.xaml“&引用;运行时错误-另一个程序集中的WPF自定义组件_C#_Wpf_Xaml_Resourcedictionary - Fatal编程技术网

C# &引用;找不到资源“;themes.xxx.xaml“&引用;运行时错误-另一个程序集中的WPF自定义组件

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

我试图使用在WPF项目的一个类库程序集中创建的自定义控件。但是,在启动应用程序时,我不断遇到运行时错误“找不到资源'themes/rcttextbox.xaml'”

在我的自定义控件库“SoftwareMethedesigner”中,“Themes”文件夹中有以下Generic.xaml代码:

我所尝试的:

  • 将每个资源字典的所有生成操作从“页面”更改为“资源”
  • 添加
    /softwaremedesinger;component/
    为Generic.xaml文件中的每个资源字典添加前缀
  • 将RctTextBox.xaml代码移动到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" />