【问题标题】:Xamarin.Forms ImageCell not displaying the image?Xamarin.Forms ImageCell 不显示图像?
【发布时间】:2018-11-16 11:46:37
【问题描述】:

我有一个 ImageCell 作为 ListView 的一部分,它应该显示通过数据绑定从页面的 ViewModel 获得的三条信息。

XAML:

<ListView x:Name="ScoreListView" ItemsSource="{Binding DBScores}"
    IsPullToRefreshEnabled="True" RefreshCommand="{Binding RefreshScoresAsyncCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <ImageCell 
                Text="{Binding DisplayName}"
                Detail="{Binding Points}"
                ImageSource="{Binding SyncImagePath}"/>
        </DataTemplate>
    </ListView.ItemTemplate>

C#

DBScores = new ObservableCollection<ScoresTable>();

...

var downloadedList = await App.AzureService.GetScores();                                                        
downloadedList = downloadedList.OrderByDescending(x => x.Points).ThenBy(x => x.DisplayName).ToList();

DBScores = new ObservableCollection<ScoresTable>();                                                         

foreach (ScoresTable element in downloadedList)                                                         
{
    if(element.Synced == 0)
    {
        element.SyncImagePath = "notsynced.png";
    }
    else
    {
        element.SyncImagePath = "synced.png";
    }
    DBScores.Add(element);                                                                              
}

...

public class ScoresTable
{
    [JsonProperty("Id")]
    public string Id { get; set; }
    public string DisplayName { get; set;}   
    public int Points { get; set; }
    public DateTime AchievedOn { get; set; }
    public int Synced { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    [JsonIgnore]
    public string SyncImagePath { get; set; }
}

DisplayNamePoints 的绑定工作得很好。我将相关图像放置在我的 Android 项目的 Resources/drawable 文件夹中,并将它们添加到 Visual Studio 项目中。他们的名字拼写正确并且大小写正确。当我从数据库加载图像时,我检查它们的Synced 属性并将图像路径设置为两个图像之一; synced.pngnotsynced.png

我很茫然,因为这在我昨天回家时有效,但现在无效。我尝试在 VS 中清理并手动删除 bin 和 obj 文件夹并重建项目。

编辑 1; Build Actions 设置为 AndroidResource

编辑 2; This 是我看到的

【问题讨论】:

  • 你的android项目中图像的构建动作是什么?它应该设置为“AndroidResource”。
  • 就是,两张图片都设置成这个i.imgur.com/2ZGPvYO.png
  • 您是否尝试过清理解决方案?并确保手动删除“bin”和“obj”文件夹...有时资源不会再次打包。
  • 我有,不过我会再试一次。更新;没有运气。
  • 我已将版本降级回 3.3.0.967583,它突然又可以工作了。发行说明中没有提到对这些内容的任何更改。我应该早点想到这一点,缓慢的一天......

标签: xamarin.forms


【解决方案1】:

从 3.4.0.1008975 降级到 3.3.0.967583 修复了它。必须是新更新的一些问题或我没有看到的一些变化。发行说明中没有提到对 ImageCells 的任何更改。

【讨论】:

  • 同上。 Xamarin 框架中层出不穷的错误确实让我重新考虑我的决定。我希望 Xamarin 团队中的某个人能看到这一点。
  • 这里也一样,那是什么鬼。不敢相信它发布了
【解决方案2】:

我遇到了与您完全相同的问题,我还通过从 Xamarin.Forms 3.4.0.1008975 恢复到 3.3.0.967583 来修复它。正如您所说,发行说明中没有提到对 ImageCells 的任何更改。我向 Xamarin.Forms GitHub 页面提交了一个问题(并使用了这个 StackOverflow 帖子作为参考,我希望这没问题。我没有把它归功于它)。

https://github.com/xamarin/Xamarin.Forms/issues/4584

编辑:他们确实承认这是一个错误并已修复它,我发现的最新稳定版本(在撰写本文时)是 v3.4.0.1009999。

【讨论】:

  • 仍然无法使用,我唯一可以使用的图像源是我手机上没有的 http 图像
  • @Jonas 您升级到哪个版本的 Xamarin.Forms?最近有报道称 v3.5.0.1006868 也被破坏了。使用 v3.4.0.1009999,这对我有用。此外,请仔细检查您的绑定(如果您正在使用绑定)。如果您尝试使用文件名,请确保它们包含在操作系统特定项目的 Assets/Resources 文件夹中(取决于您的项目设置方式、您尝试使用的操作系统等。 )
【解决方案3】:

使用 Xamarin.Forms v4.4.0.x 将 ImageUrl 与“https://...”相同的问题。更新到最新的 4.6.0.x 并且问题消失了,图像显示正确。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多