Hi There,
I am trying to create a ListView with some specific template per cell, so I am using a class that inherits from the ViewCell class.
Here are the code
public class DefaultPageListViewTemplate : ViewCell
{
public DefaultPageListViewTemplate()
{
Grid gridDefaultPage = new Grid
{
Padding = new Thickness(5, 10, 0, 0),
ColumnDefinitions =
{
new ColumnDefinition {Width = new GridLength(2, GridUnitType.Auto)},
new ColumnDefinition {Width = new GridLength(10, GridUnitType.Star)}
},
RowDefinitions =
{
new RowDefinition {Height = new GridLength(1, GridUnitType.Star)},
new RowDefinition {Height = new GridLength(1, GridUnitType.Star)}
}
};
CircleImage favoriteImage = new CircleImage
{
//HeightRequest = 42,
WidthRequest = Device.OnPlatform(55, 55, 75),
HeightRequest = Device.OnPlatform(55, 55, 75),
VerticalOptions = LayoutOptions.Center
};
favoriteImage.SetBinding(Image.SourceProperty, "Image");
gridDefaultPage.SetGridLocation(favoriteImage, 0, 0, 0, 2);
Label siteNameLabel = new Label
{
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.StartAndExpand,
FontSize = 13
};
siteNameLabel.SetBinding(Label.TextProperty, "Title");
gridDefaultPage.SetGridLocation(siteNameLabel, 0, 1, 0, 0);
Label siteDescriptionLabel = new Label
{
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.StartAndExpand,
FontSize = 11
};
siteDescriptionLabel.SetBinding(Label.TextProperty, "Description");
gridDefaultPage.SetGridLocation(siteDescriptionLabel, 1, 1, 0, 0);
MenuItem moreAction = new MenuItem {Text = "Update"};
moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
moreAction.Clicked += async (sender, e) =>
{
};
MenuItem deleteAction = new MenuItem {Text = "Delete", IsDestructive = true}; // red background
deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
deleteAction.Clicked += async (sender, e) =>
{
MenuItem mi = ((MenuItem) sender);
SpUrlsPerUser spUrlsPerUser = mi.CommandParameter as SpUrlsPerUser;
if (spUrlsPerUser == null || String.IsNullOrEmpty(spUrlsPerUser.ObjectId))
return;
RaiseDeleteSpUrlEvent(spUrlsPerUser.ObjectId);
};
ContextActions.Add(moreAction);
ContextActions.Add(deleteAction);
View = gridDefaultPage;
}
private void RaiseDeleteSpUrlEvent(string objectId)
{
OnDeleteSpUrlEvent(new DeleteSpUrlEventArgs(objectId));
}
private void OnDeleteSpUrlEvent(DeleteSpUrlEventArgs ea)
{
if (DeleteSpUrlRaised != null)
DeleteSpUrlRaised(this, ea);
}
public event EventHandler<DeleteSpUrlEventArgs> DeleteSpUrlRaised;
}
As you can see I need to raise an event handler when it comes to delete an entry within the ListView RaiseDeleteSpUrlEvent(spUrlsPerUser.ObjectId);
When the code hits if (DeleteSpUrlRaised != null)
it always says it is null and does not raise the event. Could you please tell me what I am doing wrong?
Here is the code where I call this class
ListView sitesListView = new ListView {ItemsSource = listItems};
DataTemplate dataTemplate = new DataTemplate(() => new DefaultPageListViewTemplate());
DefaultPageListViewTemplate defaultPageListViewTemplate =
dataTemplate.CreateContent() as DefaultPageListViewTemplate;
if (defaultPageListViewTemplate != null)
{
defaultPageListViewTemplate.DeleteSpUrlRaised +=
(sender, args) => //some code here;
}
sitesListView.ItemTemplate = dataTemplate;
Thank you for your help