|
| 1 | +// ExpressionPage.cs |
| 2 | + |
| 3 | +using CommunityToolkit.Maui.Markup; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using SQuan.Helpers.Maui; |
| 6 | + |
| 7 | +namespace SQuan.Helpers.Sample; |
| 8 | + |
| 9 | +[System.Diagnostics.CodeAnalysis.SuppressMessage( |
| 10 | + "Design", |
| 11 | + "CA1001:Types that own disposable fields should be disposable", |
| 12 | + Justification = "CTS is created in OnNavigatedTo and cancelled/disposed in OnNavigatedFrom as part of the page navigation lifecycle, so implementing IDisposable on this framework-managed ContentPage is unnecessary.")] |
| 13 | +public partial class ExpressionPage : ContentPage |
| 14 | +{ |
| 15 | + public static ILogger? Logger { get; private set; } = IPlatformApplication.Current?.Services.GetService<ILogger<ExpressionPage>>(); |
| 16 | + public ExpressionManager EM { get; } |
| 17 | + CancellationTokenSource? cts; |
| 18 | + |
| 19 | + public ExpressionNode X1 { get; } |
| 20 | + public ExpressionNode X2 { get; } |
| 21 | + public ExpressionNode Sum { get; } |
| 22 | + public ExpressionNode Product { get; } |
| 23 | + public ExpressionNode Hypotenuse { get; } |
| 24 | + public ExpressionNode SerialCode { get; } |
| 25 | + public ExpressionNode BadExpression { get; } |
| 26 | + public ExpressionNode RandomNumber { get; } |
| 27 | + |
| 28 | + public ExpressionPage() |
| 29 | + { |
| 30 | + EM = new(); |
| 31 | + EM.SetInvokeOnUIThread(Dispatcher); |
| 32 | + X1 = EM.SetValue<double>("/survey/x1", 3); |
| 33 | + X2 = EM.SetValue<double>("/survey/x2", 4); |
| 34 | + Sum = EM.SetExpression<double>("/survey/sum", "/survey/x1 + /survey/x2"); |
| 35 | + Product = EM.SetExpression<double>("/survey/product", "/survey/x1 * /survey/x2"); |
| 36 | + Hypotenuse = EM.SetExpression<double>("/survey/hypotenuse", "sqrt(pow(/survey/x1, 2) + pow(/survey/x2, 2))"); |
| 37 | + SerialCode = EM.SetExpression<string>("/survey/serialcode", "concat(/survey/x1, '-ABC-', /survey/x2)"); |
| 38 | + BadExpression = EM.SetExpression<double>("/survey/parseerror", "invalid_expression("); |
| 39 | + RandomNumber = EM.SetExpression<double>("/survey/random", "random()"); |
| 40 | + |
| 41 | + Content = new ScrollView |
| 42 | + { |
| 43 | + Content = new VerticalStackLayout |
| 44 | + { |
| 45 | + Spacing = 10, |
| 46 | + Padding = new Thickness(10, 10, 10, 250), |
| 47 | + Children = |
| 48 | + { |
| 49 | + new Label { Text = "Enter X1" }, |
| 50 | + new Entry { }.Bind(Entry.TextProperty, "EM[/survey/x1]", BindingMode.TwoWay, source: this), |
| 51 | + new Label { Text = "Enter X2" }, |
| 52 | + new Entry { }.Bind(Entry.TextProperty, "Value", BindingMode.TwoWay, source: X2), |
| 53 | + new Label { }.Bind(Label.TextProperty, |
| 54 | + new Binding("[/survey/sum]", BindingMode.OneWay, source: EM), |
| 55 | + new Binding("ValueType", BindingMode.OneWay, source: Sum), |
| 56 | + new Binding("ValueKind", BindingMode.OneWay, source: Sum), |
| 57 | + "Sum: {0} (valueType: {1}, valueKind: {2})"), |
| 58 | + new Label { }.Bind(Label.TextProperty, |
| 59 | + BindingBase.Create(static (ExpressionNode n) => n.Value, BindingMode.OneWay, source: Product), |
| 60 | + BindingBase.Create(static (ExpressionNode n) => n.ValueType, BindingMode.OneWay, source: Product), |
| 61 | + BindingBase.Create(static (ExpressionNode n) => n.ValueKind, BindingMode.OneWay, source: Product), |
| 62 | + "Product: {0} (valueType: {1}, valueKind: {2})"), |
| 63 | + new Label { }.Bind(Label.TextProperty, |
| 64 | + BindingBase.Create(static (ExpressionNode n) => n.Value, BindingMode.OneWay, source: Hypotenuse), |
| 65 | + BindingBase.Create(static (ExpressionNode n) => n.ValueType, BindingMode.OneWay, source: Hypotenuse), |
| 66 | + BindingBase.Create(static (ExpressionNode n) => n.ValueKind, BindingMode.OneWay, source: Hypotenuse), |
| 67 | + "Hypotenuse: {0} (valueType: {1}, valueKind: {2})"), |
| 68 | + new Label { }.Bind(Label.TextProperty, |
| 69 | + BindingBase.Create(static (ExpressionNode n) => n.Value, BindingMode.OneWay, source: SerialCode), |
| 70 | + BindingBase.Create(static (ExpressionNode n) => n.ValueType, BindingMode.OneWay, source: SerialCode), |
| 71 | + BindingBase.Create(static (ExpressionNode n) => n.ValueKind, BindingMode.OneWay, source: SerialCode), |
| 72 | + "Serial Code: {0} (valueType: {1}, valueKind: {2})"), |
| 73 | + new Label { }.Bind(Label.TextProperty, |
| 74 | + BindingBase.Create(static (ExpressionNode n) => n.Value, BindingMode.OneWay, source: BadExpression), |
| 75 | + BindingBase.Create(static (ExpressionNode n) => n.ValueType, BindingMode.OneWay, source: BadExpression), |
| 76 | + BindingBase.Create(static (ExpressionNode n) => n.ValueKind, BindingMode.OneWay, source: BadExpression), |
| 77 | + "Bad Expression: {0} (valueType: {1}, valueKind: {2})"), |
| 78 | + new Label { }.Bind(Label.TextProperty, |
| 79 | + BindingBase.Create(static (ExpressionNode n) => n.Value, BindingMode.OneWay, source: RandomNumber), |
| 80 | + BindingBase.Create(static (ExpressionNode n) => n.ValueType, BindingMode.OneWay, source: RandomNumber), |
| 81 | + BindingBase.Create(static (ExpressionNode n) => n.ValueKind, BindingMode.OneWay, source: RandomNumber), |
| 82 | + "Random Number: {0} (valueType: {1}, valueKind: {2})"), |
| 83 | + new Button { Text = "Recalculate Random Number" } |
| 84 | + .Invoke(b => b.Clicked += (s, e) => EM.RecalculateByNodeRef("/survey/random")) |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + protected override void OnNavigatedTo(NavigatedToEventArgs args) |
| 91 | + { |
| 92 | + base.OnNavigatedTo(args); |
| 93 | + try |
| 94 | + { |
| 95 | + cts = new(); |
| 96 | + EM.StartCalculationLoop(cts.Token); |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + { |
| 100 | + Logger?.LogError(ex, "An error occurred while starting the calculation loop in OnNavigatedTo."); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + protected override async void OnNavigatedFrom(NavigatedFromEventArgs args) |
| 105 | + { |
| 106 | + base.OnNavigatedFrom(args); |
| 107 | + try |
| 108 | + { |
| 109 | + await EM.StopCalculationLoopAsync(); |
| 110 | + await EM.ClearAsync(); |
| 111 | + cts?.Cancel(); |
| 112 | + cts?.Dispose(); |
| 113 | + cts = null; |
| 114 | + EM.Dispose(); |
| 115 | + } |
| 116 | + catch (Exception ex) |
| 117 | + { |
| 118 | + Logger?.LogError(ex, "An error occurred during cleanup in OnNavigatedFrom."); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +public static class BindHelpers |
| 124 | +{ |
| 125 | + public static T Bind<T>( |
| 126 | + this T target, |
| 127 | + BindableProperty targetProperty, |
| 128 | + BindingBase binding1, |
| 129 | + BindingBase binding2, |
| 130 | + BindingBase binding3, |
| 131 | + string stringFormat) where T : BindableObject |
| 132 | + { |
| 133 | + target.SetBinding(targetProperty, new MultiBinding |
| 134 | + { |
| 135 | + Bindings = |
| 136 | + { |
| 137 | + binding1, |
| 138 | + binding2, |
| 139 | + binding3 |
| 140 | + }, |
| 141 | + StringFormat = stringFormat |
| 142 | + }); |
| 143 | + return target; |
| 144 | + } |
| 145 | +} |
0 commit comments