【问题标题】:Xamarin Forms custom stepperXamarin Forms 自定义步进器
【发布时间】:2019-04-06 11:09:35
【问题描述】:

我正在尝试制作一个自定义步进器以在我的列表视图中使用,例如这个

知道怎么做吗?谢谢。

【问题讨论】:

  • 你已经拥有了什么?这个截图是你的吗?究竟是什么问题?
  • 这是一个自定义堆栈布局,中间有 2 个按钮和 1 个条目,问题是我需要在更改时获取此条目的值。例如,在常规步进器中,我可以轻松使用 ValueChanged 方法并处理值。
  • 如果是自定义控件,则需要添加自己的事件,以便页面可以订阅那些...
  • 你到底卡在哪里了?发布您当前的实施。
  • 我的实现是“York Shen”的答案我有相同的自定义类,我一直在获取条目的值以在我的代码中使用它。我在上面的评论中举了一个例子,在常规步进器中你可以很容易地使用“ValueChanged”,但在这里我不知道我该怎么做。

标签: xamarin.forms custom-renderer stepper


【解决方案1】:

解决方案 1:

Stepper 允许输入限制在某个范围内的离散值。您可以使用标签中的数据绑定显示Stepper 的值,如下所示:

定义在XAML:

<StackLayout x:Name="Container">
    <Label BindingContext="{x:Reference stepper}" Text="{Binding Value}" />
    <Stepper Minimum="0" Maximum="10" x:Name="stepper" Increment="0.5" />
</StackLayout>

解决方案 2:

你可以创建一个BindableProperty来实现这个功能,例如:

public class CustomStepper : StackLayout
{
    Button PlusBtn;
    Button MinusBtn;
    Entry Entry;

    public static readonly BindableProperty TextProperty =
      BindableProperty.Create(
         propertyName: "Text",
          returnType: typeof(int),
          declaringType: typeof(CustomStepper),
          defaultValue: 1,
          defaultBindingMode: BindingMode.TwoWay);

    public int Text
    {
        get { return (int)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public CustomStepper()
    {
        PlusBtn = new Button { Text = "+", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 15 };
        MinusBtn = new Button { Text = "-", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 15 };
        switch (Device.RuntimePlatform)
        {

            case Device.UWP:
            case Device.Android:
                {
                    PlusBtn.BackgroundColor = Color.Transparent;
                    MinusBtn.BackgroundColor = Color.Transparent;
                    break;
                }
            case Device.iOS:
                {
                    PlusBtn.BackgroundColor = Color.DarkGray;
                    MinusBtn.BackgroundColor = Color.DarkGray;
                    break;
                }
        }

        Orientation = StackOrientation.Horizontal;
        PlusBtn.Clicked += PlusBtn_Clicked;
        MinusBtn.Clicked += MinusBtn_Clicked;
        Entry = new Entry
        {
            PlaceholderColor = Color.Gray,
            Keyboard = Keyboard.Numeric,
            WidthRequest = 40, BackgroundColor = Color.FromHex("#3FFF")
        };
        Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
        Entry.TextChanged += Entry_TextChanged;
        Children.Add(PlusBtn);
        Children.Add(Entry);
        Children.Add(MinusBtn);
    }

    private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.NewTextValue))
            this.Text = int.Parse(e.NewTextValue);
    }

    private void MinusBtn_Clicked(object sender, EventArgs e)
    {
        if (Text > 1)
            Text--;
    }

    private void PlusBtn_Clicked(object sender, EventArgs e)
    {
        Text++;
    }

}

更多详细信息,请参考以下文档:

更新:

CustomStepper 类中,Entry 值与Text 属性绑定,因此您可以通过customStepper.Text 获取条目的值。

例如:

<local:CustomStepper x:Name="MyCustomerStepper"/>

您可以通过以下方式在您的xaml.cs 文件中获取其Entry 值:

var yourCustomerStepperEntryValue = MyCustomerStepper.Text.ToString();

【讨论】:

  • 这正是我正在做的,我需要获取条目的这个值才能在 XAML.cs 类中使用它。
  • 你有什么解决办法吗?
  • @mohammadanouti,您可以通过 var yourStepperValue = stepper.Value; 获得它。
  • 我非常了解自定义渲染以及如何在 XAML 等中使用它们,但问题是我不知道如何从头开始实现它,所以请你能分享一些代码如何在 XAML 中获取我创建的自定义 Stacklayout 的条目,以便在 Xaml.cs 文件中使用它
  • @mohammadanouti,我已经更新了我的答案,请检查一下。
猜你喜欢
  • 1970-01-01
  • 2019-08-31
  • 2018-07-29
  • 1970-01-01
  • 2016-09-03
  • 2018-07-23
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多