简体   繁体   English

Xamarin Xam.Plugins.Forms.Svg更改SvgPath以重新呈现图像

[英]Xamarin Xam.Plugins.Forms.Svg change SvgPath to rerender the image

I use Xam.Plugins.Forms.Svg to support svg images. 我使用Xam.Plugins.Forms.Svg支持svg图像。 I need to change the image, so I tried: 我需要更改图像,所以我尝试:

binding in xaml : 在xaml中绑定

<abstractions:SvgImage
    SvgPath="{Binding CurrentWeather.IconSource, StringFormat='Umbrella.Images.{0:F0}.svg'}"
    ....
    />

to change it directly from C#: 直接从C#更改它:

icon.SvgPath = "Umbrella.Images." + currentWeather.IconSource + ".svg";

The SvgPath attribute does change, but the image doesn't rerender. SvgPath属性确实会更改,但是不会重新渲染图像。

Is there any solution to this? 有什么解决办法吗?

It is an issue in the way the renderers are written, you would need to create a new SvgImage object and replace the current on in your page the way the renderers work in order for the static Image to be updated from a new SVG file. 这是渲染器编写方式的一个问题 ,您需要创建一个新的 SvgImage对象,并以渲染器的工作方式替换页面上的当前对象,以便从新的SVG文件更新静态Image

You can patch the renderers by overriding the OnElementPropertyChanged event and respond to SvgPath PropertyName changes: 您可以通过覆盖OnElementPropertyChanged事件来修补渲染器,并响应SvgPath PropertyName的更改:

iOS Example: iOS示例:

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);
    if (e.PropertyName == "SvgPath")
    {
        var svgStream = _formsControl.SvgAssembly.GetManifestResourceStream(_formsControl.SvgPath);
        var reader = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser()), new ValuesParser());
        var graphics = reader.Graphic;
        var canvas = new ApplePlatform().CreateImageCanvas(graphics.Size);
        graphics.Draw(canvas);
        var image = canvas.GetImage();
        var uiImage = image.GetUIImage();
        Control.Image = uiImage;
    }
}

Ref: https://github.com/paulpatarinski/Xamarin.Forms.Plugins/blob/eab230fcf4158a288387727c15903a954e01cdaa/SVG/SVG/SVG.Forms.Plugin.Android/SvgImageRenderer.cs 参考: https : //github.com/paulpatarinski/Xamarin.Forms.Plugins/blob/eab230fcf4158a288387727c15903a954e01cdaa/SVG/SVG/SVG.Forms.Plugin.Android/SvgImageRenderer.cs

Ref: https://github.com/paulpatarinski/Xamarin.Forms.Plugins/blob/eab230fcf4158a288387727c15903a954e01cdaa/SVG/SVG/SVG.Forms.Plugin.iOS/SvgImageRenderer.cs 参考: https : //github.com/paulpatarinski/Xamarin.Forms.Plugins/blob/eab230fcf4158a288387727c15903a954e01cdaa/SVG/SVG/SVG.Forms.Plugin.iOS/SvgImageRenderer.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM