【问题标题】:Xamarin Forms Custom Stepper with 2 buttons and Entry带有 2 个按钮和条目的 Xamarin 表单自定义步进器
【发布时间】:2018-11-19 12:49:51
【问题描述】:

我实现了这个 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 文件中使用它?提前致谢

【问题讨论】:

    标签: xamarin.forms custom-renderer stepper


    【解决方案1】:

    您可以为EventHandlers 创建一个属性。在这种情况下,您将在属性上使用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 属性,该属性将在值更改时触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2019-11-09
      • 2017-12-24
      • 2020-11-03
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多