Xamarin Forms 中的步进器宽度无法正确调整 Android

Stepper width won't resize properly in Xamarin Forms Android

我想在我的 Xamarin Forms 项目中使用步进器,但它无法在 Android 中正确调整宽度。 WithRequest 根本不起作用,当我设置边距时,它只会调整两个按钮之一的大小,另一个仍保持正常大小。

这是我的代码:

<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width=".3*"/>
    <ColumnDefinition Width=".5*"/>
    <ColumnDefinition Width=".2*"/>
    <ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    ...
</Grid.RowDefinitions>
...
<Stepper Grid.Row="7" Grid.Column="1" Margin="20,0,20,0"/>
</Grid>

I want to use a Stepper in my Xamarin Forms Project, but it won't resize the width properly in Android. The WithRequest wont work at all an when I set the margin it only resize one of the two buttons, the other one is still at it's normal size.

按钮的大小未根据WidthRequest属性设置。如果要设置按钮的大小,则必须制作自己的步进控件。

StepperRendererOnElementChange 方法的源代码进行一些更改,使其适用于您自己的自定义控件。以下是 StepperRenderer 的修改版本:

[assembly:ExportRenderer(typeof(MyStepper),typeof(MyStepperRenderer))]
namespace StepperDemo.Droid
{
    public class MyStepperRenderer : ViewRenderer<Stepper, LinearLayout>
    {
        AButton _downButton;
        AButton _upButton;

        public MyStepperRenderer()
        {
            AutoPackage = false;
        }

        protected override LinearLayout CreateNativeControl()
        {
            return new LinearLayout(Context) {

                Orientation = Orientation.Horizontal
            };
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                _downButton = new AButton(Context) { Text = "-", Gravity = GravityFlags.Center, Tag = this };
                //Set the MinWidth of Button
                _downButton.SetMinWidth(50);

                _downButton.SetOnClickListener(StepperListener.Instance);

                _upButton = new AButton(Context) { Text = "+", Tag = this };
                _upButton.SetOnClickListener(StepperListener.Instance);
                //Set the MinWidth of Button
                _upButton.SetMinWidth(50);

                if (e.NewElement != null)
                {
                    //Set the Width and Height of the button according to the WidthRequest
                    _downButton.LayoutParameters = new LayoutParams((int)e.NewElement.WidthRequest, LayoutParams.MatchParent);
                    _upButton.LayoutParameters = new LayoutParams((int)e.NewElement.WidthRequest, LayoutParams.MatchParent);
                }

                var layout = CreateNativeControl();

                layout.AddView(_downButton);
                layout.AddView(_upButton);

                SetNativeControl(layout);
            }

            UpdateButtonEnabled();
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            switch (e.PropertyName)
            {
                case "Minimum":
                    UpdateButtonEnabled();
                    break;
               case "Maximum":
                    UpdateButtonEnabled();
                    break;
                case "Value":
                    UpdateButtonEnabled();
                    break;
                case "IsEnabled":
                    UpdateButtonEnabled();
                    break;
            }
        }

        void UpdateButtonEnabled()
        {
            Stepper view = Element;
            _upButton.Enabled = view.IsEnabled ? view.Value < view.Maximum : view.IsEnabled;
            _downButton.Enabled = view.IsEnabled ? view.Value > view.Minimum : view.IsEnabled;
        }

        class StepperListener : Java.Lang.Object, IOnClickListener
        {
            public static readonly StepperListener Instance = new StepperListener();

            public void OnClick(global::Android.Views.View v)
            {
                var renderer = v.Tag as MyStepperRenderer;
                if (renderer == null)
                    return;

                Stepper stepper = renderer.Element;
                if (stepper == null)
                    return;

                if (v == renderer._upButton)
                    ((IElementController)stepper).SetValueFromRenderer(Stepper.ValueProperty, stepper.Value + stepper.Increment);
                else if (v == renderer._downButton)
                    ((IElementController)stepper).SetValueFromRenderer(Stepper.ValueProperty, stepper.Value - stepper.Increment);
        }
    }
}

那么就可以根据CustomRenderer Tutorial使用这个渲染器了。

并且步进按钮的宽度将正确更改: