Привязки Xamarin.Forms - одно и то же значение свойства привязывается к каждому элементу в ListView. - PullRequest
0 голосов
/ 03 мая 2018

Когда я загружаю изображение из учетной записи хранения Azure в мобильном приложении Xamarin.Forms, а затем привязываю его к элементу в ListView. Одно и то же изображение привязано к каждому элементу. Я использую DataTemplateSelector, чтобы выбрать шаблон элемента и установить изображение.

Мой код 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); }
    }
}

Что я делаю не так?

Спасибо!

1 Ответ

0 голосов
/ 03 мая 2018

Вы не привязываете messageVm.Imagesource правильно, вы устанавливаете источник изображения на NewFinalImage, который не является частью объекта-объекта. Кажется, что NewFinalImage - это некоторая переменная вне объекта item. Чтобы заставить его работать, вам нужно сохранить URL-адрес изображения или имя файла в объекте item.

Это должно сработать, надеюсь, эта информация поможет вам!

...