Windows 10 Universal Windows Platform – Lucky Dice

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

10-new-project

Step 4

Once done select from the Menu, Project, then Add New Item…

10-project-add-new-item

Step 5

From the Add New Item window select Visual C# 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

10-add-new-item-library

Step 6

Once in the Code View for Library.cs the following should be entered:

using System;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
{
    private Random random = new Random();

    private void add(Grid grid, int row, int column)
    {
        Ellipse dot = new Ellipse();
        dot.Width = 20;
        dot.Height = 20;
        dot.Fill = new SolidColorBrush(Colors.White);
        dot.SetValue(Grid.ColumnProperty, column);
        dot.SetValue(Grid.RowProperty, row);
        grid.Children.Add(dot);
    }

    public Grid Dice(int Value)
    {
        Grid grid = new Grid();
        grid.Height = 100;
        grid.Width = 100;
        for (int index = 0; (index <= 2); index++) // 3 by 3 Grid 
        {
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        switch (Value)
        {
            case 0:
                // No Dots
                break;
            case 1:
                add(grid, 1, 1); // Middle
                break;
            case 2:
                add(grid, 0, 2); // Top Right
                add(grid, 2, 0); // Bottom Left
                break;
            case 3:
                add(grid, 0, 2); // Top Right
                add(grid, 1, 1); // Middle
                add(grid, 2, 0); // Bottom Left
                break;
            case 4:
                add(grid, 0, 0); // Top Left
                add(grid, 0, 2); // Top Right
                add(grid, 2, 0); // Bottom Left
                add(grid, 2, 2); // Bottom Right
                break;
            case 5:
                add(grid, 0, 0); // Top Left
                add(grid, 0, 2); // Top Right
                add(grid, 1, 1); // Middle
                add(grid, 2, 0); // Bottom Left
                add(grid, 2, 2); // Bottom Right
                break;
            case 6:
                add(grid, 0, 0); // Top Left
                add(grid, 0, 2); // Top Right
                add(grid, 1, 0); // Middle Left
                add(grid, 1, 2); // Middle Right
                add(grid, 2, 0); // Bottom Left
                add(grid, 2, 2); // Bottom Right
                break;
        }
        return grid;
    }

    public int Roll()
    {
        return random.Next(1, 7);
    }
}

It should then appear as such:

10-library-luckydice

Step 7

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 8

Select from the Menu, View then Designer

10-designer

Step 9

The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Icon="Page" Label="New" Click="New_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Canvas Grid.Column="1" Name="DiceOne" Height="100" Width="100" Background="{ThemeResource ApplicationSecondaryForegroundThemeBrush}" Tapped="DiceOne_Tapped"/>
    <Canvas Grid.Column="3" Name="DiceTwo" Height="100" Width="100" Background="{ThemeResource ApplicationSecondaryForegroundThemeBrush}" Tapped="DiceTwo_Tapped"/>
</Grid>

It should appear as such:

10-xaml-luckydice

Step 10

Select from the Menu, View then Code

10-code

Step 11

Once in the Code View below the public MainPage() { … } the following should be entered:

public Library Library = new Library();
 
private void New_Click(object sender, RoutedEventArgs e)
{
    DiceOne.Children.Clear();
    DiceTwo.Children.Clear();
}
 
private void DiceOne_Tapped(object sender, RoutedEventArgs e)
{
    DiceOne.Children.Clear();
    DiceOne.Children.Add(Library.Dice(Library.Roll()));
}
 
private void DiceTwo_Tapped(object sender, RoutedEventArgs e)
{
    DiceTwo.Children.Clear();
    DiceTwo.Children.Add(Library.Dice(Library.Roll()));
}

It should then appear as such:

10-code-luckydice

Step 12

That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application

10-simulator

Step 13

Once the Simulator has started the Application should then appear

10-simulator-run-luckydice

Step 14

After the Application has started running you can then tap on either of the squares to roll the dice or use New to reset them

10-simulator-ran-luckydice

Step 15

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 16

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 17

Once the Emulator has started the Application should then appear

10-emulator-run-luckydice

Step 18

After the Application has started running you can then tap on either of the squares to roll the dice or use New to reset them

10-emulator-ran-luckydice

Step 19

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

One thought on “Windows 10 Universal Windows Platform – Lucky Dice

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