Reputation: 9638
I have a xaml file with the following contents
Icon.xaml
<Rectangle xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Icon"
Width="16" Height="16">
<!-- Fancy DrawingBrush here to make a sweet icon -->
</Rectangle>
As you can see the there is no resource dictionary or custom class in the file.
Now I would like to include Icon.xaml in a resource dictionary and then use it everywhere in my code:
<Button Content="{StaticResource Icon}"/>
However, I do not understand how I can tell a resource dictionary to just include the contents of a plain XAML file.
Note that I'm not trying to load the XAML file at runtime, Icon.xaml is compiled into the application.
Upvotes: 0
Views: 1619
Reputation: 9638
It took some adjustments but this was the code I finally settled on:
I removed the bounding rectangle from the DrawingBrush since that is the only thing I realy wanted:
Icon.xaml
<DrawingBrush xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
<GeometryDrawing Brush="{StaticResource IconForegroundBrush}" Geometry="F1M8.999,1C7.814,1,6.666,1.422,5.768,2.188L3.992,3.692 3.992,1 1.994,3 1.994,5 1.994,6 1.994,7 5.99,7 7.988,5 5.54,5 7.061,3.713C7.6,3.253 8.289,3 8.999,3 10.651,3 11.996,4.346 11.996,6 11.996,6.877 11.613,7.708 10.936,8.29L5.34,13.252 6.664,14.748 12.248,9.797C13.358,8.846 13.994,7.461 13.994,6 13.994,3.243 11.753,1 8.999,1" />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
I've also set the build action for Icon.xaml to Resource
instead of Page
.
I've created a helper class IconLocator
to load the icon for me:
public static DrawingBrush Icon => Load("Icon.xaml");
private static DrawingBrush Load(string fileName)
{
var uri = new Uri(Prefix + fileName);
var info = Application.GetResourceStream(uri);
var brush = XamlReader.Load(info.Stream) as DrawingBrush;
return brush;
}
I can now use this DrawingBrush everywhere:
<Border Background="{x:Static res:IconLocator.Icon}"/>
Upvotes: 2
Reputation: 1064
Have you tried to put it in app.xaml
as MergedDictionaries
<Application x:Class="WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="FolderWhereIsYourIconXaml/Icon.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
That way you can just call it like {StaticResource Icon}
But you will have to make it a ResouceDictionary.
Upvotes: 0