简体   繁体   English

Xamarin.Forms图像无法渲染

[英]Xamarin.Forms images not rendering

I am coding a Xamarin.Forms app that displays messages a user receives, which is displayed in a StackLayout with its body and an image for a like button. 我正在编写一个Xamarin.Forms应用程序,该应用程序显示用户收到的消息,该消息显示在StackLayout中,并带有其主体和一个喜欢按钮的图像。

My problem is when I have like 10 messages sometimes the image doesn't show for some of them. 我的问题是,当我喜欢10条消息时,其中的某些图像有时无法显示。 For example, it might render the image on each message except the fourth one. 例如,它可能会在除第四条消息之外的每条消息上渲染图像。

This only seems to happen on Android, iOS renders the icon for each layout and they are using the same code. 这似乎发生在Android上,iOS为每个布局渲染图标,并且它们使用相同的代码。

My Code: 我的代码:

foreach (var message in messages)
{
    StackLayout childLayout = new StackLayout
    {
        BackgroundColor = Color.White,
        Margin = new Thickness(10, 10, 10, 0),
        Padding = new Thickness(10, 10, 15, 10),
        Spacing = 0,

    };
    Label body = new Label() { Text = message.Body };
    Image image = new Image() { Source = "like_icon.png" };

    //There are other children but not related to question

    childLayout.Children.Add(image);
    mainStackLayout.Children.Add(childLayout);
}

As this is Android specific, it could be a memory issue as Android can quickly run out of memory when rendering images. 由于这是特定于Android的,因此可能是内存问题,因为Android在渲染图像时会很快耗尽内存。

One workaround is to use the FFImageLoading plugin. 一种解决方法是使用FFImageLoading插件。 This allows multiple image references using the same image source to use only one bitmap which greatly reduces the memory usage. 这允许使用同一图像源的多个图像引用仅使用一个位图,从而大大减少了内存使用。

https://github.com/luberda-molinet/FFImageLoading https://github.com/luberda-molinet/FFImageLoading

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

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