Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not set a SolidColorBrush resource value from code?

I have a resource defined in my xaml :

<core:WidgetBase xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="....Silverlight.LiquidityConstraintsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:core="clr-namespace:...Silverlight;assembly=....Silverlight"
    xmlns:local="clr-namespace:....Silverlight"
    mc:Ignorable="d">

    <core:WidgetBase.Resources>
        <SolidColorBrush x:Key="..." />
    </core:WidgetBase.Resources>
...

I am trying to set it from code :

void _administrationClient_GetByFilterModuleSettingCompleted(object sender, GetByFilterModuleSettingCompletedEventArgs e)
{
        this.Resources["..."] = new SolidColorBrush(Colors.Red);
}

But I get the error :

The method or operation is not implemented.

stack trace :

   at System.Windows.ResourceDictionary.set_Item(Object key, Object value)
   at ....Silverlight.LiquidityConstraintsView._administrationClient_GetByFilterModuleSettingCompleted(Object sender, GetByFilterModuleSettingCompletedEventArgs e)
   at ....Service.AdministrationServiceClient.OnGetByFilterModuleSettingCompleted(Object state)

It happens when I send off a request to a server to fetch me a colour, then when it returns I try and set that colour to the resource, it fails even if I try and set it as red at that point.

If it at all helps, the method in which I am setting this is an async callback method from WCF call to a server.

like image 824
sprocket12 Avatar asked Sep 04 '13 13:09

sprocket12


2 Answers

If you look at the setter for ResourceDictionary in Reflector (for Silverlight), you'll see it throws a NotImplementedException, so this will not work in Silverlight.

You could try removing the resource and re-adding it, but that's a shot in the dark.

like image 192
RobSiklos Avatar answered Nov 15 '22 08:11

RobSiklos


"This indexer implementation specifically blocks a "set" usage. If you attempt to set a value using the indexer, an exception is thrown. You must remove and re-add to the ResourceDictionary in order to change a key-value pair."

http://msdn.microsoft.com/en-us/library/ms601221(v=vs.95).aspx

like image 25
Andrew Avatar answered Nov 15 '22 06:11

Andrew