Universal Windows Platform – Lights 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 Lights.xaml and then select Add to add the file to the Project

user-lights-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>
	<ItemsControl Name="Display" Loaded="Display_Loaded">
		<ItemsControl.ItemTemplate>
			<DataTemplate>
				<Grid>
					<Ellipse Height="100" Width="100" Margin="5">
						<Ellipse.Fill>
							<SolidColorBrush Color="{Binding Colour}"/>
						</Ellipse.Fill>
					</Ellipse>
					<Ellipse Height="100" Width="100" Margin="5" Visibility="{Binding Off}">
						<Ellipse.Fill>
							<SolidColorBrush Color="Black"/>
						</Ellipse.Fill>
					</Ellipse>
				</Grid>
			</DataTemplate>
		</ItemsControl.ItemTemplate>
		<ItemsControl.ItemsPanel>
			<ItemsPanelTemplate>
				<StackPanel Orientation="{Binding Path=Orientation}"/>
			</ItemsPanelTemplate>
		</ItemsControl.ItemsPanel>
	</ItemsControl>
</Viewbox>

It should appear as such:

xaml-user-lights-control

Step 8

From the Menu choose View and then Code

vs2017-view-code

Step 9

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

public class Item : System.ComponentModel.INotifyPropertyChanged
{
	private bool _isOn;
	private Windows.UI.Color _colour;

	public virtual void OnPropertyChanged(string propertyName)
	{
		PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
	}
	public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

	public bool IsOn
	{
		get { return _isOn; }
		set
		{
			_isOn = value;
			OnPropertyChanged("IsOn");
			OnPropertyChanged("Off");
		}
	}

	public Visibility Off
	{
		get { return IsOn ? Visibility.Collapsed : Visibility.Visible; }
	}

	public Windows.UI.Color Colour
	{
		get { return _colour; }
		set { _colour = value; OnPropertyChanged("Colour"); }
	}
}

private System.Collections.ObjectModel.ObservableCollection<Item> _items =
		new System.Collections.ObjectModel.ObservableCollection<Item>();

private Orientation _orientation = Orientation.Vertical;

public System.Collections.ObjectModel.ObservableCollection<Item> Items
{
	get { return _items; }
	set { _items = value; Display.ItemsSource = Items; }
}

public Orientation Orientation {
	get { return _orientation; }
	set { _orientation = value; }
}

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

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 below &;lt;Grid> enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="Play" Icon="Play" Label="Play" Click="Play_Click"/>
</CommandBar>
<local:Lights x:Name="Display" Height="300" Width="300" Orientation="Vertical" Loaded="Display_Loaded"/>

It should appear as such:

xaml-lights-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 const int red = 0;
private const int orange = 1;
private const int green = 2;

private async System.Threading.Tasks.Task<bool> Delay(int seconds = 2)
{
	await System.Threading.Tasks.Task.Delay(seconds * 1000);
	return true;
}

private void Display_Loaded(object sender, RoutedEventArgs e)
{
	Display.Items = new System.Collections.ObjectModel.ObservableCollection<Lights.Item>() {
		new Lights.Item() { Colour = Windows.UI.Colors.Red },
		new Lights.Item() { Colour = Windows.UI.Colors.Orange },
		new Lights.Item() { Colour = Windows.UI.Colors.Green, IsOn = true }
	};
}

private async void Play_Click(object sender, RoutedEventArgs e)
{
	Display.Items[red].IsOn = false;
	Display.Items[orange].IsOn = false;
	Display.Items[green].IsOn = true;
	await Delay();
	Display.Items[green].IsOn = false;  
	await Delay();
	Display.Items[orange].IsOn = true;
	await Delay();
	Display.Items[orange].IsOn = false;
	await Delay();
	Display.Items[red].IsOn = true;
	await Delay();
	Display.Items[red].IsOn = true;
	await Delay();
	Display.Items[orange].IsOn = true;
	await Delay();
	Display.Items[red].IsOn = false;
	Display.Items[orange].IsOn = false;
	Display.Items[green].IsOn = true;
	await Delay();
}

It should then appear as such:

code-lights-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-lights-control

Step 18

After the Application has started running you can then select Play to go cycle through the UK traffic light sequence displayed using the Lights Control.

ran-lights-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