【问题标题】:ResourceDictionary in separate library单独库中的资源字典
【发布时间】:2015-09-25 22:45:21
【问题描述】:

我在一个单独的库中定义了我的资源字典,如下所示

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cultures="clr-namespace:My_Localization.Cultures"
    xmlns:properties="clr-namespace:My_Localization.Properties">

    <ObjectDataProvider x:Key="Resources" ObjectType="{x:Type cultures:CultureResources}" MethodName="GetResourceInstance"/>

    <ObjectDataProvider x:Key="CultureResourcesDS" ObjectType="{x:Type cultures:CultureResources}"/>

</ResourceDictionary> 

我已经从另一个库中使用了这个库,如下所示(仅限 xaml 的标题)

<msRibbon:RibbonTab x:Class="Ribbon.Planner.PlannerRibbon"
    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:msRibbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    mc:Ignorable="d" 
    d:DesignHeight="100" d:DesignWidth="500" Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}" 
    >
    <Grid>
     ...
     ...
     ...
    </Grid>

我已经添加了 My_Localization 库的引用,我只是更改了标题。一切正常,但唯一的问题是,在设计时,我在“Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}" 下划线。当我将鼠标悬停时,提示“资源“资源”不能解决”

为什么我的 xaml 中出现类似提示的错误?那为什么一切正常?

我的解决方案结构

  1. MainExe - 包含 app.xaml。我在这里合并了资源字典。 xaml 中没有问题,因为 app.xaml 中存在合并字典
  2. My_Localization - 包含资源字典的库(上面的代码)
  3. Lib1 - 引用 My_Localization 并且 xaml 中存在问题,如解释
  4. Lib2 - 引用 My_Localization 并且 xaml 中存在问题,如所述
  5. Lib3 - 引用 My_Localization 并且 xaml 中存在问题,如所述

【问题讨论】:

  • 因为它只在运行时合并字典。
  • 我是这么认为的,但是 xaml 显示的设计时间好像有错误是正确的吗?有没有办法防止这种情况发生?

标签: c# wpf xaml


【解决方案1】:

您需要在app.xaml 文件中或本地提供对ResourceDictionary 的引用。 app.xaml 中的资源可全局用于应用程序的所有 xaml 文件。

对于库项目中的 Xaml 文件,设计器的工作方式略有不同。

在运行时它将是启动项目中的app.xaml,它将用于所有程序集。在设计时它将是本地程序集的app.xaml。 这意味着,您可以将 app.xaml 文件添加到库中,该文件仅在 Visual Studio 设计器从该特定库中呈现 xaml 文件时使用(将文件的构建操作设置为 Page)。

要引用ResourceDictionary,请执行以下操作:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Application.Resources>
        <ResourceDictionary>

            <!-- Other global resources -->

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ASSEMBLYNAME;component/FOLDERPATH/RDNAME.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

其中 ASSEMBLYNAME 是 ResourceDictionary 所在的程序集的名称(检查项目属性)。

例子:

具有程序集名称的项目:“MyAssembly”和文件夹路径“Resources/RDs/MyRd.xaml”中的ResourceDictionary

Source="pack://application:,,,/MyAssembly;component/Resources/RDs/MyRd.xaml"

【讨论】:

  • 如何将 app.xaml 添加到库中?你的意思是只添加一个类并将其命名为 app.xaml 吗?没有 app.cs?
  • 没错。只是 xaml 部分。如果将名为app.xaml 的ResourceDictionary 添加到库项目的根目录,然后将xaml 节点更改为&lt;Application&gt;,则它将起作用。
  • 我的库是一个类库,因此在应用程序“未定义默认命名空间”时出现错误
  • 尝试添加这个:&lt;Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
【解决方案2】:

您可以在每个 xaml 中包含 ResourceDictionary

<msRibbon:RibbonTab x:Class="Ribbon.Planner.PlannerRibbon"
    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:msRibbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    mc:Ignorable="d" 
    d:DesignHeight="100" d:DesignWidth="500" Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}">
    <msRibbon:RibbonTab.Resources>
        <ResourceDictionary Source="pack://application:,,,/<ASSEMBLY_NAME>;component/<RESOURCES_FILE>.xaml"/>
    </msRibbon:RibbonTab.Resources>
    <Grid>
     ...
     ...
     ...
    </Grid>

【讨论】:

  • 我试过这个并且可以工作,但是如果我在运行时更改语言,它不会改变。
  • @user2837961:改用DynamicResource
  • 恐怕不行。但是您将 app.xml 添加到库的解决方案工作正常。我正在使用它。
【解决方案3】:

App.xaml 中,您必须将MergedDictionary 添加到将引用您的其他字典的资源中。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RibbonControlsLibrary;component/Your/Dictionary/Path.xaml" />
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 我的应用程序至少有 5-6 个库,我想在每个库中使用 My_Localization 库。现在这些库不包含 app.xaml
  • @eranotzap 啊,我不知道这一点。 OP,在这种情况下,您应该将合并的字典放入您的 RibbonTab Resources 中。
  • @user2837961 你做了迈克写的吗?你如何消耗你的资源..?
  • 在使用类库和 xaml 时有一个简单的解决方案。检查我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
相关资源
最近更新 更多