Universal Windows Platform – Segment Control

Segment Control demonstrates how to create a seven-segment display 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 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 SegmentControl and select a Location and then select Ok to create the Project
vs2017-new-project-segment-control

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) 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 XAML from Installed then select Templated Control from the list, then type in the Name as Segment.cs before selecting Add to add the file to the Project

vs2017-add-new-item-segment-control

Step 7

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

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

namespace SegmentControl
{
    public class Segment : StackPanel
    {
        private readonly byte[][] table =
        {
            // a, b, c, d, e, f, g
            new byte[] { 1, 1, 1, 1, 1, 1, 0 }, // 0
            new byte[] { 0, 1, 1, 0, 0, 0, 0 }, // 1
            new byte[] { 1, 1, 0, 1, 1, 0, 1 }, // 2
            new byte[] { 1, 1, 1, 1, 0, 0, 1 }, // 3
            new byte[] { 0, 1, 1, 0, 0, 1, 1 }, // 4
            new byte[] { 1, 0, 1, 1, 0, 1, 1 }, // 5
            new byte[] { 1, 0, 1, 1, 1, 1, 1 }, // 6
            new byte[] { 1, 1, 1, 0, 0, 0, 0 }, // 7
            new byte[] { 1, 1, 1, 1, 1, 1, 1 }, // 8
            new byte[] { 1, 1, 1, 0, 0, 1, 1 }, // 9
            new byte[] { 0, 0, 0, 0, 0, 0, 0 }, // None
            new byte[] { 0, 0, 0, 0, 0, 0, 0 }, // Colon
        };
        private const int width = 5;
        private const int height = 25;

        private string _value;
        private int _count;
        private DispatcherTimer _timer;

        public enum Sources
        {
            Value = 0,
            Time = 1
        }

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

        public static readonly DependencyProperty ForegroundProperty =
        DependencyProperty.Register("Foreground", typeof(Brush),
        typeof(Segment), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

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

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

        private Rectangle AddElement(string name, int left, int top, int width, int height)
        {
            Rectangle rect = new Rectangle()
            {
                Tag = name,
                Width = width,
                Height = height,
                RadiusX = 2,
                RadiusY = 2,
                Fill = Foreground
            };
            Canvas.SetLeft(rect, left);
            Canvas.SetTop(rect, top);
            return rect;
        }

        private void AddSegment(string name)
        {
            Canvas segment = new Canvas()
            {
                Margin = new Thickness(2),
                Tag = name,
                Height = 50,
                Width = 25
            };
            segment.Children.Add(AddElement($"{name}.a", width, 0, height, width));
            segment.Children.Add(AddElement($"{name}.h", width + width + width, width + width + width, width, width));
            segment.Children.Add(AddElement($"{name}.f", 0, width, width, height));
            segment.Children.Add(AddElement($"{name}.b", height + width, width, width, height));
            segment.Children.Add(AddElement($"{name}.g", width, height + width, height, width));
            segment.Children.Add(AddElement($"{name}.e", 0, height + width + width, width, height));
            segment.Children.Add(AddElement($"{name}.c", height + width, height + width + width, width, height));
            segment.Children.Add(AddElement($"{name}.i", width + width + width, height + width + width + width + width, width, width));
            segment.Children.Add(AddElement($"{name}.d", width, height + height + width + width, height, width));
            this.Children.Add(segment);
        }

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

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

        private void SetSegment(string name, int digit)
        {
            Canvas layout = SetLayout(name);
            byte[] values = table[digit];
            SetElement(layout, $"{name}.a").Opacity = values[0];
            SetElement(layout, $"{name}.b").Opacity = values[1];
            SetElement(layout, $"{name}.c").Opacity = values[2];
            SetElement(layout, $"{name}.d").Opacity = values[3];
            SetElement(layout, $"{name}.e").Opacity = values[4];
            SetElement(layout, $"{name}.f").Opacity = values[5];
            SetElement(layout, $"{name}.g").Opacity = values[6];
            SetElement(layout, $"{name}.h").Opacity = digit > 10 ? 1 : 0;
            SetElement(layout, $"{name}.i").Opacity = digit > 10 ? 1 : 0;
        }

        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)
                {
                    AddSegment(item.ToString());
                }
                _count = length;
            }
            foreach (int item in list)
            {
                string val = array[item].ToString();
                int digit = val == ":" ? 11 : int.Parse(val);
                SetSegment(item.ToString(), digit);
            }
        }

        public Segment()
        {
            this.Spacing = 10;
            this.Orientation = Orientation.Horizontal;
            if (Source == Sources.Time)
            {
                _timer = new DispatcherTimer()
                {
                    Interval = TimeSpan.FromMilliseconds(250)
                };
                _timer.Tick += (object sender, object e) =>
                {
                    string time = DateTime.Now.ToString("HH:mm:ss");
                    this.Value = time;
                };
                _timer.Start();
            }
        }

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

In the Segment.cs there are using statements to include the necessary functionality. There is a byte[][] to represent the parts of the segment which will be On or Off based on a particular Value, there is a DispatcherTimer to help with displaying a Time and there are Properties for the Sources of what to show and the Value to show itself. AddElement is used to add part of one of the Segments made up from Rectangle Controls and AddSegment is used to add the Segments themselves which are made up of Canvas Controls. SetLayout and SetElement are used by SetSegment to toggle the Opacity of the Rectangle Controls to display the different types of item supported. GetLayout is used to create the look-and-feel of the Control and the Constructor Segment is used to setup the DispatcherTimer if the Sources option is Time and finally there is the string Property for the Value.

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

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:Segment Margin="50" Source="Time" Foreground="{ThemeResource SystemControlHighlightAccentBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Viewbox>

The block of XAML represents the Segment Control set to show the current Time within a Viewbox.

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 the Application should then appear showing the current Time using the Segment Control in the Application

ran-segment-control

Step 14

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

vs2017-close

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