I am new in UWP app development and looking for a way to bind string based SVG to a UWP (Universal Windows Platform) page. And I wanted to show that string based SVG by using Image tag of UWP page (XAML layout). The SVG will be represented in SVG markup language like:
dataBindedModel.svg = “<svg xmlns="http://www.w3.org/2000/svg"; width="500" height="500">
<circle cx="250" cy="250" r="210" fill="#fff" stroke="#000" stroke-width="8"/>
</svg>”;
For that I need to use {x:Bind} in the XAML, but not sure how to bind this. Potential XAML Code will be like:
<Image x:Name="MyImageView" Source="{x:Bind ...required area...”}>
Update:
My MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
SVGModel dataBindedModel = new SVGModel();
public MainPage()
{
this.InitializeComponent();
dataBindedModel.SVGString = File.ReadAllText("test.xml");
this.DataContext = dataBindedModel;
}
}
public class SVGModel
{
public SVGModel() { }
public SVGModel(string svg)
{
SVGString = svg;
}
public string SVGString { get; set; }
}
My MainPage.xaml:
<Grid>
<Grid.Resources>
<local:SVGImageConverter x:Key="MyConvert" />
</Grid.Resources>
<Image x:Name="MyImageView" Source="{x:Bind dataBindedModel.SVGString,Mode=OneWay,Converter={StaticResource MyConvert }}" />
</Grid>
My test.xml:
<?xml version="1.0" encoding="utf-8" ?>
<svg width="500" height="500">
<circle cx="250" cy="250" r="210" fill="#fff" stroke="#000" stroke-width="8"/>
</svg>
Imageproperty won't render it.<Image Source="Assets/mysvg.svg"/>works because the runtime will use the SvgImageSource to convert between what's recognized as a path to ansvgfile and theImageproperty - Panagiotis KanavosSource. If it's a string generated at runtime, you can bind to an SvgImageSource and load the data with SetSourceAsync - Panagiotis KanavosMy SVG is in the XML formatgiven that SVG is XML that doesn't say anything. Have you tried saving the file with ansvgextension and using it the way the linked docs show? Besides, where is thattest.xmlfile? The path is relative which means the application will try to find it in tis executable folder, wherever that is. Windows Apps are sandboxed which means the current directory isn't where you think it is, and the app can't read anything it wants either - Panagiotis Kanavos