Operator App demonstrates how to create a basic Calculator based on some common C# Operators for addition, subtraction, multiplication and division.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Windows.Input; using Windows.UI.Xaml.Controls; public class CommandHandler : ICommand { public event EventHandler CanExecuteChanged = null; private Action _action; public CommandHandler(Action action) { _action = action; } public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { _action(); } } public class Item { public enum ItemMode { Operand = 1, Operator = 2 } public ItemMode Mode { get; set; } public Func<Item, int> Action { get; set; } public ICommand Command { get { return new CommandHandler(() => this.Action(this)); } } public object Value { get; set; } } public class Library { private static double first_operand; private static double second_operand; private static string operation = null; private static TextBlock _output; private static Func<Item, int> action = (Item item) => { switch (item.Mode) { case Item.ItemMode.Operand: _output.Text += (int)item.Value; break; case Item.ItemMode.Operator: string selected = (string)item.Value; if (selected == "=") { double result = 0.0; if (_output.Text.Length > 0) { second_operand = double.Parse(_output.Text); switch (operation) { case "/": if (second_operand > 0) { result = first_operand / second_operand; } break; case "*": result = first_operand * second_operand; break; case "-": result = first_operand - second_operand; break; case "+": result = first_operand + second_operand; break; } _output.Text = result.ToString(); } } else if (selected == "<") { if (_output.Text.Length > 0) { _output.Text = _output.Text.Substring(0, _output.Text.Length - 1); } } else { if (_output.Text.Length > 0) { first_operand = int.Parse(_output.Text); _output.Text = string.Empty; operation = item.Value.ToString(); } } break; } return 0; }; private List<Item> list = new List<Item> { new Item { Mode = Item.ItemMode.Operand, Value = 7, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 8, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 9, Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "/", Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 4, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 5, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 6, Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "*", Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 1, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 2, Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 3, Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "-", Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "<", Action = action }, new Item { Mode = Item.ItemMode.Operand, Value = 0, Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "=", Action = action }, new Item { Mode = Item.ItemMode.Operator, Value = "+", Action = action }, }; public List<Item> New(ref TextBlock text) { _output = text; return list; } }
There’s a CommandHandler Class that will help handle any commanding events and actions from the Library Class. There’s also a Item Class with an ItemMode for either Operand or Operator, an Action to represent what happens during commanding and an ICommand value that uses the CommandHandler and defines what’s passed into the Action and there’s a Value for storing content for the Item.
The Library Class defines two double variables to store the operands used in a Calculation and there’s a string for the kind of Calculation operation to be performed and a variable for a TextBlock for Output. A lambda function is defined which defines what an Action will do during commanding and has a switch statement which will if the ItemMode is Operand will append the TextBlock with the Value from an Item Object, if the ItemMode is Operator then it will check if that is an = which will product the result of a Calculation or if not then will use another switch statement to determine if it is /, *, – or + to perform the selected kind of Calculation – there’s also check to see if the Operator is < which will remove numbers from the Output to be used if any mistakes entered. There’s then a List of Item which defined all the Operand and Operator items supported and sets the Action to be the same for each one and then a New method which sets a passed in TextBlock for Output and returns the list of Item.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Viewbox Margin="50"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Name="Output" TextAlignment="Right" FontSize="30" HorizontalAlignment="Stretch" Foreground="{ThemeResource AccentButtonBackground}"/> <ItemsControl Grid.Row="1" Name="Display" Loaded="Display_Loaded"> <ItemsControl.ItemTemplate> <DataTemplate> <Button Margin="5" Height="50" Width="50" Background="{ThemeResource AccentButtonBackground}" Command="{Binding Command}"> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="{ThemeResource AccentButtonForeground}" Text="{Binding Value}"/> </Button> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <ItemsWrapGrid MaximumRowsOrColumns="4" Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </Grid> </Viewbox>
The MainPage has a Grid with two rows – the first represents the TextBlock for output and sets the Foreground and Background properties amongst others. Then there’s an ItemsControl which has it’s ItemsSource set to the New method from the Library Class in the Display_Loaded Event, within this is a DataTemplate defining the Button and also has the Foreground and Background properties set plus the Command for commanding to trigger the appropriate Action – to make sure the layout appears correctly the ItemsPanelTemplate is set to be a ItemsWrapGrid with MaximumRowsOrColumns of 4 and the Orientation set to Horizontal.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void Display_Loaded(object sender, RoutedEventArgs e) { Display.ItemsSource = library.New(ref Output); }
Below the MainPage() Method an instance of the Library Class is created, then Display_Loaded is used to set the ItemsSource of the ItemsControl to display the look-and-feel of the application.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then select any of the Button controls and select either an Operator such as *, +, /, – or < to delete a number from the display or any number from 0 to 9 then you can use = to get the answer.
Step 15
To Exit the Application select the Close button in the top right of the Application
This is by no means a fully featured calculator – for example there’s no . but it could be extended to support other Operators from C# such as % for Modulus, the main point of the example was to show the flexability of combining commanding with a list of items to product the necessary functionality of an application and to implement the layout.