Xamarin.Forms 绑定 - 相同的 属性 值绑定到 ListView 中的每个项目

Xamarin.Forms Bindings - Same property value is binded to every item in ListView

当我在 Xamarin.Forms 移动应用程序中从 Azure 存储帐户加载图像,然后将其绑定到 ListView 中的项目时。每个项目都绑定了相同的图像。我正在使用 DataTemplateSelector select 项目模板并设置图像。

我的代码 DataTemplateSelector:

protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
var newIsIncoming = false; 
        var m = item as MainChatPage;
        var messageVm = item as Message;
        if (messageVm == null)
            return null;

        var template = this.outgoingDataTemplate; ;

        if (messageVm.Autor == AppName.Helpers.Settings.Username && messageVm.ImageName == null && messageVm.Text != null)
        {
            template = this.outgoingDataTemplate;
        }

        if (messageVm.Autor != AppName.Helpers.Settings.Username && messageVm.ImageName == null && messageVm.Text != null)
        {
            template = this.incomingDataTemplate;
        }

        if (messageVm.Autor != AppName.Helpers.Settings.Username && messageVm.ImageName != null && messageVm.Text == null)
        {

            Load(messageVm.Autor, messageVm.ImageName);
            messageVm.Imagesource = NewFinalImage;
            template = this.incomingImageDataTepmlate;
        }

        if (messageVm.Autor == AppName.Helpers.Settings.Username && messageVm.ImageName != null && messageVm.Text == null)
        {
            Load(messageVm.Autor, messageVm.ImageName);
            messageVm.Imagesource = NewFinalImage;
            template = this.outgoingImageDataTemplate;

        }
}

async void Load(string containerName, string imageName)
    {          
        var imageNew = await GetImage(imageName, containerName);

        NewFinalImage = ImageSource.FromStream(() => new MemoryStream(imageNew));
    }

我的代码 MessageViewModel:

public class MessageViewModel : MvvmHelpers.ObservableObject
{
    string id;

    [JsonProperty(PropertyName = "id")]
    public string Id
    {
        get { return id; }
        set { SetProperty(ref id, value); }
    }

    string text;

    [JsonProperty(PropertyName = "text")]
    public string Text
    {
        get { return text; }
        set { SetProperty(ref text, value); }
    }

    string comesFrom;

    [JsonProperty(PropertyName = "comesfrom")]
    public string ComesFrom
    {
        get { return comesFrom; }
        set { SetProperty(ref comesFrom, value); }
    }

    string userImageName;

    [JsonProperty(PropertyName = "userimagename")]
    public string UserImageName
    {
        get { return userImageName; }
        set { SetProperty(ref userImageName, value); }
    }

    ImageSource userImageSource;

    [JsonIgnore]
    public ImageSource UserImageSource
    {
        get { return userImageSource; }
        set { SetProperty(ref userImageSource, value); }
    }

    string autor;

    [JsonProperty(PropertyName = "autor")]
    public string Autor
    {
        get { return autor; }
        set { SetProperty(ref autor, value); }
    }

    string os;

    [JsonProperty(PropertyName = "os")]
    public string OS
    {
        get { return os; }
        set { SetProperty(ref os, value); }
    }

    DateTime messageDateTime;

    [JsonProperty(PropertyName = "messagedatetime")]
    public DateTime MessageDateTime
    {
        get { return messageDateTime; }
        set { SetProperty(ref messageDateTime, value); }
    }

    public string MessageTimeDisplay => MessageDateTime.Humanize(culture: CultureInfo.CurrentCulture);

    bool isIncoming;

    [JsonProperty(PropertyName = "isincoming")]
    public bool IsIncoming
    {
        get { return isIncoming; }
        set { SetProperty(ref isIncoming, value); }
    }

    /*[JsonProperty(PropertyName = "image")]
    public string Image { get; set; }*/

    bool viewerIsIncoming;

    [JsonProperty(PropertyName = "viewerisincoming")]
    public bool ViewerIsIncoming
    {
        get { return viewerIsIncoming; }
        set { SetProperty(ref viewerIsIncoming, value); }
    }

    string type;

    [JsonProperty(PropertyName = "type")]
    public string Type
    {
        get { return type; }
        set { SetProperty(ref type, value); }
    }

    string imageName;

    [JsonProperty(PropertyName = "image")]
    public string ImageName
    {
        get { return imageName; }
        set { SetProperty(ref imageName, value); }
    }


    ImageSource image;

    [JsonIgnore]
    public ImageSource Imagesource
    {
        get { return image; }
        set { SetProperty(ref image, value); }
    }

    string video;

    [JsonProperty(PropertyName = "video")]
    public string Video
    {
        get { return video; }
        set { SetProperty(ref video, value); }
    }

    string sound;


    [JsonProperty(PropertyName = "sound")]
    public string Sound
    {
        get { return sound; }
        set { SetProperty(ref sound, value); }
    }
}

我做错了什么?

谢谢!

您没有正确绑定 messageVm.Imagesource,您将图像源设置为 NewFinalImage,这不是项目对象的一部分。 NewFinalImage 似乎是 item 对象之外的某个变量。为了让它工作,您必须将图像 url 或文件名存储在项目对象中。

应该可以,希望这些信息对您有所帮助!