Universal Windows Platform – Directs Control

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 XAML then select User Control and then in the Name as Directs.xaml and then select Add to add the file to the Project

user-directs-control

Step 7

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>
	<Grid Name="Pad" Width="180" Height="180" Loaded="Pad_Loaded">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="Auto"/>
			<ColumnDefinition Width="*"/>
		</Grid.ColumnDefinitions>
		<Grid.RowDefinitions>
			<RowDefinition Height="*"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="*"/>
		</Grid.RowDefinitions>
		<Path Name="Up" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Top" Margin="5"
		  Data="M 0,0 40,0 40,60 20,80 0,60 0,0 z" Fill="{Binding Foreground}" PointerMoved="Pad_PointerMoved"/>
		<Path Name="Down" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Bottom" Margin="5"
		  Data="M 0,20 20,0 40,20 40,80 0,80 z" Fill="{Binding Foreground}" PointerMoved="Pad_PointerMoved"/>
		<Path Name="Left" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="5"
		  Data="M 0,0 60,0 80,20 60,40 0,40 z" Fill="{Binding Foreground}" PointerMoved="Pad_PointerMoved"/>
		<Path Name="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="5"
		  Data="M 0,20 20,0 80,0 80,40 20,40 z" Fill="{Binding Foreground}" PointerMoved="Pad_PointerMoved"/>
	</Grid>
</Viewbox>

It should appear as such:

xaml-user-directs-control

Step 8

From the Menu choose View and then Code

vs2017-view-code

Step 9

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

public enum Directions
{
	Up = 0,
	Down = 1,
	Left = 2,
	Right = 3
}

public delegate void DirectionEvent(object sender, Directions direction);
public event DirectionEvent Direction;

private void Pad_Loaded(object sender, RoutedEventArgs e)
{
	this.DataContext = this;
}

private void Pad_PointerMoved(object sender, PointerRoutedEventArgs e)
{
	Windows.UI.Input.PointerPoint point = e.GetCurrentPoint(Pad);
	bool fire = (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse) ?
		point.Properties.IsLeftButtonPressed : point.IsInContact;
	if (fire)
	{
		Windows.UI.Xaml.Shapes.Path path = ((Windows.UI.Xaml.Shapes.Path)sender);
		if (Direction != null)
		{
			this.Direction(path, (Directions)Enum.Parse(typeof(Directions), path.Name));
		}
	}
}

Step 10

From the Menu choose Build and then Build Solution

vs2017-build

Step 11

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 12

From the Menu choose View and then Designer

vs2017-view-designer

Step 13

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
	<TextBlock Name="Label" HorizontalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/>
	<local:Directs x:Name="Pad" Height="300" Width="300" Foreground="{ThemeResource AccentButtonBackground}" 
		Direction="Pad_Direction"/>
</StackPanel>

It should appear as such:

xaml-directs-control

Step 14

From the Menu choose View and then Code

vs2017-view-code

Step 15

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

private void Pad_Direction(object sender, Directs.Directions direction)
{
	Label.Text = direction.ToString();
}

It should then appear as such:

code-directs-control

Step 16

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 17

Once started the Application should then appear

run-directs-control

Step 18

After the Application has started running you can then select the parts of the Directs Control for Up, Down, Left and Right.

ran-directs-control

Step 19

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Advertisements

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