Xamarin.forms Xamarin形成自定义步进器

Xamarin.forms Xamarin形成自定义步进器,xamarin.forms,custom-renderer,stepper,Xamarin.forms,Custom Renderer,Stepper,我正在尝试在我的listview中使用一个自定义的步进器,比如这个 你知道怎么做吗?谢谢。解决方案1: 步进器允许输入限制在某一范围内的离散值。您可以使用标签中的数据绑定显示步进器的值,如下所示: 在XAML中定义: <StackLayout x:Name="Container"> <Label BindingContext="{x:Reference stepper}" Text="{Binding Value}&q

我正在尝试在我的listview中使用一个自定义的步进器,比如这个

你知道怎么做吗?谢谢。

解决方案1: 步进器允许输入限制在某一范围内的离散值。您可以使用标签中的数据绑定显示步进器的值,如下所示:

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>
有关更多详细信息,请参阅以下文件:

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

例如:

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

}
<local:CustomStepper x:Name="MyCustomerStepper"/>
解决方案1: 步进器允许输入限制在某一范围内的离散值。您可以使用标签中的数据绑定显示步进器的值,如下所示:

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>
有关更多详细信息,请参阅以下文件:

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

例如:

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

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


你已经有什么了?这张截图是你的吗?确切的问题是什么?这是一个自定义的堆栈布局,中间有2个按钮和1个条目。问题是,当改变时,我需要得到这个条目的值。例如,在常规的步进程序中,我可以轻松地使用ValueChanged方法并处理值。如果它是自定义控件,则需要添加自己的事件,以便页面可以订阅这些事件…您到底在哪里?发布您当前的实现。我的实现是“York Shen”的答案。我有相同的自定义类,我一直在获取条目的值,以便在代码中使用它。我在上面的评论中给出了一个例子,在普通的步进机中,你可以很容易地使用“ValueChanged”,但在这里我不知道如何做到这一点。你已经有了什么?这张截图是你的吗?确切的问题是什么?这是一个自定义的堆栈布局,中间有2个按钮和1个条目。问题是,当改变时,我需要得到这个条目的值。例如,在常规的步进程序中,我可以轻松地使用ValueChanged方法并处理值。如果它是自定义控件,则需要添加自己的事件,以便页面可以订阅这些事件…您到底在哪里?发布您当前的实现。我的实现是“York Shen”的答案。我有相同的自定义类,我一直在获取条目的值,以便在代码中使用它。我在上面的评论中给出了一个例子,在普通的stepper中,你可以很容易地使用“ValueChanged”,但在这里我不知道如何做到这一点。这正是我正在做的,我需要得到条目的这个值才能在XAML.cs类中使用它。你有什么解决方案吗?@mohammadanouti,您可以通过使用
var yourspeppervalue=stepper.Value。我非常了解自定义渲染以及如何在XAML中使用它们等等,但问题是我不知道如何从头开始实现它,所以请与我分享一些代码,在XAML中如何获取我创建的自定义Stacklayout条目,以便在XAML.cs中使用它file@mohammadanouti,我已经更新了我的答案,请检查它。这正是我正在做的,我需要得到条目的这个值才能在XAML.cs类中使用它。你有什么解决方案吗?@mohammadanouti,你可以通过使用
var yourspepervalue=stepper.value。我非常了解自定义渲染以及如何在XAML中使用它们等等,但问题是我不知道如何从头开始实现它,所以请与我分享一些代码,在XAML中如何获取我创建的自定义Stacklayout条目,以便在XAML.cs中使用它file@mohammadanouti,我已经更新了我的答案,请检查一下。