.net 将base64绑定到ffimagloading时出现问题:CachedImage

uinbv5nw  于 2023-01-22  发布在  .NET
关注(0)|答案(1)|浏览(123)

我有一个创建stacklayout的问题,其中包含从base64string创建的图像,从数据库下载。
在视图模型中:public string ImageString {get; set;} = "[base64string - 95503 chars]";
xaml中的片段代码:<ffimageloading:CachedImage Aspect="AspectFill" Source="{Binding ImageString}"/>
当我直接尝试base64(而不是绑定)时,它起作用了,我认为问题是绑定字符串太大。
有解决这个问题的办法吗?

v09wglhw

v09wglhw1#

如果您想使用绑定,可以尝试将ImageBase64String转换为Xamarin.Forms.ImageSource
我在我这边做了一个测试,它在我这边工作。请参考下面的代码:

public class MyViewModle 
    {
        public string ImageBase64 { get; set; }

        public Xamarin.Forms.ImageSource Image { get; set; }

        public MyViewModle() {
            
          ImageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAHwAAAB8CAMAAACcwCSMAAAAZlBMVEUAAAD///+cnJz7+/vz8/MdHR329vbw8PBRUVHh4eEaGhqsrKzn5+dZWVnk5OQyMjLZ2dmOjo6ysrIoKCjR0dFiYmKjo6NKSkoNDQ3ExMQ4ODi5ubmHh4cWFhbLy8tBQUF6enpvb2/Qp8GOAAAFzUlEQVRogcWb56KyOhBFIyBFQUGkiQXe/yVvQPFQUvYEvN/+q7CYlMnMJGG7fyhm8pDrnZ71Mbd65cf6efLc/wPuhlXeRLf0wUZ6pLeoyauQ+gUkuFPd/ZRJlfr3yvkN3L20pRw8qGwvuP0g3K0aPXhQU4F8CB5aLxzd6WWFG8HDRNHPMqUJgNfCPRP0G++thNtHQ3SPP9pr4NfCHN2puBrDvWYdulOjansFvLqtZzN2qwzgtrUFupMl7XkZPGy3YjPWymadBB4TvYpar5gCrwEvTlFZ4/DjtuhORxT+A7aYLoD/hC2kL+E/YovoC/jlV2zGLjr483dsxp5qeLBiEdMrDVRwz/8lmzHfU8DvlDelLz/yX7SmusvhhIEe5XHYLRh2GOcRgX6UwWPUqT6s08SCUwK74zKWwFELmuUqFTYoPRL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PvYBBKoENeN3n787ZH+7yJMxuVeXnaiajKi/OTzavB8vf4dNLZ9svIyODozg8XjMyY/vGSgYuZlxKW98ZmJcPyxbazMlY+8+Ll1PjqrQckYjZTsZPDTN1WGdQymcWDyla+a0Zwez4JTVTLOYcH4krfkle745tjiM98NzYf6ctYA7P6MvDx4sz0CiZ23I7GWxXHD68/QTui/IdUXnXsOVp9pFEm5RCE/8KlNHI/nC/ENy0Jp0HlEvSVwkgdv5hr6ulKUf0sP1z838/Fm6ASy/VqDK7CmK5HGw6kJFTq7lLvVQpfjKqySn1Qu8upKluURTr7rRcdPke7rrQ45lvImSWrpTZPqLU0b3prCbU8iVsZBufQrdGcMuy7kX0ryLwNt68DXB0ALH3g27KEeCc5cbW5FmJ2kfWTEh26BdDbXDOsluwlrt4ZYldUjLc+iXYm0vrvOk9c/7Q6/92W+TvI49eoJldCP38xUfmb/hPxbhSmN+ggQuAAAAAElFTkSuQmCC";

            if (ImageBase64 != null)
            {
                if (ImageBase64.Trim() != "")
                {
                    var byteArray = Convert.FromBase64String(ImageBase64);

                    if (byteArray != null)
                    {
                        Image = Xamarin.Forms.ImageSource.FromStream(
                            () => new MemoryStream(Convert.FromBase64String(ImageBase64)));
                    }
                }
            }
        }

    }

用法示例:

<ffimageloading:CachedImage HorizontalOptions="Center" Source="{Binding Image}" DownsampleToViewSize="true" WidthRequest="40"/>

注:

不要忘记为页面设置BindingContext

public partial class MainPage : ContentPage 
    {
        public MainPage()
        {
            InitializeComponent();

            this.BindingContext = new MyViewModle();
        }
    }

相关问题