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

user-matrix-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="Display"/>
</Viewbox>

It should appear as such:

xaml-user-matrix-control

Step 8

From the Menu choose View and then Code

vs2017-view-code

Step 9

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

private readonly byte[][] table =
{
	new byte[] {
	0,0,0,0,0,0,0,0,
	0,1,1,1,1,1,1,0,
	0,1,1,0,0,1,1,0,
	0,1,1,0,0,1,1,0,
	0,1,1,0,0,1,1,0,
	0,1,1,1,1,1,1,0,
	0,0,0,0,0,0,0,0
	}, // 0
	new byte[] {
	0,0,0,0,0,0,0,0,
	0,0,0,1,1,0,0,0,
	0,1,1,1,1,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,0,0,0,0,0
	}, // 1 
	new byte[] {
	0,0,0,0,0,0,0,0,
	0,1,1,1,1,1,1,0,
	0,0,0,0,0,1,1,0,
	0,1,1,1,1,1,1,0,
	0,1,1,0,0,0,0,0,
	0,1,1,1,1,1,1,0,
	0,0,0,0,0,0,0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,0,0,0,0,
	0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0
	}, // None
	new byte[] {
	0,0,0,0,0,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,0,0,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,1,1,0,0,0,
	0,0,0,0,0,0,0,0
	}, // Colon
	new byte[] {
	0,0,0,0,0,0,0,0,
	0,0,0,0,0,1,1,0,
	0,0,0,0,1,1,0,0,
	0,0,0,1,1,0,0,0,
	0,0,1,1,0,0,0,0,
	0,1,1,0,0,0,0,0,
	0,0,0,0,0,0,0,0
	} // Slash
};
private readonly List<char> symbols =
	new List<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', ':', '/' };
private const int columns = 8;
private const int rows = 7;
private const int size = 10;
private byte[,] board = null;
private int count = 0;
private string _output = string.Empty;

private byte[,] AsTwoDimensionalArray(byte[] input, int height, int width)
{
	byte[,] output = new byte[height, width];
	for (int r = 0; r < height; r++)
	{
		for (int c = 0; c < width; c++)
		{
			output[r, c] = input[r * width + c];
		}
	}
	return output;
}

private int SetBoard(char[] characters)
{
	int length = characters.Count();
	board = new byte[rows, (columns * length)];
	int offset = 0;
	foreach (char character in characters)
	{
		int index = symbols.IndexOf(character);
		byte[,] symbol = AsTwoDimensionalArray(table[index], rows, columns);
		for (int row = 0; row < rows; row++)
		{
			for (int column = 0; column < columns; column++)
			{
				board[row, column + offset] = symbol[row, column];
			}
		}
		offset += columns;
	}
	return length;
}

private Windows.UI.Xaml.Shapes.Rectangle GetPixel(int row, int column)
{
	Windows.UI.Xaml.Shapes.Rectangle rect = 
		new Windows.UI.Xaml.Shapes.Rectangle()
	{
		Height = size,
		Width = size,
		Fill = Foreground,
		RadiusX = 2,
		RadiusY = 2,
		Opacity = 0,
		Margin = new Thickness(2)
	};
	rect.SetValue(Grid.ColumnProperty, column);
	rect.SetValue(Grid.RowProperty, row);
	return rect;
}

private void Setup()
{
	for (int row = 0; row < board.GetLength(0); row++)
	{
		for (int column = 0; column < board.GetLength(1); column++)
		{
			Windows.UI.Xaml.Shapes.Rectangle rect = 
				Display.Children.Cast<Windows.UI.Xaml.Shapes.Rectangle>()
				.FirstOrDefault(
				f => Grid.GetRow(f) == row 
				&& Grid.GetColumn(f) == column);
			rect.Opacity = board[row, column];
		}
	}
}

private void Layout(int length)
{
	int total = (columns * length);
	Display.Children.Clear();
	Display.RowDefinitions.Clear();
	Display.ColumnDefinitions.Clear();
	// Grid
	for (int row = 0; row < rows; row++)
	{
		Display.RowDefinitions.Add(new RowDefinition());
	}
	for (int column = 0; column < total; column++)
	{
		Display.ColumnDefinitions.Add(new ColumnDefinition());
	}
	// Layout
	for (int row = 0; row < rows; row++)
	{
		for (int column = 0; column < total; column++)
		{
			Display.Children.Add(GetPixel(row, column));
		}
	}
	Display.Height = Display.RowDefinitions.Count() * (size + 10);
	Display.Width = (Display.ColumnDefinitions.Count() * (size + 20)) / 2;
}

private void SetOutput(string content)
{
	char[] characters = content.ToCharArray();
	int length = SetBoard(characters);
	if (characters.Length != count)
	{
		Layout(characters.Length);
	}
	Setup();
}

public string Output
{
	get { return _output; }
	set
	{
		if (_output != value)
		{
			_output = value;
			SetOutput(_output);
		}
	}
}

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:

<local:Matrix x:Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"  Margin="50" Foreground="{ThemeResource AccentButtonBackground}" Loaded="Display_Loaded"/>

It should appear as such:

xaml-matrix-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 Display_Loaded(object sender, RoutedEventArgs e)
{
	int count = 0;
	DispatcherTimer timer = new DispatcherTimer()
	{
		Interval = TimeSpan.FromMilliseconds(100)
	};
	timer.Tick += (object s, object args) =>
	{
		if (count > 150) count = 0;
		string format = (count < 100) ? "HH:mm:ss" : "dd/MM/yyyy";
		Display.Output = DateTime.Now.ToString(format);
		count++;
	};
	timer.Start();
}

It should then appear as such:

code-matrix-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 displaying a Matrix based Clock that will also alternate to show Time and then Date

run-matrix-control

Step 18

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