How to add content view in segment control in xamarin forms

随声附和 提交于 2019-12-06 15:25:43

The post you are following is focusing for iOS not for android. From custom render it probably going to show you radio button on android & for iOS same as above. To set up custom renderer you need to write up huge code including layout, styles & renderer class.

To reduce our work we can use SegmentedControl.FormsPlugin. Just install it from Nuget package manager on solution level & write below code.

SegmentAppPage.xaml file

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:controls="clr-namespace:SegmentedControl.FormsPlugin.Abstractions;assembly=SegmentedControl.FormsPlugin.Abstractions"
             x:Class="SegmentedApp.SegmentedAppPage" >

    <StackLayout x:Name="segContainer"
                 Padding="12"
                 Spacing="12">
        <controls:SegmentedControl BackgroundColor="White" SelectedTextColor="White" TintColor="#007AFF" x:Name="SegControl" ValueChanged="Handle_ValueChanged">
            <controls:SegmentedControl.Children>
                <controls:SegmentedControlOption  Text="Vender Name" />
                <controls:SegmentedControlOption Text="Product/Service" />
            </controls:SegmentedControl.Children>
        </controls:SegmentedControl>
        <StackLayout x:Name="SegContent" />
    </StackLayout>
</ContentPage>

Above we have declared control by using namespace of plugin. You can add controls as many children you need. In our case we need two buttons.

SegmentAppPage.xaml.cs file

public partial class SegmentedAppPage : ContentPage
{
    View1 view1 = new View1();
    View2 view2 = new View2();
    public SegmentedAppPage()
    {
        InitializeComponent();
    }
    void Handle_ValueChanged(object sender, SegmentedControl.FormsPlugin.Abstractions.ValueChangedEventArgs e)
    {
        switch (e.NewValue)
        {
            case 0:
                SegContent.Children.Clear();
                SegContent.Children.Add(view1);
                break;
            case 1:
                SegContent.Children.Clear();
                SegContent.Children.Add(view2);
                break;
        }
    }
}

Output screen:

Reason for Problem

You currently have not implemented a custom renderer for the android platform and therefor will not see the same segmented controls and children of the segmented controls render.

A Solution to your Problem

In a previous app I developed I came across a similar problem and used the following plugin to solve the issue on both Android and iOS:

https://github.com/alexrainman/SegmentedControl

If you install the Nuget package SegmentedControl by alexrainman in your project and follow his documentation you will get it to work.

This cuts out the need for you to implement your own custom renderers on both platforms.

Please note:

your SegControl_ValueChanged method will change to Handle_ValueChanged

You should remove your current custom renderers for the iOS platform as not to cause confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!