ResourceDictionary.MergedDictionaries属性有一个奇怪的问题 [英] An odd problem with ResourceDictionary.MergedDictionaries property
问题描述
我在主题文件夹中制作了两个xaml资源文件
generic.xaml
I made two xaml resource files in themes folder
generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="themes/msofficeui.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
msofficeui.xaml
msofficeui.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:System.Windows.Resources.Libraries">
<Color A="#FF" R="#BF" G="#BF" B="#BF" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ColorLibrary}, ResourceId=MSOLightGray}"/>
<Color A="#FF" R="#91" G="#91" B="#91" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ColorLibrary}, ResourceId=MSOGray}" />
<Color A="#FF" R="#F9" G="#E1" B="#89" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ColorLibrary}, ResourceId=MSOLightGold}" />
<Color A="#FF" R="#FF" G="#D8" B="#6C" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ColorLibrary}, ResourceId=MSOGold}" />
<Color A="#FF" R="#38" G="#38" B="#38" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ColorLibrary}, ResourceId=MSOBlack}" />
<LinearGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:BrushLibrary}, ResourceId=MSORibbonBackgroundGray}"
StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FFBFBFBF" Offset="0" />
<GradientStop Color="#FF919191" Offset="1" />
</LinearGradientBrush>
<RadialGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:BrushLibrary}, ResourceId=MSOButtonBackgroundEnter}"
GradientOrigin = "0.5,1.02" Center="0.5,1.02" RadiusX="0.8" RadiusY="0.2">
<GradientStop Color="#FFFFFFFF" Offset="0" />
<GradientStop Color="#FFF9E189" Offset= "1" />
</RadialGradientBrush>
<RadialGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:BrushLibrary}, ResourceId=MSOButtonBackgroundPressed}" Center="0.5,1.5">
<GradientStop Color="#FFFFFFFF" Offset="0" />
<GradientStop Color="#FFFFD86C" Offset= "0.55" />
</RadialGradientBrush>
</ResourceDictionary>
当我尝试使用应用程序中的一种资源时,收到如下消息:
无法找到资源"themes/msofficeui.xaml".
在MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode模式,FileAccess访问)处
在System.IO.Packaging.PackagePart.GetStream(FileMode模式,FileAccess访问)处
在System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
在System.IO.Packaging.PackWebResponse.GetResponseStream()
在System.IO.Packaging.PackWebResponse.get_ContentType()
在MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse响应)
在MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest请求,ContentType& TypeType)
在System.Windows.ResourceDictionary.set_Source(Uri值)
在System.Windows.Baml2006.WpfSharedBamlSchemaContext中.< Create_BamlProperty_ResourceDictionary_Source> b__1c4(对象目标,对象值)
在System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(对象实例,对象值)
在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember成员,Object obj,Object值)
在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst,XamlMember属性,Object value)
我将在general.xaml中提到的msofficeui.xaml的uri更改为绝对包Uris和相对包Uris,但仍然抛出相同的异常.
我直接将msofficeui.xaml的内容直接放入通用文件中,而不使用MergedDictionaries,然后一切正常.所以这绝对是一个Uri问题.
uri有什么问题?我花了几个小时.我尽了我所能想象的一切.现在我很生气.....
when i tried to use one of the resources in my app, I got a message as follows:
Cannot locate resource ''themes/msofficeui.xaml''.
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.get_ContentType()
at MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse response)
at MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest request, ContentType& contentType)
at System.Windows.ResourceDictionary.set_Source(Uri value)
at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_ResourceDictionary_Source>b__1c4(Object target, Object value)
at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
I changed the uri referred to msofficeui.xaml in generic.xaml to both absolute and relative pack Uris, but the same exception continued to be thrown out.
I put the stuff of msofficeui.xaml directly in generic without using MergedDictionaries, then everything goes fine. So it is definitely a Uri problem.
What''s wrong with the uri? It has taken me a couple of hours. I tried every way I can imagine. Now I''m mad.....
推荐答案
由于它们都在Themes目录中,因此您实际上是在告诉
ResourceDictionary
合并可以修复文件夹"themes/themes/msofficeui.xaml"中的ResourceDictionary
Since they are both in the Themes directory you are actually telling the
ResourceDictionary
to merge theResourceDictionary
of the folder "themes/themes/msofficeui.xaml" to fix it you can
- 从"themes/msofficeui.xaml"更改为仅"msofficeui.xaml"
- 从"themes/msofficeui.xaml"更改为"/AssemblyName;component/themes/msofficeui.xaml"
在第二种方法中,您被迫要求从Assembly
资源中查找正确的信息,当在不同文件夹中使用多个ResourceDictionaries
时,这特别有用.
In the seccond approach you are forcing to ask from the Assembly
resources to find the correct info, this is specialy useful when working with multiple ResourceDictionaries
in different folders.
代码:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AssemblyName;component/themes/msofficeui.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
希望这会有所帮助
万事如意
Raul Mainardi Neto
Raul Mainardi Neto
我最近遇到了这个问题,第一个答案是您应该做的,但是我编写了自动工具,可以提供一些解释.请参阅矢量画笔库
谢谢
肯
I ran into this problem recently, The first answer is what you should do, however I wrote and automated tool that could help along with some explanation. Please see Vector Brush Library
Thanks
Ken
这篇关于ResourceDictionary.MergedDictionaries属性有一个奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!