Billing/Billing.Shared/Views/IconSelectPage.xaml.cs

86 lines
2.5 KiB
C#

using Billing.Models;
using Billing.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
namespace Billing.Views
{
public partial class IconSelectPage : BillingPage
{
public static readonly BindableProperty IconsSourceProperty = BindableProperty.Create(nameof(IconsSource), typeof(IList<BillingIcon>), typeof(IconSelectPage));
public IList<BillingIcon> IconsSource
{
get => (IList<BillingIcon>)GetValue(IconsSourceProperty);
set => SetValue(IconsSourceProperty, value);
}
public Command IconCheck { get; }
public event EventHandler<string> IconChecked;
private string iconChecked;
public IconSelectPage(string icon)
{
iconChecked = icon;
IconCheck = new Command(OnIconCheck);
InitializeComponent();
InitIcons();
}
private void InitIcons()
{
var source = new List<BillingIcon>
{
new() { Icon = BaseModel.ICON_DEFAULT },
new() { Icon = "wallet" },
new() { Icon = "creditcard" },
new() { Icon = "debitcard" },
new() { Icon = "cmb" },
new() { Icon = "rcb" },
new() { Icon = "yuebao" },
new() { Icon = "zhaozhaoying" }
};
source.AddRange(IconConverter.IconPreset.Select(icon => new BillingIcon { Icon = $"#brand#{icon.Key}" }));
foreach (var icon in source)
{
if (icon.Icon == iconChecked)
{
icon.IsChecked = true;
}
}
IconsSource = source;
}
private async void OnIconCheck(object o)
{
if (o is string icon)
{
foreach (var ic in IconsSource)
{
ic.IsChecked = ic.Icon == icon;
}
iconChecked = icon;
IconChecked?.Invoke(this, icon);
await Navigation.PopAsync();
}
}
}
public class BillingIcon : BindableObject
{
public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(BillingIcon));
public bool IsChecked
{
get => (bool)GetValue(IsCheckedProperty);
set => SetValue(IsCheckedProperty, value);
}
public string Icon { get; set; }
}
}