Windows 10 – Universal Windows Platform – Custom Toggle

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 a Name and select a Location to save to before selecting Ok to create the Project.

2015-new-project

Step 4

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

2015-project-add-new-item

Step 5

From the Solution Explorer select App.xaml

2015-app

Step 6

Select from the Menu, View then Designer

2015-view-designer

Step 7

The XAML View will be displayed and in this above </Application> enter the following XAML:

<Application.Resources>
	<Style x:Key="CustomToggle" TargetType="ToggleSwitch">
		<Setter Property="Background" Value="LightSalmon"/>
		<Setter Property="BorderBrush" Value="Salmon"/>
		<Setter Property="Foreground" Value="Gold"/>
		<Setter Property="ManipulationMode" Value="System,TranslateX"/>
		<Setter Property="UseSystemFocusVisuals" Value="True"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="ToggleSwitch">
					<Grid HorizontalAlignment="Center">
						<VisualStateManager.VisualStateGroups>
							<VisualStateGroup x:Name="ToggleStates">
								<VisualStateGroup.Transitions>
									<VisualTransition x:Name="DraggingToOnTransition" From="Dragging" GeneratedDuration="0" To="On">
										<Storyboard>
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
												TemplateSettings.KnobCurrentToOnOffset, 
												RelativeSource={RelativeSource Mode=TemplatedParent}}" 
												TargetName="SwitchKnob"/>
										</Storyboard>
									</VisualTransition>
									<VisualTransition x:Name="DraggingToOffTransition" From="Dragging" GeneratedDuration="0" To="Off">
										<Storyboard>
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
												TemplateSettings.KnobCurrentToOffOffset, 
												RelativeSource={RelativeSource Mode=TemplatedParent}}" 
												TargetName="SwitchKnob"/>
										</Storyboard>
									</VisualTransition>
									<VisualTransition x:Name="OnToOffTransition" From="On" GeneratedDuration="0" To="Off">
										<Storyboard>
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
												TemplateSettings.KnobOnToOffOffset, 
												RelativeSource={RelativeSource Mode=TemplatedParent}}" 
												TargetName="SwitchKnob"/>
										</Storyboard>
									</VisualTransition>
									<VisualTransition x:Name="OffToOnTransition" From="Off" GeneratedDuration="0" To="On">
										<Storyboard>
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
												TemplateSettings.KnobOffToOnOffset, 
												RelativeSource={RelativeSource Mode=TemplatedParent}}" 
												TargetName="SwitchKnob"/>
										</Storyboard>
									</VisualTransition>
								</VisualStateGroup.Transitions>
								<VisualState x:Name="Dragging"/>
								<VisualState x:Name="Off"/>
								<VisualState x:Name="On">
									<Storyboard>
										<DoubleAnimation Duration="0" To="24" Storyboard.TargetProperty="X" 
											Storyboard.TargetName="KnobTranslateTransform"/>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" 
											Storyboard.TargetName="SwitchKnobBounds">
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
										</ObjectAnimationUsingKeyFrames>
										<ObjectAnimationUsingKeyFrames 
											Storyboard.TargetProperty="Opacity" 
											Storyboard.TargetName="SwitchKnobOn">
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
										</ObjectAnimationUsingKeyFrames>
										<ObjectAnimationUsingKeyFrames 
											Storyboard.TargetProperty="Opacity" 
											Storyboard.TargetName="SwitchKnobOff">
											<DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
										</ObjectAnimationUsingKeyFrames>
									</Storyboard>
								</VisualState>
							</VisualStateGroup>
						</VisualStateManager.VisualStateGroups>
						<Rectangle x:Name="OuterBorder" Height="30" Width="55" RadiusY="15" RadiusX="15"  
							StrokeThickness="1" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}"/>
						<Rectangle x:Name="SwitchKnobBounds" Height="30" Width="55" RadiusY="15" RadiusX="15" 
							StrokeThickness="1" Stroke="Goldenrod" Fill="{TemplateBinding Foreground}" Opacity="0"/>
						<Grid x:Name="SwitchKnob" Grid.Row="2" HorizontalAlignment="Left" Height="25" Width="30">
							<Grid.RenderTransform>
								<TranslateTransform x:Name="KnobTranslateTransform"/>
							</Grid.RenderTransform>
							<Ellipse x:Name="SwitchKnobOn" Height="15" Width="15" 
								Fill="{TemplateBinding Background}" Opacity="0"/>
							<Ellipse x:Name="SwitchKnobOff" Height="15" Width="15" 
								Fill="{TemplateBinding Foreground}"/>
						</Grid>
						<Thumb x:Name="SwitchThumb" AutomationProperties.AccessibilityView="Raw">
							<Thumb.Template>
								<ControlTemplate TargetType="Thumb">
									<Rectangle Fill="Transparent"/>
								</ControlTemplate>
							</Thumb.Template>
						</Thumb>
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</Application.Resources>

It should appear as such:

2015-custom-toggle-app-xaml

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:

<ToggleSwitch VerticalAlignment="Center" HorizontalAlignment="Center" 
	Style="{StaticResource CustomToggle}"/>

It should appear as such:

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

2015-custom-toggle-simulator-run

Step 13

After the Application has started running you can tap the Toggle to switch between On or Off in the Application

2015-custom-toggle-simulator-ran

Step 14

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

2015-stop

Step 15

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 16

Once the Emulator has started the Application should then appear

2015-tailored-app-toggle-run

Step 17

After the Application has started running you can tap the Toggle to switch between On or Off in the Application

2015-custom-toggle-emulator-ran

Step 18

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