Usage of XAML Resource in C# code

I have a Silverlight 5 project with several ResourceDictionary files that I use to store common Styles, Colors, Brushes, etc. These ResourceDictionary files are then merged into the root App.xaml ResourceDictionary so they are available across the whole application.

Here is a BitmapImage defined in my ResourceDictionary:

<BitmapImage x:Key="AccessMethodIcon" UriSource="/Nexus;component/Assets/Images/access-method.png" />

If I reference this resource inside a StaticResource expression in XAML, Resharper recognizes this as a usage of this BitmapImage.

<Image Source="{StaticResource AccessMethodIcon}" />

I can hit F12 on AccessMethodIcon in that line, and it will take me to the definition in my ResourceDictionary. If I use the Find Usages command on the item in my ResourceDictionary, the usage in the Image tag will be listed. That all works great.

The issue is when I want to reference this BitmapImage in C# code.

var accessMethodIcon = (BitmapImage)Application.Current.Resources["AccessMethodIcon"];

In this case, the string "AccessMethodIcon" is a magic string with no checking against the actual names of resources I have defined. When I hit F12 on it, it takes me to the decompiled definition of System.String. When I use the Find Usages command on the item in my ResourceDictionary, this line is not listed.

Could you make it so that Resharper can recognize this as a usage of the item from the ResourceDictionary? Even if there's some way I could do it with an JetBrains.Annotations attribute on a method or something, that would be great.

Please sign in to leave a comment.