Hi everyone, i tried to use the DataTemplateSelector. I follow this https://blog.xamarin.com/customizing-list-view-cells-xamarin-forms-datatemplateselector
but i dont know why it does't work.
Class MyDataTemplateSelector :
using Xamarin.Forms;
namespace SocialeFoot.model
{
class MyDataTemplateSelector : DataTemplateSelector
{
public MyDataTemplateSelector(DataTemplate inc,DataTemplate outc)
{
// Retain instances!
this.incomingDataTemplate = inc;
this.outgoingDataTemplate = outc;
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var messageVm = item as MessageViewModel;
if (messageVm == null)
return null;
return messageVm.isIncoming ? this.incomingDataTemplate : this.outgoingDataTemplate;
}
public DataTemplate incomingDataTemplate;
public DataTemplate outgoingDataTemplate;
}
Class Messagerie (where the list 'll be display)
public class MessageriePersonal : ContentPage
{
public MessageriePersonal(CellMessGlobalModel sender)
{
Title = sender.userName;
Image backgroundImg = new Image { Aspect = Aspect.Fill };
backgroundImg.Source = ImageSource.FromResource("SocialeFoot.resource.background.png");
//list view
List<MessageViewModel> cellList = new List<MessageViewModel>();
cellList.Add(new MessageViewModel(true, ImageSource.FromResource("SocialeFoot.resource.avatar.png"), "grgrg"));
cellList.Add(new MessageViewModel(false, ImageSource.FromResource("SocialeFoot.resource.avatar.png"), "grgrg"));
ListView list = new ListView();
//template
var incomingtemplate = new DataTemplate(typeof(IncomingViewCell));
var outcoming = new DataTemplate(typeof(OutgoingViewCell));
list.ItemsSource = cellList;
MyDataTemplateSelector tmp = new MyDataTemplateSelector( incomingtemplate, outcoming );
list.ItemTemplate = tmp;
list.RowHeight = 60;
RelativeLayout rl = new RelativeLayout();
rl.Children.Add(backgroundImg,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.RelativeToParent((parent) => { return parent.Width; }),
Constraint.RelativeToParent((parent) => { return parent.Height; })
);
rl.Children.Add(list,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.RelativeToParent((parent) => { return parent.Width; }),
Constraint.RelativeToParent((parent) => { return parent.Height; })
);
}
}
Class MessageViewModel
public class MessageViewModel
{
public bool isIncoming { get; set; }
public ImageSource image { get; set; }
public string text { get; set; }
public MessageViewModel(bool isIncoming,ImageSource image,string texte)
{
this.isIncoming = isIncoming;
this.image = image;
this.text = texte;
}
}
class Custom viewcell (OutcomingViewCell his nearly the smame, just text color change)
class IncomingViewCell : ViewCell
{
public IncomingViewCell()
{
//instantiate each of our views
var image = new CircleImage()
{
HeightRequest = 50,
WidthRequest = 50,
Aspect = Aspect.AspectFill,
BorderColor = Color.White,
BorderThickness = 1
};
StackLayout cellWrapper = new StackLayout();
StackLayout horizontalLayout = new StackLayout();
StackLayout VerticalLayout = new StackLayout();
Label message = new Label();
message.FontAttributes = FontAttributes.Bold;
//set bindings
message.SetBinding(Label.TextProperty, new Binding("text"));
image.SetBinding(Image.SourceProperty, "image");
//Set properties for desired design
cellWrapper.BackgroundColor = Color.FromHex("#eee");
horizontalLayout.Orientation = StackOrientation.Horizontal;
VerticalLayout.Orientation = StackOrientation.Vertical;
message.VerticalOptions = LayoutOptions.CenterAndExpand;
message.TextColor = Color.White;
message.BackgroundColor = Color.Blue;
//add views to the view hierarchy
VerticalLayout.Children.Add(message);
horizontalLayout.Padding = new Thickness(5, 5, 0, 5);
horizontalLayout.Children.Add(image);
horizontalLayout.Children.Add(VerticalLayout);
cellWrapper.Children.Add(horizontalLayout);
View = cellWrapper;
}
}
If someone can help me.