Billing/Billing.Shared/UI/ItemSelectPage.cs
2022-03-02 10:45:40 +08:00

69 lines
2.2 KiB
C#

using Billing.Themes;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Billing.UI
{
public class ItemSelectPage<T> : ContentPage
{
public event EventHandler<T> ItemTapped;
public ItemSelectPage(IEnumerable<T> source)
{
var content = new ListView
{
ItemsSource = source,
ItemTemplate = new DataTemplate(() => new ViewCell
{
View = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(20, 0),
Spacing = 10,
Children =
{
new TintImage
{
WidthRequest = 22,
HeightRequest = 22,
Aspect = Aspect.AspectFit,
VerticalOptions = LayoutOptions.Center
}
.Binding(Image.SourceProperty, "Icon"),
new Label
{
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation
}
.Binding(Label.TextProperty, "Name")
.DynamicResource(Label.TextColorProperty, BaseTheme.TextColor)
}
}
})
}
.DynamicResource(BackgroundColorProperty, BaseTheme.WindowBackgroundColor);
// events
content.ItemTapped += List_ItemTapped;
Content = content;
}
private void List_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item is T t)
{
ItemTapped?.Invoke(this, t);
}
}
}
public class SelectItem<T>
{
public string Icon { get; set; }
public T Value { get; set; }
public string Name { get; set; }
}
}