WPF合并资源字典没有正在从App.xaml中认可字典、资源、WPF、App

2023-09-03 08:56:08 作者:你别皱眉

我有一个WPF的.NET 4.5应用程序,其中我有麻烦合并资源字典。

I have a WPF .net 4.5 application where I am having trouble merging resource dictionaries.

我有相同的问题,因为此等问题和的这个问题但接受的解决方案并没有为我工作。

I have the exact same problem as This SO question and This Question but the accepted solution does not work for me.

我在App.xaml中声明的资源字典如下(简化为清楚起见):

I have a resource dictionaries declared in my app.xaml as follows (simplified for clarity):

<Application.Resources>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skin/ResourceLibrary.xaml" />              
            <ResourceDictionary Source="Skin/Brushes/ColorStyles.xaml" />               
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>               
</Application.Resources>

问题: 通过应用程序和错误有关缺少静态资源出现的应用程序可以看到当App.xaml中列出的ColorStyles dictonary,但如果我移动/窝吧ResourceLibrary.xaml里面,然后ColorStyles.xaml没有见过。

Problem: The app can "SEE" the ColorStyles dictonary when listed in app.xaml, but if I move/nest it inside the ResourceLibrary.xaml, then the ColorStyles.xaml are not "seen" by the app and errors about missing static resources appear.

下面是我如何创建ResourceLibrary.xaml词典(简体):

Here is how I create the ResourceLibrary.xaml dictionary (simplified):

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

    <ResourceDictionary.MergedDictionaries>

        <!--  BRUSHES AND COLORS  -->
        <ResourceDictionary Source="Brushes/ColorStyles.xaml" />

    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

更改原因:我目前我的资源字典的组织是可怕的,我需要去改变它(因为我创建对象一次以上)。我想有一个皮肤文件夹一个资源字典,然后子文件夹组织剩余的风格字典,将在这反过来将被称为App.xaml中的ResourceLibrary.xaml文件中的所有合并。

Reason for change: My current organization of my resource dictionaries is awful and I need to change it (as I am creating objects more than once). I wanted to have one resource dictionary in a "Skin" folder and then sub-folders for organizing the remaining style dictionaries which would all be merged in the ResourceLibrary.xaml file which in turn would be called in app.xaml.

我的尝试: 是的,我也尝试使用从上面的链接的解决方案:

What I tried: Yes I did try to use the solution from the link above:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skin/ResourceLibrary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>

但我得到的虚拟款式线以下错误:

but I get the following error on the dummy style line:

错误2属性的元素不能在一个元素的中间   内容。他们必须是前或后的内容。

Error 2 Property elements cannot be in the middle of an element's content. They must be before or after the content.

修改1:

改变code到以下摆脱了错误的上面,由于口齿不清评论:

changing the code to the following got rid of the error above, thanks to lisp comment:

<Application.Resources>
    <ResourceDictionary>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skin/ResourceLibrary.xaml"></ResourceDictionary>             
            <ResourceDictionary Source="Skin/Brushes/ColorStyles.xaml" />
            <ResourceDictionary Source="Skin/NamedStyles/AlertStyles.xaml" />

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>               
</Application.Resources>

但资源库仍然没有被调用。

but the Resource Library is still not being called.

END编辑1:

我也试图改变所有的文件路径收拾URI的,但这并没有解决此问题有两个。

I also tried to change all the file paths to pack URI's, but that did not solve the problem either.

编辑2:

我试图移动resourceLibrary.xaml和其他资源字典到不同的类库项目(使用相同的文件夹结构和文件如上)。然后我用下面的URI,但我依然无法访问在ResourceLibrary.xaml文件中声明的资源。

I tried moving the resourceLibrary.xaml and the other resource dictionaries into a different class library project (using the same folder structure and files as above). I then used the following URI but I still am not able to access resources declared in the ResourceLibrary.xaml file.

<ResourceDictionary Source="pack://application:,,,/FTC.Style;component/ResourceLibrary.xaml" />

但同样,如果我每个资源字典添加到App.xaml文件,使用上面的快速反应部队的格式,资源是可用的。

But again, if I add each resource dictionary to the App.Xaml file, using the UIR format above, the resources are usable.

END EDIT 2

1编辑后,错误没有了,但我仍无法使用是在ResourceLibrary.xaml文件合并字典的一部分资源。我倾向于同意dowhilefor的意见,以我是否应该使用这种方法,但我想这出,因为最常见的解决了这个问题(见在这个岗位上面的链接)不工作,也许该解决方案可以帮助别人。

After Edit 1, the error is gone, but I am still unable to use resources that are a part of the merged dictionary in the ResourceLibrary.xaml file. I am inclined to agree with the comment of dowhilefor as to whether or not I should use this approach, but I want to figure this out because the most common solution to this problem (see links at top of this post) is not working and maybe this solution could help someone else.

问:为什么被忽略的ResourceLibrary.xaml文件

Question: Why is the ResourceLibrary.xaml file being ignored?

推荐答案

