A small C# console application which builds static resource references from ResourceDictionaries in WPF applications. This allows XAML-defined resources to be referenced in a strongly-typed way in C#, without using the FindResource("Name") method.
.\ReSource.CLI.exe <source-csproj-path> <main-source-assembly> <destination-cs-file-path> <namespace>
The best way to run this is to set it as pre-build command in project properties -> Build events -> Pre-build event command line box, example:
ReSource.CLI.exe $(ProjectPath) $(TargetPath) $(ProjectDir)\R.cs $(ProjectName).R
or directly in the .csproj file:
<TargetName="PostBuild"AfterTargets="PostBuildEvent">
<ExecCommand="path\\to\\ReSource.CLI.exe $(ProjectPath) $(TargetPath) $(ProjectDir)\\R.cs $(ProjectName).R" />
</Target>Consider the following App.xaml file:
<Applicationxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"x:Class="TCC.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionarySource="ResourceDictionaries/SVG.xaml"/>
<ResourceDictionarySource="ResourceDictionaries/Colors.xaml"/>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>The script will parse it as XML and produce the following output:
//////////////////////////////////////////////////////// File automatically generated from App.xaml ////////////////////////////////////////////////////////namespaceTCC.R{publicclassSVG:RH{publicstaticGeometrySvgClose=>Get<Geometry>("SvgClose");
...}publicclassColors:RH{publicstaticColorCardDarkColor=>Get<Color>("CardDarkColor");
...}
...publicclassRH{protectedstaticTGet<T>(stringres){return(T)Application.Current.FindResource(res);}}}Resources can then be referenced directly:
// + NORMAL WAY// - prone to typos// - need to know resource type beforehand to properly cast it// - can cause runtime exceptionsvarres=((Color)App.Current.FindResource("HpColor"));// + STRONGLY-TYPED WAY// - no typos due to not using string name directly// - type already known// - runtime exceptions only if the resource file is not re-generated before build (which shouldn't happen)varres=TCC.R.Colors.HpColor;- Resources defined directly in the
App.xamlfile are not parsed
This WPF application consists of a single window which shows all the PathGeometry resources defined in the target assembly and its dependencies. It can be used as a Visual Studio external tool (Tools menu).
Hovering an icon will display its key and the <Assembly>_<ResourceDictionary> it's from. Clicking the icon will copy it's key to the clipboard.
Some resource dictionaries might not be parsed due to the following reasons:
- mismatch between SvgViewer's and target's (or dependencies') .NET version
- mismatch between
.csprojname and output file name
