Xamarin带有2个按钮和输入项的Forms Custom Stepper

时间:2018-11-19 12:49:52

标签: xamarin.forms custom-renderer stepper

我实现了这个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++;
            }

        }
    }

当正常放置在页面中时,我可以访问它并采用text属性,并在我的Xaml.cs代码中使用它。但就我而言,我将其放置在列表视图中,并且您知道在列表视图中项目是可绑定的,因此我无法直接访问它。在常规步进器中,当将其放置在列表视图中时,我们可以使用“ ValueChanged”方法,并且可以通过使用Xaml.cs文件中“ ValueChanged”方法中的e.NewValue轻松获取值。有没有一种方法可以向CustomStepper类添加一些内容,以帮助我访问Text属性并在Xaml.cs文件中使用它?预先感谢

1 个答案:

答案 0 :(得分:1)

您可以为EventHandler s创建一个属性。在这种情况下,您可以在属性上使用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代码中的事件处理程序绑定/分配给OnValueChanged属性,该属性将在值更改时触发。