4

I have a XAML resource.

Screenshot of a Resources folder with a file titled "cancel_gray.xaml" inside of it

It looks like this.

Image of X button created out of a ViewBox that contains multiple DrawingBrushes and GeometryDrawings

How do I display this resource inside of a button?


Note:

  • I don't want to copy the code from the XAML image into App.xaml as a resource, I want to reference it properly from the Resources folder.
  • I would convert it to an SVG but I would rather use the image in its native format.
ASh
  • 34,632
  • 9
  • 60
  • 82
zkdr
  • 83
  • 11
  • Looks extraordinarily complicated for something that could be drawn by a Path with a simple geometry made of two lines. – Clemens Sep 20 '19 at 15:33
  • 1
    I didn't create the resource, this was from the Visual Studio 2017 images collection. – zkdr Sep 20 '19 at 15:35
  • Also, if someone want to give me advice on how I should present an issue like this next time on SO, let me know how I can improve. – zkdr Sep 20 '19 at 16:56

1 Answers1

2

This example will guide you to reference a xaml resource in a proper way.

step 1

ResourceXaml (Example.xaml in a resource folder)

<Viewbox x:Key="A_Name">
  // blahh...
</Viewbox>

app.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>    
            <!--Reference xaml file-->            
            <ResourceDictionary Source="/Resources/Example.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>        
</Application.Resources>

step 2

Button in YourPage.xaml

<Button Content="{StaticResource Image_KeyName_In_Referenced_XAML_File}" />
Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34
  • Thank you. I ended up just copy and pasting the ViewBox into App.xaml, giving it a key, and using that key as a reference. The thing that was tripping me up was that I couldn't use {StaticResource} in a tag inside of the – zkdr Sep 20 '19 at 15:56
  • 1
    @zdwyer just not to make app.xaml unmanaged, create serprate.xaml for each type of resources, so whenever you need you can easily edit it and copy it to use another project, so that you dont need copy paste same code again and again in each project just attach your xamls, and mainly helps in maintaining codes clean and managed – Shubham Sahu Sep 22 '19 at 13:38
  • 2
    You can't just add the plain ViewBox xaml to a ResourceDictionary by referencing it with the Source property. This property expects a ResourceDictionary root object in the referenced xaml file. But you can either add this ViewBox xaml directly to a ResourceDictionary xaml file and reference that in app.xaml or other ResourceDictionary. Or you need to add a wrapper object like to the ResourceDictionary, so you can reference any xaml file by the Source property of the wrapper object and then use its Content property to get the ViewBox object. – Anateus Jun 16 '20 at 15:49