Skip to content

1iveowl/Plugin.SegmentedControl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugin Segmented Control for Xamarin Forms and .NET Standard

NuGet Badge

Please star this project if you find it useful. Thank you!

Update - Version 5.2+

Version 5.2 and forward migrated to AndroidX for Android.

Update - Version 5.4.5+ (PR: #101)

ItemsSource is now of type IEnumerable instead of IList. It can contains full objects. When it contains a list of full object, the text displayed in a segment is read from a property on each object: define this property using TextPropertyName.

TextPropertyName: string, name of a property in an ItemsSource[i] object. If TextPropertyName is set, the displayed text is read from this property on each source object. If TextPropertyName is not set, ItemsSource must be a list of string.

New property: SelectedItem SelectedItem = Items[SelectedSegment] SelectedItem contains the current selected object from ItemsSource.

Fix:

Remove the extra padding below the control on Android, caused by the wrong usage of match_parent.

Why this library?

There are other Segmented Control libraries out there. This library adds two important capabilities:

  • It works across all four key platforms: iOS, Android, macOS, and UWP - all other libraries I've encounted lack UWP and/or macOS.
  • It's based on .NET Standard 2.0

Furthermore, this library is has more flexibility and features than other libraries that I'm aware of.

Enjoy! And please don't forget to star this project if you find it useful and/or provide feedback if you run into issues or shortcomings.

Supported platforms

Platform Supported Version Renderer
Xamarin.iOS Unified Yes iOS 8.1+ UISegmentedControl
Xamarin.Android Yes API 21+ RadioGroup
Xamarin.UWP Yes Win10 16299+ User Control/RadioButton
Xamarin.MacOS Partial 10.0+ NSSegmentedControl

Features

  • Bindable Tint color
  • Bindable Select color
  • Bindable Text color
  • Bindable Disabled color
  • Bindable Font size
  • Bindable Font Family
  • Bindable Item Text
  • Bindable Selected Item
  • Bindable ICommand
  • Bindable IsEnabled Item
  • Bindable ItemsSource
  • Bindable Border color (Android & iOS only)
  • Bindable Border width (Android & iOS only)

For more details please see below or for even more details see: Test/Demo App

How to use

Using this plugin is easy.

iOS

Add initializer to AppDelegate

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

    Plugin.Segmented.Control.iOS.SegmentedControlRenderer.Initialize();
    ...
}

UWP

You need to add the assembly to App.xaml.cs in you project. For more details see the Xamarin documentation here.

var assembliesToInclude = new List<Assembly> {typeof(Plugin.Segmented.Control.UWP.SegmentedControlRenderer).GetTypeInfo().Assembly};

Xamarin.Forms.Forms.Init(e, assembliesToInclude);

Android

No special needs.

For using custom fonts with Android see this blog post: https://blog.verslu.is/xamarin/xamarin-forms-xamarin/custom-fonts-with-xamarin-forms-revisited/

.NET Standard

The Xamarin Forms must use .NET Standard. I suggest using .NET Standard 2.0+.

Here is a great blog post about how to move your PCL to .NET Standard: Building Xamarin.Forms Apps with .NET Standard

XAML

Plugin Segmented Control Picture

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Test.SegmentedControl"
             xmlns:control="clr-namespace:Plugin.Segmented.Control;assembly=Plugin.Segmented"
             x:Class="Test.SegmentedControl.MainPage">

    <ContentPage.Resources>
        <OnPlatform x:Key="PlatformFontName" x:TypeArguments="x:String">
            <On Platform="UWP" Value="Courier New"></On>
            <On Platform="Android" Value="Serif"></On>
            <On Platform="iOS" Value="Helvetica"></On>
            <On Platform="macOS" Value="Baskerville"></On>
        </OnPlatform>
    </ContentPage.Resources>
    
    <ContentPage.Content>
        <StackLayout BackgroundColor="White" x:Name="SegmentWithStack">
            <Label 
                Text="Welcome to Xamarin.Forms!"
                HorizontalOptions="CenterAndExpand" />
            <control:SegmentedControl 
                x:Name="SegmentedControl" 
                SelectedSegment="{Binding SelectedSegment, Mode=TwoWay}"
                TintColor="BlueViolet"
                SelectedTextColor="White"
                DisabledColor="Gray"
                BorderColor="Black"
                BorderWidth="2.0"
                FontSize="Small"
                FontFamily="{StaticResource PlatformFontName}"
                Margin="8,8,8,8"
                SegmentSelectedCommand="{Binding SegmentChangedCommand}"
                OnElementChildrenChanging="OnElementChildrenChanging"
                ItemsSource="{Binding SegmentStringSource}">
                <!--<control:SegmentedControl.Children>
                    <control:SegmentedControlOption Text="{Binding ChangeText}"/>
                    <control:SegmentedControlOption Text="Item 2"/>
                    <control:SegmentedControlOption Text="Item 3"/>
                    <control:SegmentedControlOption Text="Item 4"/>
                </control:SegmentedControl.Children>-->
            </control:SegmentedControl>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

You can bind to the SegmentSelectedCommand for notification in your view model when a segment change has occurred.

<control:SegmentedControl
    SegmentSelectedCommand="{Binding SegmentChangedCommand}"
</control:SegmentedControl>   

Credits

For inspiration and for the Android and iOS part I'd like to thank Alex Rainman for his great work on SegmentedControl.FormsPlugin.

Thank you to rjantz3 for adding much requested features and enhancements. Thank you to Thomas Kälin for critical Android and iOS fixes and improvements.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published