Windows 10 – Universal Windows Platform – Radial Control

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

2015-visual-studio

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

2015-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in the Name as RadialControl and select a Location to save to before selecting Ok to create the Project.

2015-radial-control-add-new-project

Step 4

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

2015-project-add-new-item

Step 5

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

2015-radial-control-panel-add

Step 6

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

using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace RadialControl
{
    public partial class RadialPanel : Panel
    {
        private bool ignorePropertyChange;

        private static void OnIsOrientedPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            RadialPanel source = (RadialPanel)d;
            bool value = (bool)e.NewValue;
            if (source.ignorePropertyChange)
            {
                source.ignorePropertyChange = false;
                return;
            }
            source.InvalidateMeasure();
        }

        private static void OnItemHeightOrWidthPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            RadialPanel source = (RadialPanel)d;
            double value = (double)e.NewValue;
            if (source.ignorePropertyChange)
            {
                source.ignorePropertyChange = false;
                return;
            }
            if (!double.IsNaN(value) && ((value <= 0.0) || double.IsPositiveInfinity(value)))
            {
                source.ignorePropertyChange = true;
                source.SetValue(e.Property, (double)e.OldValue);
                throw new ArgumentException("OnItemHeightOrWidthPropertyChanged InvalidValue", "value");
            }
            source.InvalidateMeasure();
        }

        public double ItemHeight
        {
            get { return (double)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        public static readonly DependencyProperty ItemHeightProperty =
            DependencyProperty.Register("ItemHeight", typeof(double), typeof(RadialPanel),
                new PropertyMetadata(double.NaN, OnItemHeightOrWidthPropertyChanged));

        public double ItemWidth
        {
            get { return (double)GetValue(ItemWidthProperty); }
            set { SetValue(ItemWidthProperty, value); }
        }

        public static readonly DependencyProperty ItemWidthProperty =
            DependencyProperty.Register("ItemWidth", typeof(double), typeof(RadialPanel),
                new PropertyMetadata(double.NaN, OnItemHeightOrWidthPropertyChanged));

        public bool IsOriented
        {
            get { return (bool)GetValue(IsOrientedProperty); }
            set { SetValue(IsOrientedProperty, value); }
        }

        public static readonly DependencyProperty IsOrientedProperty =
            DependencyProperty.Register("IsOriented", typeof(bool), typeof(RadialPanel),
                new PropertyMetadata(false, OnIsOrientedPropertyChanged));

        protected override Size MeasureOverride(Size constraint)
        {
            double itemWidth = ItemWidth;
            double itemHeight = ItemHeight;
            bool hasFixedWidth = !double.IsNaN(itemWidth);
            bool hasFixedHeight = !double.IsNaN(itemHeight);
            Size itemSize = new Size(
                hasFixedWidth ? itemWidth : constraint.Width,
                hasFixedHeight ? itemHeight : constraint.Height);
            foreach (UIElement element in Children)
            {
                element.Measure(itemSize);
            }
            return itemSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double itemWidth = ItemWidth;
            double itemHeight = ItemHeight;
            bool hasFixedWidth = !double.IsNaN(itemWidth);
            bool hasFixedHeight = !double.IsNaN(itemHeight);
            double radiusX = finalSize.Width * 0.5;
            double radiusY = finalSize.Height * 0.5;
            double count = Children.Count;
            double deltaAngle = 2 * Math.PI / count;
            Point centre = new Point(finalSize.Width / 2,
                finalSize.Height / 2);
            for (int i = 0; i < count; i++)
            {
                UIElement element = Children[i];
                Size elementSize = new Size(
                hasFixedWidth ? itemWidth : element.DesiredSize.Width,
                hasFixedHeight ? itemHeight : element.DesiredSize.Height);
                double angle = i * deltaAngle;
                double x = centre.X + radiusX * Math.Cos(angle)
                    - elementSize.Width / 2;
                double y = centre.Y + radiusY * Math.Sin(angle)
                    - elementSize.Height / 2;
                if (IsOriented)
                {
                    element.RenderTransform = null;
                }
                else
                {
                    element.RenderTransformOrigin = new Point(0.5, 0.5);
                    element.RenderTransform = new RotateTransform()
                    {
                        Angle = angle * 180 / Math.PI
                    };
                }
                element.Arrange(new Rect(x, y,
                    elementSize.Width, elementSize.Height));
            }
            return finalSize;
        }
    }
}

It should then appear as such:

2015-radial-control-panel-code

Step 7

Then select from the Menu, Build, then Build Solution

2015-build

Step 8

From the Solution Explorer select MainPage.xaml

2015-mainpage-library

Step 9

Select from the Menu, View then Designer

2015-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<local:RadialPanel Height="300" Width="300">
	<Rectangle Width="50" Height="50" Fill="Black"/>
	<Rectangle Width="50" Height="50" Fill="Gray"/>
	<Rectangle Width="50" Height="50" Fill="Red"/>
	<Rectangle Width="50" Height="50" Fill="Orange"/>
	<Rectangle Width="50" Height="50" Fill="Yellow"/>
	<Rectangle Width="50" Height="50" Fill="Green"/>
	<Rectangle Width="50" Height="50" Fill="Cyan"/>
	<Rectangle Width="50" Height="50" Fill="Blue"/>
	<Rectangle Width="50" Height="50" Fill="Magenta"/>
	<Rectangle Width="50" Height="50" Fill="Purple"/>
</local:RadialPanel>

It should appear as such:

2015-radial-control-mainpage-xaml

Step 11

That completes the Universal Windows Application so Save the Project then select the Debug and Simulator option to run the Application

2015-simulator

Step 12

Once the Simulator has started the Application should then appear displaying the Radial Control in the Application

2015-radial-control-simulator-run

Step 13

To Exit the application select Stop Debugging in Visual Studio Community 2015

2015-stop

Step 14

Another option is to run as a Windows 10 Mobile application, select Debug and select Emulator 10.0.10586.0 WVGA 4 inch 512MB option to run the Application

2015-emulator

Step 15

Once the Emulator has started the Application should then appear displaying the Radial Control in the Application

2015-radial-control-emulator-run

Step 16

To Exit the application select Stop Debugging in Visual Studio Community 2015

2015-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