当我从Xamarin.Forms移动应用程序中的Azure存储帐户加载图像,然后将其绑定到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); }
}
}
我做错了什么?
谢谢!
答案 0 :(得分:1)
您没有绑定messageVm.Imagesource
正确,您将imagesource设置为NewFinalImage
,而NewFinalImage
不是item-object的一部分。似乎index.js
是项目对象之外的一些变量。为了使其工作,您必须将图像URL或文件名存储在项目对象中。
这应该有用,我希望这些信息能帮到你!