Note that there are some explanatory texts on larger screens.

plurals
  1. POMarkup extension 'StaticResourceExtension' requires 'IXamlSchemaContextProvider' be implemented in the IServiceProvider for ProvideValue
    text
    copied!<p>A resource extension I've been using for a few years now stopped working at design time in a new .Net 4 project with the following error:</p> <blockquote> <p>Markup extension 'StaticResourceExtension' requires 'IXamlSchemaContextProvider' be implemented in the IServiceProvider for ProvideValue.</p> </blockquote> <p>The relevant method from the extension is the following:</p> <pre><code> public override object ProvideValue(IServiceProvider serviceProvider) { Style resultStyle = new Style(); foreach (string currentResourceKey in resourceKeys) { object key = currentResourceKey; if (currentResourceKey == ".") { IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); key = service.TargetObject.GetType(); } Style currentStyle = new StaticResourceExtension(key).ProvideValue(serviceProvider) as Style; if (currentStyle == null) throw new InvalidOperationException("Could not find style with resource key " + currentResourceKey + "."); resultStyle.Merge(currentStyle); } return resultStyle; } </code></pre> <p>Presumably, the compiler is giving the error because when I call <code>currentStyle = new StaticResourceExtension(key).ProvideValue(serviceProvider);</code>, the serviceProvider I'm passing along is missing IXamlSchemaContextProvider information. Don't know where it would come from though, I don't even know how the service provider for the markup extension gets set in the first place, I just use it like this:</p> <p><code>&lt;Style x:Key="ReadOnlyTextCell" TargetType="{x:Type TextBlock}" BasedOn="{util:MultiStyle ReadOnlyCell TextCell}"/&gt;</code></p> <hr> <p>The full code for the extension is here:</p> <pre><code>using System; using System.Windows; using System.Windows.Markup; /* MultiStyleExtension - used to merge multiple existing styles. * * Example: &lt;Window xmlns:local="clr-namespace:FlagstoneRe.WPF.Utilities.UI"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="ButtonStyle" TargetType="Button"&gt; &lt;Setter Property="Width" Value="120" /&gt; &lt;Setter Property="Height" Value="25" /&gt; &lt;Setter Property="FontSize" Value="12" /&gt; &lt;/Style&gt; &lt;Style x:Key="GreenButtonStyle" TargetType="Button"&gt; &lt;Setter Property="Foreground" Value="Green" /&gt; &lt;/Style&gt; &lt;Style x:Key="RedButtonStyle" TargetType="Button"&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/Style&gt; &lt;Style x:Key="BoldButtonStyle" TargetType="Button"&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Button Style="{local:MultiStyle ButtonStyle GreenButtonStyle}" Content="Green Button" /&gt; &lt;Button Style="{local:MultiStyle ButtonStyle RedButtonStyle}" Content="Red Button" /&gt; &lt;Button Style="{local:MultiStyle ButtonStyle GreenButtonStyle BoldButtonStyle}" Content="green, bold button" /&gt; &lt;Button Style="{local:MultiStyle ButtonStyle RedButtonStyle BoldButtonStyle}" Content="red, bold button" /&gt; * Notice how the syntax is just like using multiple CSS classes. * The current default style for a type can be merged using the "." syntax: &lt;Button Style="{local:MultiStyle . GreenButtonStyle BoldButtonStyle}" Content="Bold Green Button" /&gt; */ namespace FlagstoneRe.WPF.Utilities.UI { [MarkupExtensionReturnType(typeof(Style))] public class MultiStyleExtension : MarkupExtension { private string[] resourceKeys; /// &lt;summary&gt; /// Public constructor. /// &lt;/summary&gt; /// &lt;param name="inputResourceKeys"&gt;The constructor input should be a string consisting of one or more style names separated by spaces.&lt;/param&gt; public MultiStyleExtension(string inputResourceKeys) { if (inputResourceKeys == null) throw new ArgumentNullException("inputResourceKeys"); this.resourceKeys = inputResourceKeys.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (this.resourceKeys.Length == 0) throw new ArgumentException("No input resource keys specified."); } /// &lt;summary&gt; /// Returns a style that merges all styles with the keys specified in the constructor. /// &lt;/summary&gt; /// &lt;param name="serviceProvider"&gt;The service provider for this markup extension.&lt;/param&gt; /// &lt;returns&gt;A style that merges all styles with the keys specified in the constructor.&lt;/returns&gt; public override object ProvideValue(IServiceProvider serviceProvider) { Style resultStyle = new Style(); foreach (string currentResourceKey in resourceKeys) { object key = currentResourceKey; if (currentResourceKey == ".") { IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); key = service.TargetObject.GetType(); } Style currentStyle = new StaticResourceExtension(key).ProvideValue(serviceProvider) as Style; if (currentStyle == null) throw new InvalidOperationException("Could not find style with resource key " + currentResourceKey + "."); resultStyle.Merge(currentStyle); } return resultStyle; } } public static class MultiStyleMethods { /// &lt;summary&gt; /// Merges the two styles passed as parameters. The first style will be modified to include any /// information present in the second. If there are collisions, the second style takes priority. /// &lt;/summary&gt; /// &lt;param name="style1"&gt;First style to merge, which will be modified to include information from the second one.&lt;/param&gt; /// &lt;param name="style2"&gt;Second style to merge.&lt;/param&gt; public static void Merge(this Style style1, Style style2) { if(style1 == null) throw new ArgumentNullException("style1"); if(style2 == null) throw new ArgumentNullException("style2"); if(style1.TargetType.IsAssignableFrom(style2.TargetType)) style1.TargetType = style2.TargetType; if(style2.BasedOn != null) Merge(style1, style2.BasedOn); foreach(SetterBase currentSetter in style2.Setters) style1.Setters.Add(currentSetter); foreach(TriggerBase currentTrigger in style2.Triggers) style1.Triggers.Add(currentTrigger); // This code is only needed when using DynamicResources. foreach(object key in style2.Resources.Keys) style1.Resources[key] = style2.Resources[key]; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload