【问题标题】:Xamarin Forms using an anonymous method within a For loopXamarin Forms 在 For 循环中使用匿名方法
【发布时间】:2020-08-13 01:49:30
【问题描述】:

在下面的 For 循环中,循环完成后,它最后一次将计数器加一,例如如果条件是 i

我知道在循环中使用匿名方法还有其他问题,我已经应用了推荐的解决方案,但没有这样的运气。

这是我的代码

FileModel fileModel = DependencyService.Get<IImageFetcher>().ReadImagesAsync();

    var items = new List<FileImageInfo>();
    for (int i = 0; i < fileModel.Images.Count; i++)
    {
        var imageStream = ImageSource.FromStream(() => new MemoryStream(fileModel.Images[i]));
        items.Add(new FileImageInfo
        {
            FileType = "Jpeg",
            FileSize = fileModel.Sizes[i],
            DateCreated = fileModel.Dates[i],
            imageSource = imageStream
        });
    }
    Items.ItemsSource = items;

上面的代码工作表明问题肯定出在循环而不是图像或其他任何东西

【问题讨论】:

  • 具体是哪一行抛出异常,具体是什么异常? for 循环本身不太可能是问题 - 您可以将其更改为 foreach 进行检查。您确定图像、尺寸和日期的长度始终相同吗?最后,这是基本的 C#,它与 Xamarin 无关
  • 你说的这个匿名方法在哪里?

标签: c# for-loop xamarin.forms anonymous-function


【解决方案1】:

使用 foreach 循环就可以了。在 Xamarin 论坛上被告知必须使用 foreach 而不是 For 与 listview 图像

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2013-05-23
    • 2020-06-26
    相关资源
    最近更新 更多