【问题标题】:Xamarin.Forms ImageCell not displaying image version 4.7.0.xxXamarin.Forms ImageCell 不显示图像版本 4.7.0.xx
【发布时间】:2020-07-29 03:40:21
【问题描述】:

我已经尝试在我的模型中使用 stringImageSource,但我仍然没有在 ImageCell 中显示图像。

如果我尝试这行代码<Image Source="https://img.icons8.com/carbon-copy/2x/user.png"></Image>,图像显示正常。

型号

public class Contact
{
    public string Name { get; set; }
    public string Status { get; set; }
    public ImageSource ImageUri { get; set; }
}

XAML:

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App1.ListPage">
    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="listView" SeparatorVisibility="Default" SeparatorColor="Blue">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <!--<TextCell Text="{Binding Name}" Detail="{Binding Status}" TextColor="Accent" DetailColor="Beige"></TextCell>-->
                        <!--<TextCell Text="{Binding Name}" Detail="{Binding Status}"></TextCell>-->
                        `<ImageCell Text="{Binding Name}" Detail="{Binding Status}" ImageSource="{Binding ImageUri}`"></ImageCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <!--<Image Source="https://img.icons8.com/carbon-copy/2x/user.png"></Image>-->
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

XAML.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListPage : ContentPage
{
    public ListPage()
    {
        InitializeComponent();

        listView.ItemsSource = new List<Contact>
        {
            new Contact{ Name="Monkey D. Luffy", ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png"))},
            new Contact{ Name="Monkey D. Dragon",
                ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png"))},
            new Contact{ Name="Portgas D. Ace",
                ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png")), Status="Hello World!"}
        };
    }
}

【问题讨论】:

  • 您可以在数据模板ViewCell 中直接使用Image 而不是ImageCell
  • 你的图片尺寸是多少?我认为您需要在您的ListView 中添加HasUnevenRows="true",您的图像看起来就像被行高隐藏了
  • @cahyo ViewCell 有效,我不知道 ImageCell 有什么问题
  • 我将我的 xamarin 表单 nuget 包更新为 4.7.0.1179,这是我的测试图像:aka.ms/campus.jpg,这个图像很大,但正在等待几秒钟。它可以正常工作。imgur.com/a/UovlCAs如果可以,您可以使用我的图片网址进行测试。

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

刚刚在我的操场上做了两个测试。

我在 VS2019 中以一个空白的 Xamarin.Forms 项目(那里只有一个 MainPage)开始,然后

  1. 将 XF nuget 包更新到 4.7.0.1142。
  2. 粘贴了您的代码。
  3. 在您的 xaml 代码 sn-p 中删除两个额外的 ` 字符。

它正在工作。

然后,我在模型类中尝试了这样的字符串属性:
public string ImageUri { get; set; }
并且还像这样在 xaml.cs 类中设置字符串值:
new Contact{ Name="", ImageUri="https://xxx.png"}

它也有效。

不确定这个问题,但你的代码还是可以的。

【讨论】:

  • 这个奇怪的,已经创建新项目尝试,仍然没有显示。已经清除 bin,obj 文件。但是我使用 ViewCell 图像可以,我正在使用我的手机进行调试。
  • @CoffeeCode,我也在用我的安卓手机,安卓操作系统版本怎么样。当你提到 4.7.x 时,这是否意味着你可以看到 4.6.x 上的图片? (为了测试,您始终可以不注释列表视图之外的图像,以排除其他可能性,例如网络错误)。我在win10上使用VS2019,顺便说一句。
【解决方案2】:

尝试将此 nuget https://github.com/luberda-molinet/FFImageLoading 用于图像。是最好的!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多