Click here to Skip to main content
15,887,425 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I made two xaml resource files in themes folder

generic.xaml

XML
<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

XML
<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>


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.....
Posted

Since they are both in the Themes directory you are actually telling the ResourceDictionary to merge the ResourceDictionary of the folder "themes/themes/msofficeui.xaml" to fix it you can



  1. change from "themes/msofficeui.xaml" to just "msofficeui.xaml"
  2. change from "themes/msofficeui.xaml" to "/AssemblyName;component/themes/msofficeui.xaml"

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.



The code:


XML
<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>


Hope this helps


All Best,


Raul Mainardi Neto

 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900