单独库中的ResourceDictionary [英] ResourceDictionary in separate library

查看:52
本文介绍了单独库中的ResourceDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在下面的单独库中定义了resourceDictionary

I have defined my resourceDictionary in a separate library as below

<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的标题)

I have used this library from another library as below (Header of xaml only)

<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 lib的引用,而我只是更改了标题.一切正常,但唯一的问题是在设计时我在下划线标有"Header ="{Binding Path = Ribbon_About,Source = {StaticResource Resources}}".当我将鼠标悬停时,提示资源"Resources"无法得到解决"

I have added the reference of My_Localization lib and I am only changing the header. All works fine but the only problem is that in design time I have "Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}" underlined. When I hover my mouse there is hint "The resource "Resources" could not be resolved"

为什么我的xaml中出现诸如提示的错误?然后为什么一切正常?

Why is there an error like hint in my xaml? and then why does it all work fine?

我的解决方案结构

  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中存在问题,如所解释的

推荐答案

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

You need to provide a reference to the ResourceDictionary either in the app.xaml file or locally. Resources in app.xaml are available globally to all xaml files of the application.

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

For Xaml files in library projects the designer works a little differently.

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

At runtime it will be the app.xaml in the startup project that will be used for all assemblies. At designtime it will be the app.xaml of the local assembly. This means, you can add a app.xaml file to libraies, which will only be used by the Visual Studio Designer when rendering xaml files from that specific library (Set build action of file to Page).

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

To reference a ResourceDictionary do this:

<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 是该程序集的名称(检查项目的属性).

Where ASSEMBLYNAME is the name of the assembly where the ResourceDictionary is (check properties of project).

示例:

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

Project with assembly name: "MyAssembly" and ResourceDictionary in folder path "Resources/RDs/MyRd.xaml"

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

这篇关于单独库中的ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