【问题标题】:Using Resource Dictionaries in a Class Library在类库中使用资源字典
【发布时间】:2013-04-02 18:43:49
【问题描述】:

我在我的应用程序中使用this (excellent) flowchart diagram designer,但我想将其用作UserControl

要将Application 转换为UserControl,我更改了应用程序的唯一窗口:

<Window x:Class="DiagramDesigner.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:DiagramDesigner"
    xmlns:c="clr-namespace:DiagramDesigner.Controls"
    WindowStartupLocation="CenterScreen"
    Title="Diagram Designer"
    Height="850" Width="1000">
  <Window.Resources>
    <ContextMenu x:Key="DesignerCanvasContextMenu">
      ...
    </ContextMenu>
  </Window.Resources>
  ...
</Window>

进入用户控件:

<UserControl x:Class="DiagramDesigner.DiagramDesignerWPFControl"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:s="clr-namespace:DiagramDesigner"
     xmlns:c="clr-namespace:DiagramDesigner.Controls"
     Height="850" Width="1000">
  <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          ...
        </ResourceDictionary.MergedDictionaries>
        <ContextMenu x:Key="DesignerCanvasContextMenu">
         ...
        </ContextMenu>
    </ResourceDictionary>
  </UserControl.Resources>
  ...
</UserControl>

我从App.xaml 的内容中取出ResourceDicctionary 并将其添加到控件中。然后我删除了App.xaml文件,因为它不能在Class Library编译中使用。


我的问题是

当我将新的User Control 添加到另一个项目中的 WPF 表单时,我可以运行新应用程序,我可以添加图表组件并移动它们,但是当我加入/链接时会引发以下异常:

找不到名为“{SolidBorderBrush}”的资源。资源名称区分大小写。

我的 User Control 中的资源或它们的位置有什么问题?


接受答案后的版本:

引发的异常还指向调用了“{SolidBorderBrush}”的行。我最初并没有把它放在这个问题中,因为它是一个电话而不是一个声明。这是链接异常的一段代码:

<Trigger Property="IsMouseOver" Value="true">
   <Setter TargetName="Border" Property="Background" Value="{DynamicResource ToolbarSelectedBackgroundBrush}" />
   <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource SolidBorderBrush}" />
</Trigger>

【问题讨论】:

  • 你说你从 App.xaml 中获取了 ResourceDictionary,但你没有说你把它放在哪里。更多关于资源字典、它们是什么以及如何使用它们:blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/…(尤其是Resources are Declared at Different Scopes
  • 哦,我把它放在UserControl。在我粘贴的代码中可以看到。我会更清楚地解决这个问题。
  • 我的错...我没有仔细查看代码。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • @J.A.I.L. - 无论如何,您的问题与 C# 没有任何关系。如果您使用 VB 或 F# 进行开发,一切都不会改变(问题或答案)。

标签: wpf flowchart


【解决方案1】:

我在这里猜测,因为您的问题实际上并未显示任何似乎导致问题的代码,但您可能需要使用 DynamicResource

{DynamicResource SolidBorderBrush}

您只能在非常特殊的情况下使用 StaticResource。大多数情况下,您的性​​能都会得到很大提升,但很容易陷入无法使用的情况(这可能是已经发生的情况)。

【讨论】:

  • 你猜对了!问题来自 SolidBorderBrushStaticResource 声明。不,我必须将项目中的大部分(如果不是全部)StaticResource 声明更改为DynamicResource。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-11-14
  • 2011-05-25
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2015-09-25
相关资源
最近更新 更多