Windows 10 – Universal Windows Platform – Gauge 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 GaugeControl and select a Location to save to before selecting Ok to create the Project.

2015-gauge-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 XAML then select User Control from the list, then type in the Name as Gauge.xaml before selecting Add to add the file to the Project

2015-gauge-control-usercontrol-add

Step 6

The Design View will be displayed along with the XAML View and in this below <Grid> enter the following XAML:

<Viewbox>
	<Canvas Height="300" Width="300" Name="Display"/>
</Viewbox>

It should appear as such:

2015-gauge-control-usercontrol-xaml

Step 7

Select from the Menu, View then Code

2015-view-code

Step 8

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

private Brush foreground = new SolidColorBrush(Windows.UI.Colors.White);
private Brush background = ((Brush)App.Current.Resources["SystemControlBackgroundAccentBrush"]);
private Windows.UI.Xaml.Shapes.Rectangle needle;
private int needleWidth = 2;
private int needleLength = 0;
private double diameter = 0;
private int value = 0;
private int minimum = 0;
private int maximum = 100;

private TransformGroup transformGroup(double angle, double x, double y)
{
	TransformGroup transformGroup = new TransformGroup();
	TranslateTransform firstTranslate = new TranslateTransform()
	{
		X = x,
		Y = y
	};
	transformGroup.Children.Add(firstTranslate);
	RotateTransform rotateTransform = new RotateTransform()
	{
		Angle = angle
	};
	transformGroup.Children.Add(rotateTransform);
	TranslateTransform secondTranslate = new TranslateTransform()
	{
		X = diameter / 2,
		Y = diameter / 2
	};
	transformGroup.Children.Add(secondTranslate);
	return transformGroup;
}

private void indicator(int value)
{
	double percentage = (((double)value / (double)maximum) * 100);
	double position = (percentage / 2) + 5;
	needle.RenderTransform = transformGroup(position * 6,
	-needleWidth / 2, -needleLength + 4.25);
}

private void init(Canvas canvas)
{
	canvas.Children.Clear();
	diameter = canvas.Width;
	double inner = diameter;
	Windows.UI.Xaml.Shapes.Ellipse face = new Windows.UI.Xaml.Shapes.Ellipse()
	{
		Height = diameter,
		Width = diameter,
		Fill = background
	};
	canvas.Children.Add(face);
	Canvas markers = new Canvas()
	{
		Width = inner,
		Height = inner
	};
	for (int i = 0; i < 51; i++)
	{
		Windows.UI.Xaml.Shapes.Rectangle marker = new Windows.UI.Xaml.Shapes.Rectangle()
		{
			Fill = foreground
		};
		if ((i % 5) == 0)
		{
			marker.Width = 4;
			marker.Height = 16;
			marker.RenderTransform = transformGroup(i * 6, -(marker.Width / 2),
			-(marker.Height * 2 + 4.5 - face.StrokeThickness / 2 - inner / 2 - 16));
		}
		else
		{
			marker.Width = 2;
			marker.Height = 8;
			marker.RenderTransform = transformGroup(i * 6, -(marker.Width / 2),
			-(marker.Height * 2 + 12.75 - face.StrokeThickness / 2 - inner / 2 - 16));
		}
		markers.Children.Add(marker);
	}
	markers.RenderTransform = new RotateTransform()
	{
		Angle = 30,
		CenterX = diameter / 2,
		CenterY = diameter / 2
	};
	canvas.Children.Add(markers);
	needle = new Windows.UI.Xaml.Shapes.Rectangle()
	{
		Width = needleWidth,
		Height = (int)diameter / 2 - 30,
		Fill = foreground
	};
	canvas.Children.Add(needle);
	Windows.UI.Xaml.Shapes.Ellipse middle = new Windows.UI.Xaml.Shapes.Ellipse()
	{
		Height = diameter / 10,
		Width = diameter / 10,
		Fill = foreground
	};
	Canvas.SetLeft(middle, (diameter - middle.ActualWidth) / 2);
	Canvas.SetTop(middle, (diameter - middle.ActualHeight) / 2);
	canvas.Children.Add(middle);
}

public int Minimum { get { return minimum; } set { minimum = value; } }

public int Maximum { get { return maximum; } set { maximum = value; } }

public int Value
{
	get
	{
		return value;
	}
	set
	{
		if (value >= Minimum && value <= Maximum)
		{
			this.value = value;
			indicator(value);
		}
	}
}

While still in the Code View in the public Gauge() { … } below this.InitializeComponent(); the following should be entered:

init(Display);

It should then appear as such:

2015-gauge-control-usercontrol-code

Step 9

Then select from the Menu, Build, then Build Solution

2015-build

Step 10

From the Solution Explorer select MainPage.xaml

2015-mainpage-library

Step 11

Select from the Menu, View then Designer

2015-view-designer

Step 12

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

<local:Gauge Width="300" Value="25"/>

It should appear as such:

2015-gauge-control-mainpage-xaml

Step 13

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

2015-simulator

Step 14

Once the Simulator has started the Application should then appear displaying the Gauge Control with the set Value in the Application

2015-gauge-control-simulator-run

Step 15

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

2015-stop

Step 16

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 17

Once the Emulator has started the Application should then appear displaying the Gauge Control with the set Value in the Application

2015-gauge-control-emulator-run

Step 18

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

2015-stop

Creative Commons License

Leave a comment