0

How to: XAML -> System.Windows.Media.Geometry ?

I want to create vector image (for example, in Inkscape), save it to XAML, than take whole file (from the very first "<" symbol to the very last symbol ">"), put in string, and then to make System.Windows.Media.Geometry object.

This way is very good, it works perfect:

 System.Windows.Media.Geometry g = Geometry.Parse("M 263,99 ... Z");

But, it fails (in my hands) with multi-line paths, fills, and other symbols.

UPD:

string sourse_xaml_str = 

@"

<?xml version="1.0" encoding="UTF-8"?>
<!--This file is compatible with Silverlight-->
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Name="svg8" Width="210" Height="297">
  <Canvas.RenderTransform>
    <TranslateTransform X="0" Y="0"/>
  </Canvas.RenderTransform>
  <Canvas.Resources/>
  <!--Unknown tag: sodipodi:namedview-->
  <!--Unknown tag: metadata-->
  <Canvas Name="layer1">
    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path10" Fill="#FFFF5555" StrokeThickness="0.26458332" Data="m 74.083331 126.9107 -10.873824 18.802 9.324097 19.61673 -21.241956 -4.53148 -15.775318 14.92964 -2.254427 -21.6026 -19.073781 -10.38971 19.848644 -8.81966 3.987074 -21.35084 14.521563 16.15176 z"/>
  </Canvas>
</Canvas>

";

System.Windows.Media.Geometry top_menu_item_icon = XamlReader.Parse( sourse_xaml_str ) as System.Windows.Media.Geometry;
Console.WriteLn( XamlReader.Parse( sourse_xaml_str ).ToString() );

Results:

  1. top_menu_item_icon == null
  2. WriteLn() show this: System.Windows.Controls.Canvas

So, it still not works for me.

  • I'm not familiar with how Inkscape outputs XAML, but have you tried [XamlReader](https://stackoverflow.com/questions/910814/loading-xaml-at-runtime) to load a XAML file at runtime? Could you post the contents of a small example XAML file from Inkscape? – Ron Beyer Aug 22 '18 at 20:53
  • Thank You for idea. Need to pack everything inside one only `C#` file. That's why I am going to put whole file in string manually. I played with `XamlReader`, but can't let it to read from string. – Andrii Vdovenko Aug 22 '18 at 20:58
  • Use [XamlReader.Parse](https://learn.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlreader.parse?view=netframework-4.7.2#System_Windows_Markup_XamlReader_Parse_System_String_) – Clemens Aug 22 '18 at 21:07
  • Sorry, still not works for me. Tried it before, trying now, but no result. Can't find any info, also, about how to convert `Canvas` to `Geometry` – Andrii Vdovenko Aug 23 '18 at 10:04

0 Answers0