Hello everyone, I'm working on a project that I'm doing everything in C#, now that I started implementing ListView I'm worried if I can do everything I want to do in C#.
I have this class:
public class PlacesBD
{
[PrimaryKey,AutoIncrement]
public int Id { get; set; }
public string name { get; set; }
public double lat { get; set; }
public double lon { get; set; }
public sbyte timeZone { get; set; }
public string ImageResourceId { get; set; }
}
This CustomCell:
public class PlaceCustomCell:ViewCell
{
public static readonly BindableProperty SelectedItemBackgroundColorProperty =
BindableProperty.Create("SelectedItemBackgroundColor",
typeof(Color), typeof(PlaceCustomCell),
Color.Default);
public Color SelectedItemBackgroundColor
{
get
{
return (Color)GetValue(SelectedItemBackgroundColorProperty);
}
set
{
SetValue(SelectedItemBackgroundColorProperty, value);
}
}
public PlaceCustomCell()
{
var placeImage = new Image
{
HeightRequest = 50,
WidthRequest = 50,
Aspect = Aspect.Fill,
VerticalOptions = LayoutOptions.Center
};
placeImage.SetBinding(Image.SourceProperty, new Binding("ImageResourceId"));
SelectedItemBackgroundColor = Color.FromRgb(0, 160, 220);
var nameLabel = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
FontAttributes = FontAttributes.Bold,
VerticalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Start,
};
nameLabel.SetBinding(Label.TextProperty, new Binding("name"));
string stringPercentage = RandomNumber(300, 699).ToString();
stringPercentage+= "%";
var percentageLabel = new Label
{
Text = stringPercentage,
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
FontAttributes = FontAttributes.Bold,
VerticalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.End
};
var grid = new Grid()
{
ColumnDefinitions = {
new ColumnDefinition { Width = new GridLength(0.2, GridUnitType.Star)},
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star)},
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star)},
},
ColumnSpacing = 0,
RowSpacing = 0
};
grid.Children.Add(placeImage, 0, 0);
grid.Children.Add(nameLabel, 1, 0);
grid.Children.Add(percentageLabel, 2, 0);
View = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Padding = new Thickness(5, 5, 5, 5),
HeightRequest = 60,
Children = { grid }
};
}
The ItemSource comes from:
protected async override void OnAppearing()
{
base.OnAppearing();
LocalSQLService BD = new LocalSQLService();
List<PlacesBD> resultado = BD.Consultar();
placesListView.ItemsSource = resultado.ToList();
}
And I want to pass values of the PlacesBD class when the item is tapped:
public async void OnPlaceTapped(object sender, ItemTappedEventArgs e)
{
if(e == null)
{
return;
}
Debug.WriteLine("Tapped: " + e.Item);
((ListView)sender).SelectedItem = null; // de-select the row
}
How can I create a new page and pass my object PlacesBD data to the new page?