Universal Windows Platform – Operator App

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 locate and select Visual Studio 2017.

vs2017-home

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-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

vs2017-new-project

Step 4

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 5

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 6

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 7

Then 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;
    }
}

Step 8

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-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:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
	<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>

It should appear as such:

xaml-operator-app
 

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the 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);
}

It should then appear as such:

code-operator-app
 

Step 13

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 14

Once started the Application should then appear

run-operator-app
 

Step 15

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.

ran-operator-app
 

Step 16

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Advertisements

One thought on “Universal Windows Platform – Operator App

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 )

Google+ photo

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

Connecting to %s