【问题标题】:StaticResource not found when using WPF control in another assembly在另一个程序集中使用 WPF 控件时找不到 StaticResource
【发布时间】:2021-01-07 08:22:53
【问题描述】:

我创建了一个 WPF UserControl 并想在另一个项目中引用它。 但是即使UserControl 可以在自己的程序集中使用,当在其他项目中引用时,会出现StaticResource not found 问题。

我的UserControl 看起来像这样:

<UserControl x:Class="DiagramDesigner.VacSymGrmV"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DiagramDesigner"
             xmlns:s="clr-namespace:DiagramDesigner"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <local:DesignerCanvas Focusable="true"  x:Name="vacgrm"                         
                            Background="{StaticResource WindowBackgroundBrush}"
                            Margin="10" FocusVisualStyle="{x:Null}"
                            ContextMenu="{StaticResource DesignerItemContextMenu}"/>
    </Grid>
</UserControl>

问题点是:{Static Resource WindowBackgroundBrush},这是一个XAML文件中定义的资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
   <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
   <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#000" />
   <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
</ResourceDictionary>

这个程序集是一个EXE,在UserControl的设计窗口中,我可以看到它正确显示,并且编译并运行。

但是当我想在另一个 WPF 应用程序中使用 UserControl 时,就像在下面的代码中一样,这个应用程序在启动时抛出一个异常,声称找不到 WindowBackgroundBrush

<Window x:Class="XVacSysComp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:vac="clr-namespace:DiagramDesigner;assembly=VacSymDgm"
        xmlns:local="clr-namespace:XVacSysComp;assembly=XVacSysComp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <vac:VacSymGrmV>
  
        </vac:VacSymGrmV>
    </Grid>
</Window>

VacSymGrmVUserControl 并且毫不奇怪,UserControl 没有正确显示在设计窗口中。

异常说:

Exception: 'WindowBackgroundBrush' is not found, 注意大写/小写...

在另一个程序集中使用静态资源时,这是与静态资源的位置有关的问题吗?

【问题讨论】:

  • 该 ResourceDictionary 是否在任何地方导入,例如添加到 UserControl 的资源的MergedDictionaries 中?
  • UserControl 没有自己的资源。事实上,“
  • 哦,真的,包含WindowBackgroundBrush的资源xaml出现了一些其他的资源文件。

标签: wpf xaml staticresource


【解决方案1】:

异常告诉你确切的问题,WindowBackgroundBrush 没有找到。

'WindowBackgroundBrush'没有找到,注意大写/小写...

资源在资源字典中定义,您在包含程序集 (EXE) 的任何位置合并,例如应用程序资源。如果你在那里使用用户控件,静态资源可以被解析。

但是,如果您使用来自不同应用程序的用户控件,该应用程序将如何自动访问来自任何其他程序集的资源?即使您的用户控件位于原始应用程序的程序集 (EXE) 中,仅从不同的应用程序或库引用它也不会引导其他 WPF 应用程序或神奇地将引用的资源从那里添加到正在执行的程序集。

解决方法很简单,将对应的资源字典合并到其他应用的应用资源字典或资源解析范围内的不同资源字典。在共享资源的情况下,考虑创建一个可以被不同项目引用的专用程序集。

<ResourceDictionary>
   <!-- ...other resources. -->
   <ResourceDictionary.MergedDictionaries>
      <!-- ...other resource dictionaries. -->
      <ResourceDictionary Source="pack://application:,,,/MySharedAssembly;component/MySharedResourceDictionary.xaml" />
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

有关引用resource dictionary from a referenced assembly 和示例中使用的源语法的更多信息,请查看pack URI documentation

根据您的场景,您还可以将资源与您的用户控件合并并提供,但我想对于有问题的资源这不适用,因为它是应用程序背景。

【讨论】:

  • 我真的想当然地认为驻留在同一个程序集中会给用户直接访问资源的控制权限,事实并非如此。非常感谢。
  • @Chengting stackoverflow 建议标记可接受的答案,谢谢,它会帮助其他人找到好的答案
【解决方案2】:

你应该将你的资源字典导入你的 UserControl

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多