【问题标题】:How to add content view in segment control in xamarin forms如何在 xamarin 表单的段控制中添加内容视图
【发布时间】:2018-12-06 14:32:44
【问题描述】:

我必须在段控制中实现内容视图。这是我必须实现的 UI

如您所见,有两个内容视图,即供应商名称和产品/服务。我关注 this example 并在 iOS 中实现了它,但在 android 中,它只是一个空白应用程序。这是我的 XAML 代码。

  <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:SegmentedApp" x:Class="SegmentedApp.SegmentedAppPage" >

    </ContentPage>

This is the code behind
 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;
        }
    }

这是我的观点1

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

我没有为此找到自定义渲染器。我不知道我应该如何为这个段控件实现自定义渲染器。这是我项目的link

【问题讨论】:

  • 如果可能,请提供您的项目链接。
  • 是的,我在最后提到了项目链接。
  • 但这并没有打开。删除 bin 和 obj 文件夹后提供以减小大小。
  • @CGPA6.4 我已经放了最新的链接。我从 bin 和 obj 文件夹中删除了该文件。

标签: xamarin xamarin.forms uisegmentedcontrol


【解决方案1】:

您关注的帖子关注的是 iOS 而不是安卓。从自定义渲染中,它可能会在 android 和 iOS 上显示与上面相同的单选按钮。要设置custom renderer,您需要编写大量代码,包括布局、样式和渲染器类。

为了减少我们的工作,我们可以使用SegmentedControl.FormsPlugin。只需在解决方案级别从 Nuget 包管理器安装它并编写以下代码。

SegmentAppPage.xaml 文件

<?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>

上面我们已经通过使用插件的namespace 声明了control。您可以根据需要添加controls。在我们的例子中,我们需要两个按钮。

SegmentAppPage.xaml.cs 文件

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;
        }
    }
}

输出画面:

【讨论】:

  • 所以@CGPA6.4 不需要自定义渲染器?
  • 不用自定义渲染器也没问题。非常感谢@CGPA6.4 你太棒了。兄弟拳头!!!!
  • @user3452 - 快乐编码?‍?
【解决方案2】:

问题原因

您目前还没有为 android 平台实现自定义渲染器,因此不会看到相同的分段控件和分段控件的子级渲染。

问题的解决方案

在我之前开发的应用程序中,我遇到了类似的问题,并使用以下插件解决了 Android 和 iOS 上的问题:

https://github.com/alexrainman/SegmentedControl

如果您在项目中安装 alexrainman 的 Nuget 包 SegmentedControl 并按照他的文档进行操作,您将可以使用它。

这减少了您在两个平台上实现自己的自定义渲染器的需要。

请注意:

您的 SegControl_ValueChanged 方法将更改为 Handle_ValueChanged

您应该删除当前的 iOS 平台自定义渲染器,以免造成混淆

【讨论】:

  • android 自定义渲染器对我有用吗,因为它会在自定义渲染器代码中显示单选按钮的选择。
  • 我需要实现一个自定义渲染器,它将两个内容视图显示为标签页。
  • 是的,当然,它适用于您当前的目标,因为您目前只有一个带有 seg 控件和第二行视图的网格。 SegControl_ValueChanged 方法只是将与刚刚激活的段相关的视图交换到视图中。我上面引用的包包含所有这些功能:)
  • 我已经添加了自定义渲染器,但它仍然显示空白应用程序。
  • 移除自定义渲染器并安装 Nuget 包。此包中的自定义渲染器很可能与顶层的表单代码冲突,这就是您看到空白页的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多