我哈瓦一个大问题MergedDictionaries,我相信,你的问题是一样的。 我希望我的ResourceDictionaries进行适当的组织,这意味着对我来说,有例如单独Buttons.xaml,TextBoxes.xaml,Colors.xaml等。我在Theme.xaml它们合并,往往所有的风格都在一个单独的组件(这样我就可以很容易地切换主题)。我的ApplicationResources如下:

I hava a big problem with MergedDictionaries and I believe that your problem is the same. I want my ResourceDictionaries to be properly organized, which means for me that there are for example seperate Buttons.xaml, TextBoxes.xaml, Colors.xaml and so on. I merge them in Theme.xaml, often all the Styles are in a seperate assembly (so that I could easily switch Themes). My ApplicationResources are as follows:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/DefaultTheme;component/Theme.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="{x:Type Ellipse}"/>
  </ResourceDictionary>
</Application.Resources>

和每一个的StaticResource在主应用程序组件的工作定义的.xamls,默认样式的工作多亏了伪风格。 有什么不工作是.xamls之间StaticResources主题里面。如果我定义了使用从Colors.xaml一个的StaticResource在Buttons.xaml样式,我得到StaticResources和UnsetValue错误。它的工作原理,如果我加入Colors.xaml到应用MergedDictionaries。

And every StaticResource in the .xamls defined in the main application assembly work, default styles work thanks to the dummy Style. What doesn't work are StaticResources between .xamls inside of Theme. If I define a Style in Buttons.xaml that uses a StaticResource from Colors.xaml, I get an error about StaticResources and UnsetValue. It works if I add Colors.xaml to Application MergedDictionaries.

放弃组织。把一切都在一个的.xaml。我相信这是怎么ResourceDictionaries一般都应该使用,因为所有的问题与MergedDictionaries(对我来说这将是一场噩梦)。

Abandon organization. Put everything in one .xaml. I believe that is how the ResourceDictionaries were generally supposed to be used because of all the 'problems' with MergedDictionaries (for me this would be a nightmare).

更改所有交叉XAML的StaticResource引用主题里面DynamicResource。它的工作原理,但配备了一个价格作为DynamicResources比StaticResources'更重'。

Change all cross-xaml StaticResource references inside of theme to DynamicResource. It works but comes with a price as DynamicResources are 'heavier' than StaticResources.

在使用StaticResources从其他的.xaml每一个主题的.xaml,加上这另外的ResourceDictionary来MergedDictionaries。这意味着,Buttons.xaml,TextBoxes.xaml和其他人将不得不Colors.xaml在他们MergedDictionaries。这将导致颜色的ResourceDictionary被存储在内存中的多个副本。要避免这种情况,你可能想看看 SharedResourceDictionary 。

In every Theme .xaml that uses StaticResources from another .xaml, add this another ResourceDictionary to MergedDictionaries. That means that Buttons.xaml, TextBoxes.xaml and others would have Colors.xaml in their MergedDictionaries. It will result in Colors ResourceDictionary being stored in memory in multiple copies. To avoid that you might want to look into SharedResourceDictionary.

通过不同ResourceDictionaries设置,不同的嵌套我想出了一个理论的:

By different ResourceDictionaries setup, different nestings I came up with a theory:

如果没有在上面发现在同一的.xaml或本ResourceDictionary中的MergedDictionaries一个的StaticResource,就搜索的其他顶级MergedDictionaries 的。

If a StaticResource isn't found above in the same .xaml or in the MergedDictionaries of this ResourceDictionary, it is searched in other top-level MergedDictionaries.

我会preFER添加到ApplicationResources只有一个的.xaml,但我通常最终会使用的两个。你不必添加到ApplicationResources每.XAML,你在主题,只是 - 例如 - Controls.xaml(与任何一种MergedDictionaries嵌套的,但Controls.xaml辞书之间没有交叉引用是允许的),并Common.xaml它包含控件的所有公共资源。如果Common.xaml嵌套也是允许的,但没有交叉引用,不能有独立的Colors.xaml和Brushes.xaml使用颜色StaticResources - 那么,你就必须有3 .xamls添加到应用程序MergedDictionaries

I would prefer to add to ApplicationResources only one .xaml, but I usually end up using two. You dont have to add to ApplicationResources every .xaml that you have in Theme, just - for example - Controls.xaml (with any kind of MergedDictionaries nesting, but no cross-references between Dictionaries of Controls.xaml are allowed) and Common.xaml which contains all common Resources of Controls. In case of Common.xaml nesting is also allowed, but no cross-references, there cannot be seperate Colors.xaml and Brushes.xaml that uses Colors as StaticResources - then you would have to have 3 .xamls added to Application MergedDictionaries.

现在我总是用第三个解决方案,但我不认为这是完美的,还是想知道是否有更好的方法。我希望我正确地跨preTED你描述为同样的问题,什么是我的。

Now I always use the third solution, but I don't consider it perfect and still would like to know if there is a better way. I hope I correctly interpreted what you described as the same problem as mine.