Universal Windows Platform – Tutorial Insider – Operator App


comentsys.wordpress.com today features a Universal Windows Platform tutorial showing how to create an Operator App which will allow create a basic calculator based on some common C# Operators for addition, subtraction, multiplication and division.


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.

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.

Behind the Scenes

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. This example was created twice – the first time it just didn’t look good enough so was redone again to improve the look-and-feel and to simplify it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s