Advice: The code works fine, this question is about best practices of doing it.
In my project, the fill of the rectangle is a canvas that represents the state of an video, example, if the user click in play, the canvas needs to be changed to something like playing, is the video ends, the canvas needs to be changes to something like stopped. I have all of the canvas that represents this states in one resource dictionary.
My requirement is: i have a ResourceDictionary with a lot of resources, the resources looks like this
<Canvas Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" x:Key="appbar_add">
<Path Width="24" Height="24" Canvas.Left="12" Canvas.Top="12" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 22,12L 26,12L 26,22L 36,22L 36,26L 26,26L 26,36L 22,36L 22,26L 12,26L 12,22L 22,22L 22,12 Z " />
</Canvas>
<Canvas Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" x:Key="appbar_alert">
<Path Width="22.1402" Height="20.75" Canvas.Left="12.9299" Canvas.Top="14" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 24,14C 26,14 36,33 35,34C 34,35 14,35 13,34C 12,33 22,14 24,14 Z M 24,29C 23.1716,29 22.5,29.6716 22.5,30.5C 22.5,31.3284 23.1716,32 24,32C 24.8284,32 25.5,31.3284 25.5,30.5C 25.5,29.6716 24.8284,29 24,29 Z M 22.5,20L 23,28L 25,28L 25.5,20L 22.5,20 Z " />
</Canvas>
<Canvas Width="48.0067" Height="48.0067" Clip="F1 M 0,0L 48.0067,0L 48.0067,48.0067L 0,48.0067L 0,0" x:Key="appbar_alien">
<Path Width="22.005" Height="16.0048" Canvas.Left="12.9999" Canvas.Top="16.9998" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 16.9999,16.9998L 19.0002,16.9998L 19.0026,19.0027L 21.0002,18.9998L 21.0029,21.0029L 27.0038,21.0029L 26.9999,18.9998L 28.9999,18.9998L 28.9999,16.9998L 31.0002,16.9998L 31.0002,19.0001L 29.0002,19.0001L 29.0041,21.0029L 31.0043,21.0029L 31.0043,23.0032L 33.0046,23.0032L 33.0046,25.0035L 31.0043,25.0035L 31.0001,31.0001L 29.0001,31.0001L 29.0001,33.0001L 25.0035,33.0046L 25.0035,31.0043L 28.9999,30.9999L 29.004,28.0039L 19.0026,28.0039L 19.0026,31.0043L 17.0024,31.0043L 16.9999,25.0001L 15.0002,25.0001L 15.0002,29.0001L 12.9999,29.0001L 12.9999,24.9998L 14.9999,24.9998L 14.9999,22.9998L 16.9999,22.9998L 17.0024,21.0029L 19.0026,21.0029L 18.9999,19.0001L 16.9999,19.0001L 16.9999,16.9998 Z M 33.0046,25.0035L 35.0049,25.0035L 35.0049,29.0041L 33.0046,29.0041L 33.0046,25.0035 Z M 19.0026,31.0043L 23.0032,31.0043L 23.0032,33.0046L 19.0026,33.0046L 19.0026,31.0043 Z M 19.0026,23.0032L 19.0026,25.0035L 21.0029,25.0035L 21.0029,23.0032L 19.0026,23.0032 Z M 27.0038,23.0032L 27.0038,25.0035L 29.004,25.0035L 29.004,23.0032L 27.0038,23.0032 Z " />
</Canvas>
...
I want to open this resource, find one resource by name, and apply this resource to a fill of a rectangle. Actually i am doing the following:
private void ChangeFill()
{
Collection<ResourceDictionary> appResources = Application.Current.Resources.MergedDictionaries;
if (Application.Current.Resources.MergedDictionaries.Count > 1)
{
ResourceDictionary recIcones = appResources.Where(r => r.Source.OriginalString == "Resources/Icons.xaml").FirstOrDefault(); // get the properly resource dictionary
if (recIcones != null)
{
Canvas x = (Canvas)recIcones["appbar_add"]; // find a espicified resource by name
VisualBrush vBrush = new VisualBrush(x);
rctParado.Fill = vBrush; // apply it to the rectangle
}
}
}
This works fine, the question is:
Is this the better way to do it?
Just call
FindResourceon something in your visual tree. This will automatically search for a resource, recursing up the visual tree as necessary until it finds it (including looking in your app-level resources). If you only want to search in the application’s resources, use theFindResourcemethod on theApplicationobject.As for dynamically assigning the resource, it’s likely you could achieve the same thing more cleanly and elegantly via data binding. That is, bind the
Canvas‘Styleproperty to whatever it is that instigates the change in style, and use a converter to do the resource lookup. It’s hard to be more specific without the full details of your scenario.