Universal Windows Platform – Matrix Control

Matrix Control demonstrates how to create a Matrix Control to display the current Time and Date.

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

vs2017

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 the Name as MatrixControl and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

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

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Matrix.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

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

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace MatrixControl
{
    public class Matrix : StackPanel
    {
        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,0
            }, // 2
            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,0,0,0,0,1,1,0,
            0,1,1,1,1,1,1,0,
            0,0,0,0,0,0,0,0
            }, // 3
            new byte[] {
            0,0,0,0,0,0,0,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,1,1,0,
            0,0,0,0,0,1,1,0,
            0,0,0,0,0,0,0,0
            }, // 4
            new byte[] {
            0,0,0,0,0,0,0,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,1,1,0,
            0,1,1,1,1,1,1,0,
            0,0,0,0,0,0,0,0
            }, // 5
            new byte[] {
            0,0,0,0,0,0,0,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,1,1,0,0,1,1,0,
            0,1,1,1,1,1,1,0,
            0,0,0,0,0,0,0,0
            }, // 6
            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,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
            }, // 7
            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,1,1,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
            }, // 8
            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,1,1,1,1,0,
            0,0,0,0,0,1,1,0,
            0,1,1,1,1,1,1,0,
            0,0,0,0,0,0,0,0
            }, // 9
            new byte[] {
            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,0,0,0,0,
            0,0,0,0,0,0,0,0
            }, // Space
            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> glyphs =
            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 padding = 1;

        private string _value;
        private int _count;

        public enum Sources
        {
            Value = 0,
            Time = 1,
            Date = 2,
            TimeDate = 3
        }

        public static readonly DependencyProperty ForegroundProperty =
        DependencyProperty.Register("Foreground", typeof(Brush),
        typeof(Matrix), null);

        public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source", typeof(Sources),
        typeof(Matrix), new PropertyMetadata(Sources.Time));

        public static readonly DependencyProperty SizeProperty =
        DependencyProperty.Register("Size", typeof(UIElement),
        typeof(Matrix), new PropertyMetadata(4));

        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        public Sources Source
        {
            get { return (Sources)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public int Size
        {
            get { return (int)GetValue(SizeProperty); }
            set { SetValue(SizeProperty, value); }
        }

        private Rectangle AddElement(string name, int left, int top)
        {
            Rectangle element = new Rectangle()
            {
                Tag = name,
                Height = Size,
                Width = Size,
                Fill = Foreground,
                RadiusX = 1,
                RadiusY = 1,
                Opacity = 0,
                Margin = new Thickness(2)
            };
            element.SetBinding(Rectangle.FillProperty, new Binding()
            {
                Source = this,
                Path = new PropertyPath("Foreground"),
                Mode = BindingMode.TwoWay
            });
            element.SetValue(Canvas.LeftProperty, left);
            element.SetValue(Canvas.TopProperty, top);
            return element;
        }

        private void AddSection(string name)
        {
            Canvas layout = new Canvas()
            {
                Tag = name,
                Height = rows * Size,
                Width = columns * Size
            };
            int x = 0;
            int y = 0;
            int index = 0;
            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    layout.Children.Add(AddElement($"{name}.{index}", x, y));
                    x = (x + Size + padding);
                    index++;
                }
                x = 0;
                y = (y + Size + padding);
            }
            this.Children.Add(layout);
        }

        private Rectangle SetElement(Canvas layout, string name)
        {
            return layout.Children.Cast<Rectangle>().FirstOrDefault(f => (string)f.Tag == name);
        }

        private Canvas SetSection(string name)
        {
            return this.Children.Cast<Canvas>().FirstOrDefault(f => (string)f.Tag == name);
        }

        private void SetLayout(string name, char glyph)
        {
            Canvas layout = SetSection(name);
            int pos = glyphs.IndexOf(glyph);
            byte[] values = table[pos];
            for (int index = 0; index < layout.Children.Count; index++)
            {
                SetElement(layout, $"{name}.{index}").Opacity = values[index];
            }
        }

        private void GetLayout()
        {
            char[] array = _value.ToCharArray();
            int length = array.Length;
            List<int> list = Enumerable.Range(0, length).ToList();
            if (_count != length)
            {
                this.Children.Clear();
                foreach (int item in list)
                {
                    AddSection(item.ToString());
                }
                _count = length;
            }
            foreach (int item in list)
            {
                SetLayout(item.ToString(), array[item]);
            }
        }

        public Matrix()
        {
            this.Spacing = 0;
            this.Orientation = Orientation.Horizontal;
            DispatcherTimer timer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(250)
            };
            timer.Tick += (object s, object args) =>
            {
                if (Source != Sources.Value)
                {
                    string format = string.Empty;
                    switch (Source)
                    {
                        case Sources.Time:
                            format = "HH:mm:ss";
                            break;
                        case Sources.Date:
                            format = "dd/MM/yyyy";
                            break;
                        case Sources.TimeDate:
                            format = "HH:mm:ss dd/MM/yyyy";
                            break;
                    }
                    Value = DateTime.Now.ToString(format);
                    
                }
            };
            timer.Start();
        }

        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                GetLayout();
            }
        }
    }
}

In the Matrix Class there’s a byte[][] which represents the positions of elements to make up each Symbol or glyph for each number between 0 and 9 plus : and / – where 1 represents an element which will be visible and 0 represents an element which will not be visible. There’s a list of char to represent all the characters supported by the table of symbols, there’s values for the “columns”, “rows” of a Matrix element. There are DependencyProperty and Properties for Foreground, Source and Size.

The is an AddElement Method which will create part of a Section of the Display as a Rectangle and used AddSection to create a Section itself and position the Elements, SetElement is used to toggle the Opacity of a Rectangle Element and SetSection to do the same for a Section of the Display. SetLayout is used to update the Display itself with the Value passed in or set with Source and GetLayout is used to create the look-and-feel of the Control and the Matrix Constructor sets up the Source of the Control for Time, Date and TimeDate and there’s a Value Property.

Step 8

Once done select from the Menu, Build, then Build Solution

vs2017-build-build-solution

Step 9

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

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>
	<local:Matrix Margin="50" Name="Display" Source="TimeDate" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{ThemeResource AccentButtonBackground}"/>
</Viewbox>

The MainPage has the Matrix Control itself with the Source set to TimeDate and will also work in the Designer

Step 12

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

vs2017-local-machine

Step 13

After the Application has started running it should then appear displaying a Matrix Display that will show the current Time and Date.

ran-matrix-control

Step 14

To Exit the Application select the Close button in the top right of the Application

vs2017-close

This example creates a grid-like display to be able to show more than just the Time but the Date as well abut also could be extended to show even more characters if needed. In order to create the glyphs or Symbols used in the example their layouts needed to be designed for the characters 0 – 9 plus : and /.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s