From 283acf7d35baa36e9bb952bd7dc7b764d32b8974 Mon Sep 17 00:00:00 2001 From: Tsanie Lily Date: Sat, 26 Feb 2022 17:29:12 +0800 Subject: [PATCH] balance binding --- Billing.Shared/UI/Definition.cs | 26 ++++++------- Billing.Shared/UI/OptionsCells.cs | 38 +++++++++++++------ Billing.Shared/Views/AddAccountPage.xaml | 5 ++- Billing.Shared/Views/AddAccountPage.xaml.cs | 10 +++++ Billing.sln | 10 ++--- .../Resources/Resource.designer.cs | 2 +- 6 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Billing.Shared/UI/Definition.cs b/Billing.Shared/UI/Definition.cs index 9a293e0..29bc40a 100644 --- a/Billing.Shared/UI/Definition.cs +++ b/Billing.Shared/UI/Definition.cs @@ -19,7 +19,7 @@ namespace Billing.UI return obj; } - public static View HorizontalOptions(this View view, LayoutOptions options) + public static T HorizontalOptions(this T view, LayoutOptions options) where T : View { if (view != null) { @@ -28,7 +28,7 @@ namespace Billing.UI return view; } - public static View VerticalOptions(this View view, LayoutOptions options) + public static T VerticalOptions(this T view, LayoutOptions options) where T : View { if (view != null) { @@ -37,41 +37,41 @@ namespace Billing.UI return view; } - public static View DynamicResource(this View view, BindableProperty property, string key) + public static T Margin(this T view, Thickness margin) where T : View + { + view.Margin = margin; + return view; + } + + public static T DynamicResource(this T view, BindableProperty property, string key) where T : Element { view.SetDynamicResource(property, key); return view; } - public static View GridColumn(this View view, int column) + public static T GridColumn(this T view, int column) where T : BindableObject { Grid.SetColumn(view, column); return view; } - public static View GridRow(this View view, int row) + public static T GridRow(this T view, int row) where T : BindableObject { Grid.SetRow(view, row); return view; } - public static View GridColumnSpan(this View view, int columnSpan) + public static T GridColumnSpan(this T view, int columnSpan) where T : BindableObject { Grid.SetColumnSpan(view, columnSpan); return view; } - public static View GridRowSpan(this View view, int rowSpan) + public static T GridRowSpan(this T view, int rowSpan) where T : BindableObject { Grid.SetRowSpan(view, rowSpan); return view; } - - public static View Margin(this View view, Thickness margin) - { - view.Margin = margin; - return view; - } } public class Tap : IDisposable diff --git a/Billing.Shared/UI/OptionsCells.cs b/Billing.Shared/UI/OptionsCells.cs index e01cd37..f84dd11 100644 --- a/Billing.Shared/UI/OptionsCells.cs +++ b/Billing.Shared/UI/OptionsCells.cs @@ -1,4 +1,5 @@ using Billing.Themes; +using System; using Xamarin.Forms; namespace Billing.UI @@ -269,19 +270,34 @@ namespace Billing.UI set => SetValue(PlaceholderProperty, value); } - protected override View Content => new OptionEntry + public event EventHandler Unfocused; + + protected override View Content { - HorizontalOptions = LayoutOptions.Fill, - HorizontalTextAlignment = TextAlignment.End, - VerticalOptions = LayoutOptions.Center, - ReturnType = ReturnType.Next + get + { + var content = new OptionEntry + { + HorizontalOptions = LayoutOptions.Fill, + HorizontalTextAlignment = TextAlignment.End, + VerticalOptions = LayoutOptions.Center, + ReturnType = ReturnType.Next + } + .Binding(Entry.TextProperty, nameof(Text), BindingMode.TwoWay) + .Binding(InputView.KeyboardProperty, nameof(Keyboard)) + .Binding(Entry.PlaceholderProperty, nameof(Placeholder)) + .DynamicResource(Entry.TextColorProperty, BaseTheme.TextColor) + .DynamicResource(Entry.PlaceholderColorProperty, BaseTheme.SecondaryTextColor) + .DynamicResource(VisualElement.BackgroundColorProperty, BaseTheme.OptionTintColor); + content.Unfocused += Content_Unfocused; + return content; + } + } + + private void Content_Unfocused(object sender, FocusEventArgs e) + { + Unfocused?.Invoke(sender, e); } - .Binding(Entry.TextProperty, nameof(Text), BindingMode.TwoWay) - .Binding(InputView.KeyboardProperty, nameof(Keyboard)) - .Binding(Entry.PlaceholderProperty, nameof(Placeholder)) - .DynamicResource(Entry.TextColorProperty, BaseTheme.TextColor) - .DynamicResource(Entry.PlaceholderColorProperty, BaseTheme.SecondaryTextColor) - .DynamicResource(VisualElement.BackgroundColorProperty, BaseTheme.OptionTintColor); } public class OptionEditorCell : OptionVerticalCell diff --git a/Billing.Shared/Views/AddAccountPage.xaml b/Billing.Shared/Views/AddAccountPage.xaml index b16b064..37761d7 100644 --- a/Billing.Shared/Views/AddAccountPage.xaml +++ b/Billing.Shared/Views/AddAccountPage.xaml @@ -39,8 +39,9 @@ + Text="{Binding Balance, Converter={StaticResource moneyConverter}}" + Keyboard="Numeric" Placeholder="{r:Text BalancePlaceholder}" + Unfocused="Balance_Unfocused"/> diff --git a/Billing.Shared/Views/AddAccountPage.xaml.cs b/Billing.Shared/Views/AddAccountPage.xaml.cs index e2468f8..6b365ab 100644 --- a/Billing.Shared/Views/AddAccountPage.xaml.cs +++ b/Billing.Shared/Views/AddAccountPage.xaml.cs @@ -69,6 +69,16 @@ namespace Billing.Views InitializeComponent(); } + private void Balance_Unfocused(object sender, FocusEventArgs e) + { + if (sender is OptionEntry entry && decimal.TryParse(entry.Text, out decimal d)) + { + var converter = (MoneyConverter)Resources["moneyConverter"]; + entry.Text = converter.Convert(d, null, null, null)?.ToString(); + //Balance = d; + } + } + private async void OnCheckAccount() { if (Tap.IsBusy) diff --git a/Billing.sln b/Billing.sln index a737a6b..84061b3 100644 --- a/Billing.sln +++ b/Billing.sln @@ -9,7 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Billing.iOS", "Billing\Bill EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Billing.Shared", "Billing.Shared\Billing.Shared.shproj", "{6AC75D01-70D6-4A07-8685-BC52AFD97A7A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Svg2Png", "Svg2Png\Svg2Png.csproj", "{43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Svg2Png", "Svg2Png\Svg2Png.csproj", "{43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}" EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution @@ -62,10 +62,10 @@ Global {5C4F1C35-6F66-4063-9605-A9F37FCABBA8}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator {5C4F1C35-6F66-4063-9605-A9F37FCABBA8}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator {5C4F1C35-6F66-4063-9605-A9F37FCABBA8}.Release|iPhoneSimulator.Deploy.0 = Release|iPhoneSimulator - {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator - {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator - {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhone.ActiveCfg = Debug|iPhoneSimulator - {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhone.Build.0 = Debug|iPhoneSimulator + {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhone.Build.0 = Debug|Any CPU {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator {43BB5B21-61E0-42BB-ADF1-DBCD662E61E1}.Release|Any CPU.ActiveCfg = Release|iPhoneSimulator diff --git a/Billing/Billing.Android/Resources/Resource.designer.cs b/Billing/Billing.Android/Resources/Resource.designer.cs index 09f20b5..4a225e0 100644 --- a/Billing/Billing.Android/Resources/Resource.designer.cs +++ b/Billing/Billing.Android/Resources/Resource.designer.cs @@ -14,7 +14,7 @@ namespace Billing.Droid { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.2.0.155")] public partial class Resource {