2

In my Windows Phone 8 app I use set of icons. These icons are used multiple times with different size and colour. Right now I just have multiple png files from same icon to cover all these variants.

How this can he handled with vector images? My ultimate goal is to store vector path to e.g. resource file and use that as "image source", set image size and colour in my view, not in resource file.

Lets say I have Canvar like this:

<Canvas Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="23.75" Height="49.4791" Canvas.Left="26.9167" Canvas.Top="13.8542" Stretch="Fill" Fill="#FF000000" Data="F1 M 26.9167,13.8542L 50.6666,13.8542L 50.6667,39.5833L 26.9167,63.3333L 26.9167,13.8542 Z "/>
</Canvas>

How I can store this to resource and use it as image source?

All suggestions and ideas are welcome how to handle this.

UPDATE

Based on Chris W answer, I came to this solution:

<Style x:Key="Marker" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Canvas Width="76" Height="76">
                    <Path Fill="{TemplateBinding Foreground}" Data="m 26.9167 13.8542 23.7499 0 10e-5 25.7291 -23.75 23.75 0 -49.4791 z"></Path>
                </Canvas>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<ContentControl Style="{StaticResource Marker}" Foreground="Red" />

This works ok, I can reuse the Mrker style and set it fill color. The missing piece in this is, how should I resize the canvas/contetnControl?

Now the canvas size is 76*76, even if I change that the output image still the same size. How should I resize the canvas?

devha
  • 3,307
  • 4
  • 28
  • 52
  • I don't think WPF/Silverlight supports SVG (See http://stackoverflow.com/questions/15191536/using-svg-files-like-png-files-in-windows-phone and http://stackoverflow.com/questions/12092522/handling-svg-in-windows-phone-app). SVG is a common vector file format. But, you could convert and SVG to XAML – Peter Ritchie Feb 15 '14 at 23:21
  • Thats exactly what im trying to do, convert vector image to xaml (canvas) and use it. But what are best the practises for that. How can I assign canvas defined in resource to actual ui element (image?). – devha Feb 15 '14 at 23:29

1 Answers1

2

The first part of your question would be a duplicate and I think Illustrator or other suites can do the same, but to answer your second question you can just make it into a ContentControl template like;

<Style x:Key="ReUsableResource" TargetType="ContentControl">
        <Setter Property="Height" Value="49.4791"/>
        <Setter Property="Width" Value="23.75"/>
        <Setter Property="Template">    
          <Setter.Value>
             <ControlTemplate TargetType="ContentControl">
                 <Canvas>
                       <Path Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stretch="Fill" Fill="#FF000000" Data="F1 M 26.9167,13.8542L 50.6666,13.8542L 50.6667,39.5833L 26.9167,63.3333L 26.9167,13.8542 Z "/>
                 </Canvas>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
    </Style>

Then invoke it like;

<ContentControl Width="100" Height="50" Style="{StaticResource ReUsableResource}"/>

Oh and just read the comments, to get it from Vector to XAML another really handy resource is Mike Swanson's XAML exporter plugin for Illustrator.

Hope this helps.

Community
  • 1
  • 1
Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • Thanks, that works ok but how to resize canvas? Update question too. – devha Feb 16 '14 at 12:04
  • @devha see edited answer, just strip the Height/Width from the canvas and apply it to where you invoke the contentcontrol – Chris W. Feb 16 '14 at 16:48
  • Changing ContentControl size does not change the canvas size (even if I remove height/widht from canvas) :( – devha Feb 16 '14 at 18:02
  • @devha dude you're going to have to play with things sometimes to learn, I didn't even bother loading your Path stuff until now, but I did and here's some things. Your Clip doesn't appear to do anything, the canvas.top/canvas.left declarations.....can't quite figure out their purpose. As for the graphic, try my updated answer and notice the bound Width/Height values which will change it now that I stripped the other stuff off. Hope this helps. – Chris W. Feb 17 '14 at 03:37