Xamarin Forms 带有 2 个按钮和条目的自定义步进器

Xamarin Forms Custom Stepper with 2 buttons and Entry

我实现了这个 CustomStepper:

 using System;
    using Xamarin.Forms;

    namespace AppXamarin
    {
        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: 0,
                  defaultBindingMode: BindingMode.TwoWay);

            public int Text
            {
                get { return (int)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }
            public CustomStepper()
            {
                PlusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
                MinusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
                PlusBtn.Image = "exp20181029Artboard51";
                MinusBtn.Image = "exp20181029Artboard52";
                switch (Device.RuntimePlatform)
                {
                    case Device.UWP:
                    case Device.Android:
                        {
                            PlusBtn.BackgroundColor = Color.Transparent;
                            MinusBtn.BackgroundColor = Color.Transparent;
                            break;
                        }
                    case Device.iOS:
                        {
                            PlusBtn.BackgroundColor = Color.Transparent;
                            MinusBtn.BackgroundColor = Color.Transparent;
                            break;
                        }
                }
                Orientation = StackOrientation.Horizontal;
                PlusBtn.Clicked += PlusBtn_Clicked;
                MinusBtn.Clicked += MinusBtn_Clicked;
                Entry = new Entry { PlaceholderColor = Color.Gray, Keyboard = Keyboard.Numeric, WidthRequest = 30, BackgroundColor = Color.Transparent, FontSize = 15 };
                Entry.Keyboard = Keyboard.Numeric;
                Entry.Behaviors.Add(new NumericValidationBehavior());
                Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
                Entry.HorizontalTextAlignment = TextAlignment.Center;
                Entry.TextChanged += Entry_TextChanged;
                Children.Add(MinusBtn);
                Children.Add(Entry);
                Children.Add(PlusBtn);
            }
            private void Entry_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
                    this.Text = int.Parse(e.NewTextValue);
            }

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

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

        }
    }

当正常放置在页面中时,我可以访问它并获取文本 属性 并在我的 Xaml.cs 代码中使用它。但就我而言,我将它放在列表视图中,正如您所知,在列表视图中,项目是可绑定的,我无法直接访问它。在常规步进器中,当它被放置在列表视图中时,我们可以使用 "ValueChanged" 方法,并且可以通过在 Xaml.cs 文件中的 "ValueChanged" 方法中使用 e.NewValue 轻松获取值.有没有一种方法可以向 CustomStepper class 添加一些东西来帮助我访问文本 属性 并在 Xaml.cs 文件中使用它?提前致谢

您可以为 EventHandler 创建一个 属性。在这种情况下,您可以在 属性 上使用 event 修饰符来告诉程序 属性 正在触发一个事件。例如:

private EventHandler onValueChangedEvent = null;

public event EventHandler OnValueChanged
{
    add
    {
        onValueChangedEvent = null;
        onValueChangedEvent = value;
    }
    remove
    {
        // Will show a warning. You can ignore it.
        onValueChangedEvent = null;
    }
}

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

    onValueChangedEvent?.Invoke(this, e);
}

然后您会在 xaml.cs 代码中 bind/assign 一个事件处理程序到 OnValueChanged 属性,它会在值更改时触发。