简体   繁体   English

如何在IOS Xamarin.Forms中将内容页面添加到细分控件

[英]How to Add Content Page to Segment Control in IOS Xamarin.Forms

I have used Segmented Control in my application. 我在应用程序中使用了分段控制。 I don't know how to add two content pages to Segment control like a tabbed page. 我不知道如何像选项卡式页面一样将两个内容页面添加到Segment控件中。 I have attached the sample file. 我已经附上了样本文件。 Please give any suggestion Link for Sample Application 请给任何建议链接示例应用程序

Sample Code: 样例代码:

public partial class SamplePage : ContentPage
{

    SegmentedControl segControl;
    SegmentedControlOption optionOne;
    SegmentedControlOption optionTwo;

    public SamplePage()
    {
        segControl = new SegmentedControl();
        optionOne = new SegmentedControlOption();
        optionTwo = new SegmentedControlOption();            

        optionOne.Text = "One";
        optionTwo.Text = "Two";                  

        segControl.Children.Add(optionOne);
        segControl.Children.Add(optionTwo);

        var stack = new StackLayout()
        {
            VerticalOptions = LayoutOptions.StartAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Children = { segControl }
        };

        this.Content = stack;

    }       
}

ScreenShot Attached 屏幕截图已附加

Just some suggestions and explanations. 只是一些建议和解释。

  1. We can't put a ContentPage inside another ContentPage 我们不能将ContentPage放在另一个ContentPage

    It's better to use ContentView instead of ContentPage 最好使用ContentView而不是ContentPage

  2. Grid is more recommended in this scenario , since it fills with the whole Screen. 在这种情况下,更推荐使用Grid ,因为它充满了整个屏幕。

  3. Use ValueChanged event to change the view dynamically. 使用ValueChanged事件可动态更改视图。

Code : 代码:

Page

public partial class SegmentedAppPage : ContentPage
{
    SegmentedControl segControl;      
    SegmentedControlOption scOptionOne;
    SegmentedControlOption scOptionTwo;

    Grid grid;

    View1 view1 = new View1();
    View2 view2 = new View2();

    public SegmentedAppPage()
    {
        InitializeComponent();

        segControl = new SegmentedControl();
        segControl.SelectedValue = "One";
        scOptionOne = new SegmentedControlOption();
        scOptionTwo = new SegmentedControlOption();            

        scOptionOne.Text = "One";
        scOptionTwo.Text = "Two";

        segControl.Children.Add(scOptionOne);
        segControl.Children.Add(scOptionTwo);

        grid = new Grid();
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

        grid.Children.Add(segControl, 0, 0);
        grid.Children.Add(view1, 0, 1);

        this.Content = grid;

        segControl.ValueChanged += SegControl_ValueChanged;
    }

    private void SegControl_ValueChanged(object sender, EventArgs e)
    {
        SegmentedControl control = sender as SegmentedControl;
        if(control.SelectedValue is "One")
        {
            grid.Children.Remove(view2);
            grid.Children.Add(view1,0,1);  //This line 
        }
        else if (control.SelectedValue is "Two")
        {
            grid.Children.Remove(view1);
            grid.Children.Add(view2, 0, 1); //This line 
        }
        this.Content = grid;
    }
}

ContentView 内容查看

public class View1 : ContentView
{
    public View1()
    {
        Content = new StackLayout
        {
            BackgroundColor = Color.Green,
            Children = {
                new Label { Text = "View1" }
            }
        };
    }
}

To set default value on segmentedControl , modify code in SegmentedControlRenderers 要在segmentedControl上设置默认值,请在SegmentedControlRenderers修改代码

protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
{
    base.OnElementChanged(e);

    var segmentedControl = new UISegmentedControl();

    for (var i = 0; i < e.NewElement.Children.Count; i++)
    {
        segmentedControl.InsertSegment(e.NewElement.Children[i].Text, i, false);
    }

    segmentedControl.ValueChanged += (sender, eventArgs) => {
        e.NewElement.SelectedValue = segmentedControl.TitleAt(segmentedControl.SelectedSegment);
    };
    segmentedControl.SelectedSegment = 0; // add this line
    SetNativeControl(segmentedControl);
}

Test 测试

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在IOS Xamarin.Forms中未设置细分控件宽度 - Segment Control Width is not setting in IOS Xamarin.Forms 如何在 xamarin 表单中的段控件中添加内容视图 - How to add content view in segment control in xamarin forms 如何在xamarin.forms的选项卡式页面内添加用户控件 - How to add a user control inside a tabbed page in xamarin.forms 如何将用户控件添加到Xamarin表单的内容页面? - How do I add a User control to Content Page in Xamarin Forms? 向Zxing条码Xaml内容页面Xamarin.forms添加元素 - Add elements to Zxing barcode Xaml content page, Xamarin.forms 如何在WebView Xamarin.Forms中加载带有javascript内容的HTML页面? - How to load HTML page with javascript content in a WebView Xamarin.Forms? 如何在Xamarin.Forms中添加iOS的Assets Catalog? - How to add Assets Catalog for iOS in Xamarin.Forms? Xamarin.Forms 如何在 Android 和 iOS 上添加应用评​​级? - Xamarin.Forms how to add app rating on Android and iOS? Xamarin.Forms:如何在 Android 和 Z9E304D4E8DF1B7248ABZ809 上获取和添加滑动事件? - Xamarin.Forms : How to get and add swipe events on Android and ios? 我们是否可以将Xamain.iOS自定义控件转换为Xamarin.Forms自定义控件? 如果是,那怎么办? - Are we able to Convert Xamain.iOS custom control to Xamarin.Forms Custom control? If yes then how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM