DBTech.Maui.SwitchCotrol 1.0.0

dotnet add package DBTech.Maui.SwitchCotrol --version 1.0.0
                    
NuGet\Install-Package DBTech.Maui.SwitchCotrol -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DBTech.Maui.SwitchCotrol" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DBTech.Maui.SwitchCotrol" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DBTech.Maui.SwitchCotrol" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DBTech.Maui.SwitchCotrol --version 1.0.0
                    
#r "nuget: DBTech.Maui.SwitchCotrol, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DBTech.Maui.SwitchCotrol@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DBTech.Maui.SwitchCotrol&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DBTech.Maui.SwitchCotrol&version=1.0.0
                    
Install as a Cake Tool

Maui SwitchControl

This is a Switch control that would allow you to set ThumbColor and OnColor based on your requirements.

How to use it?

  • Install the NuGet package into your project
  • Use it in your code where you want it, like this:

MainPage.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:ColorSwitch.Controls;assembly=ColorSwitch"
             x:Class="TestApp.Views.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="CustomLabelStyle" TargetType="Label">
                <Setter Property="TextColor" Value="Blue" />
                <Setter Property="FontSize" Value="16" />
                <Setter Property="HorizontalTextAlignment" Value="Center" />
                <Setter Property="VerticalTextAlignment" Value="Center" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout Spacing="15">

        <Grid ColumnDefinitions="*,Auto" >

            <Label Grid.Column="0" Text="I'm SwitchControl." VerticalOptions="Center" />

            <controls:SwitchControl
                Grid.Column="1"
                x:Name="customSwitchControl"
                IsToggled="{Binding IsSwitchToggled}"
                ThumbOnColor="{StaticResource Green}"
                ThumbOffColor="{StaticResource WhiteSmoke }"
                TrackOnColor="{StaticResource LightGreen}"
                TrackOffColor="{StaticResource Gray300}"
                HorizontalOptions="End"/>

        </Grid>

         <controls:SwitchControl
            x:Name="customSwitchWithLabel"
            Padding="5,0"
            LabelStyle="{StaticResource CustomLabelStyle}"
            IsToggled="{Binding IsSwitchToggled}"
            ThumbOffColor="{StaticResource WhiteSmoke}"
            ThumbOnColor="{StaticResource Green}"
            TrackOnColor="{StaticResource LightGreen}"
            TrackOffColor="{StaticResource LightPink}"
            HorizontalOptions="Start" >
            <controls:SwitchControl.Triggers>
                <DataTrigger TargetType="controls:SwitchControl"
                                Binding="{Binding IsToggled, Source={x:Reference customSwitchWithLabel}}"
                                Value="True">
                    <Setter Property="LabelText" Value="Switch is ON"/>
                </DataTrigger>
                <DataTrigger TargetType="controls:SwitchControl"
                                Binding="{Binding IsToggled, Source={x:Reference customSwitchWithLabel}}"
                                Value="False">
                    <Setter Property="LabelText" Value="Switch is OFF"/>
                </DataTrigger>
            </controls:SwitchControl.Triggers>
        </controls:SwitchControl>

    </VerticalStackLayout>
</ContentPage>

MainPageViewModel.cs

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel()
    {
    }

    private bool _isSwitchToggled { get; set; }
    public bool IsSwitchToggled
    {
        get { return _isSwitchToggled; }
        set { _isSwitchToggled = value; OnPropertyChanged(nameof(IsSwitchToggled)); }
    }
}

Results:

https://github.com/user-attachments/assets/be700561-4161-4b83-88d9-3b621a8760e2

https://github.com/user-attachments/assets/ea26e209-e046-4dee-92ea-7dc49b93f982

https://github.com/user-attachments/assets/89711564-385c-424e-bc5a-65c1765ceec3

Product Compatible and additional computed target framework versions.
.NET net8.0-android34.0 is compatible.  net8.0-ios17.2 is compatible.  net8.0-maccatalyst17.2 is compatible.  net9.0-android was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net10.0-android was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 510 8/15/2024