I am creating a subclass of ContentView, UserPostView to use as a data template in a ListView. I dynamically add items to the ItemSource of ListView by creating new instances of UserPostView.
<Image x:Name="PictureView" Source="Logo" BackgroundColor="#ddd" />
<Label x:Name="NameView" Text="Name" TextColor="#444" FontAttributes="Bold" Grid.Row="0" Grid.Column="1" Margin="10" />
<Label x:Name="LocationNameView" Text="Location" TextColor="#666" Grid.Row="0" Grid.Column="2" YAlign="Center" />
<Label x:Name="DeptNameView" Text="Dept" TextColor="#666" Grid.Row="0" Grid.Column="3" YAlign="Center" />
and in code:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace CMCityClient.Views
{
public partial class UserPostView : ContentView
{
public ImageSource Picture
{
get { return PictureView.Source; }
set { PictureView.Source = value; }
}
public string PostOwner {
get { return NameView.Text; }
set { NameView.Text = value; }
}
public string PostOwnerLocation {
get { return LocationNameView.Text; }
set { LocationNameView.Text = value; }
}
// ...
// ...
public UserPostView(ImageSource picture, string owner, string loc, string dept, string text) {
InitializeComponent();
Picture = picture;
PostOwner = owner;
PostOwnerLocation = loc;
PostOwnerDept = dept;
PostText = text;
}
public UserPostView(ImageSource picture, string owner, string loc, string dept, string text, ImageSource image) {
InitializeComponent();
Picture = picture;
PostOwner = owner;
PostOwnerLocation = loc;
PostOwnerDept = dept;
PostText = text;
PostImage = image;
}
}
}
but when I use it in my ListView, it will not show the passed data, it shows the original data instead. (Text="Name"...)
public TimelinePage()
{
InitializeComponent();
ImageSource pic = ImageSource.FromUri(new Uri("https://tse1.mm.bing.net/th?id=OIP.acchQ02P8HmrmwbjCTwG5AHaHa&pid=Api"));
timeline.ItemsSource = new UserPostView[]{
new UserPostView(ImageSource.FromResource("Logo"), "Kevin Wang", "SHDR", "AOP", "hey"),
new UserPostView(pic, "Lily", "SHDR", "ENT", "good"),
new UserPostView(pic, "Zhang Shuo", "FRDL", "ENT", "Salut! "),
};
}
help! How can I let them show the data that passed through the constructor?
NameViewshowsNameinstead ofLilyorKevin Wang. - Kevin Wang