【问题标题】:Bind string based SVGs to a UWP application将基于字符串的 SVG 绑定到 UWP 应用程序
【发布时间】:2021-05-27 20:05:29
【问题描述】:

我是 UWP 应用程序开发的新手,正在寻找一种将基于字符串的 SVG 绑定到 UWP(通用 Windows 平台)页面的方法。我想通过使用 UWP 页面的图像标签(XAML 布局)来展示基于字符串的 SVG。 SVG 将以 SVG 标记语言表示,例如:

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>”;

为此,我需要在 XAML 中使用 {x:Bind},但不确定如何绑定它。 潜在的 XAML 代码如下:

<Image x:Name="MyImageView" Source="{x:Bind ...required area...”}>

更新

我的 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; }

    }

我的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>

我的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>

【问题讨论】:

  • 您所拥有的只是文本,而不是图像。将一些文本分配给 Image 属性不会呈现它。 &lt;Image Source="Assets/mysvg.svg"/&gt; 有效,因为运行时将使用 SvgImageSource 在被识别为 svg 文件的路径和 Image 属性之间进行转换
  • SVG 是从哪里来的?一个文本文件?资源?在这种情况下,您只需将正确的路径传递给Source。如果是运行时生成的字符串,可以绑定到 SvgImageSource 并使用SetSourceAsync 加载数据
  • @PanagiotisKanavos - 我的 SVG 是 XML 格式(test.xml 见上文)。我创建了一个 xml 文件,将我的 SVG xml 粘贴到其中。我正在 MainPage.xaml.cs 的构造函数中的 SVGModel 类的 SVGString 属性中读取这个 xml。 (见上文)但是为了运行代码而遇到了一些异常。
  • My SVG is in the XML format 鉴于 SVG XML,它什么也没说。您是否尝试过使用 svg 扩展名保存文件并按照链接文档显示的方式使用它?此外,那个test.xml 文件在哪里?路径是相对的,这意味着应用程序将尝试在可执行文件夹中找到它,无论它在哪里。 Windows 应用程序是沙盒化的,这意味着当前目录不在您认为的位置,并且该应用程序也无法读取它想要的任何内容
  • @PanagiotisKanavos - 如何在 MainPage.xaml.cs 的 ctor 中直接使用 XML 设置 SVGString 属性。如果我尝试: dataBindedModel.SVGString = "";有什么想法吗?

标签: c# .net image uwp desktop


【解决方案1】:

UWP 不允许直接绑定 SVG 字符串,它只包含 SvgImageSource 类,请保存为 svg 文件并设置为图像控件,如下所示。

<Image Source="Assets/mysvg.svg"/>

另一种方法是使用值转换器将字符串转换为SvgImageSource,例如

public class SVGImageConverter : IValueConverter
{
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }

    public  object Convert(object value, Type targetType, object parameter, string language)
    {
        var svg = new SvgImageSource();
        try
        {
            var svgBuffer = CryptographicBuffer.ConvertStringToBinary(value.ToString(), BinaryStringEncoding.Utf8);
       
            using (var stream = svgBuffer.AsStream())
            {
                 svg.SetSourceAsync(stream.AsRandomAccessStream()).AsTask().ConfigureAwait(false);                    
            }
        }
        catch (Exception ex)
        {

        }

        return svg;
    }
}

用法

<Image x:Name="MyImageView" Source="{x:Bind dataBindedModel.svg,Mode=OneWay,Converter={StaticResource MyConvert }}" />

请注意:请确保您的 svg 字符串是正确的 svg 类型。

【讨论】:

  • Nico Zhu - MSFT - 感谢您的反馈。我已经尝试了您的解决方案,但由于某种原因它无法正常工作。我已经更新了我的问题。请告诉我的代码中缺少什么。
  • Nico Zhu - MSFT - 请尝试使用您选择的任何其他 xml 字符串。我想通过这个绑定。谢谢!
  • 好的,我给你打样,请稍等。
  • 让我检查一下。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2013-02-18
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 2012-08-08
相关资源
最近更新 更多