|
| 1 | +using System; |
| 2 | + |
| 3 | +using Xamarin.Forms; |
| 4 | + |
| 5 | +namespace LayoutSamples |
| 6 | +{ |
| 7 | + public class CalculatorGridCode : ContentPage |
| 8 | + { |
| 9 | + public CalculatorGridCode () |
| 10 | + { |
| 11 | + Title = "Calculator - C#"; |
| 12 | + BackgroundColor = Color.FromHex ("#404040"); |
| 13 | + |
| 14 | + var plainButton = new Style (typeof(Button)) { |
| 15 | + Setters = { |
| 16 | + new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromHex ("#eee") }, |
| 17 | + new Setter { Property = Button.TextColorProperty, Value = Color.Black }, |
| 18 | + new Setter { Property = Button.BorderRadiusProperty, Value = 0 }, |
| 19 | + new Setter { Property = Button.FontSizeProperty, Value = 40 } |
| 20 | + } |
| 21 | + }; |
| 22 | + var darkerButton = new Style (typeof(Button)) { |
| 23 | + Setters = { |
| 24 | + new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromHex ("#ddd") }, |
| 25 | + new Setter { Property = Button.TextColorProperty, Value = Color.Black }, |
| 26 | + new Setter { Property = Button.BorderRadiusProperty, Value = 0 }, |
| 27 | + new Setter { Property = Button.FontSizeProperty, Value = 40 } |
| 28 | + } |
| 29 | + }; |
| 30 | + var orangeButton = new Style (typeof(Button)) { |
| 31 | + Setters = { |
| 32 | + new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromHex ("#E8AD00") }, |
| 33 | + new Setter { Property = Button.TextColorProperty, Value = Color.White }, |
| 34 | + new Setter { Property = Button.BorderRadiusProperty, Value = 0 }, |
| 35 | + new Setter { Property = Button.FontSizeProperty, Value = 40 } |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + var controlGrid = new Grid { RowSpacing = 1, ColumnSpacing = 1 }; |
| 40 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (150) }); |
| 41 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }); |
| 42 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }); |
| 43 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }); |
| 44 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }); |
| 45 | + controlGrid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) }); |
| 46 | + |
| 47 | + controlGrid.ColumnDefinitions.Add (new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) }); |
| 48 | + controlGrid.ColumnDefinitions.Add (new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) }); |
| 49 | + controlGrid.ColumnDefinitions.Add (new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) }); |
| 50 | + controlGrid.ColumnDefinitions.Add (new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) }); |
| 51 | + |
| 52 | + var label = new Label { |
| 53 | + Text = "0", |
| 54 | + XAlign = TextAlignment.End, |
| 55 | + YAlign = TextAlignment.End, |
| 56 | + TextColor = Color.White, |
| 57 | + FontSize = 60 |
| 58 | + }; |
| 59 | + controlGrid.Children.Add (label, 0, 0); |
| 60 | + |
| 61 | + Grid.SetColumnSpan (label, 4); |
| 62 | + |
| 63 | + controlGrid.Children.Add (new Button { Text = "C", Style = darkerButton }, 0, 1); |
| 64 | + controlGrid.Children.Add (new Button { Text = "+/-", Style = darkerButton }, 1, 1); |
| 65 | + controlGrid.Children.Add (new Button { Text = "%", Style = darkerButton }, 2, 1); |
| 66 | + controlGrid.Children.Add (new Button { Text = "div", Style = orangeButton }, 3, 1); |
| 67 | + controlGrid.Children.Add (new Button { Text = "7", Style = plainButton }, 0, 2); |
| 68 | + controlGrid.Children.Add (new Button { Text = "8", Style = plainButton }, 1, 2); |
| 69 | + controlGrid.Children.Add (new Button { Text = "9", Style = plainButton }, 2, 2); |
| 70 | + controlGrid.Children.Add (new Button { Text = "X", Style = orangeButton }, 3, 2); |
| 71 | + controlGrid.Children.Add (new Button { Text = "4", Style = plainButton }, 0, 3); |
| 72 | + controlGrid.Children.Add (new Button { Text = "5", Style = plainButton }, 1, 3); |
| 73 | + controlGrid.Children.Add (new Button { Text = "6", Style = plainButton }, 2, 3); |
| 74 | + controlGrid.Children.Add (new Button { Text = "-", Style = orangeButton }, 3, 3); |
| 75 | + controlGrid.Children.Add (new Button { Text = "1", Style = plainButton }, 0, 4); |
| 76 | + controlGrid.Children.Add (new Button { Text = "2", Style = plainButton }, 1, 4); |
| 77 | + controlGrid.Children.Add (new Button { Text = "3", Style = plainButton }, 2, 4); |
| 78 | + controlGrid.Children.Add (new Button { Text = "+", Style = orangeButton }, 3, 4); |
| 79 | + controlGrid.Children.Add (new Button { Text = ".", Style = plainButton }, 2, 5); |
| 80 | + controlGrid.Children.Add (new Button { Text = "=", Style = orangeButton }, 3, 5); |
| 81 | + |
| 82 | + var zeroButton = new Button { Text = "0", Style = plainButton }; |
| 83 | + controlGrid.Children.Add (zeroButton, 0, 5); |
| 84 | + Grid.SetColumnSpan (zeroButton, 2); |
| 85 | + |
| 86 | + Content = controlGrid; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
0 commit comments