2017-08-11 39 views
0

我想在Xamarin Forms Project中使用步進器,但它不會在Android中正確調整寬度。 WithRequest不會在所有的工作,當我設置邊距它只調整其中一個按鈕,另一個仍然在它的正常大小。步進寬度不會在Xamarin表格中正確調整大小Android

image1

這裏是我的代碼:

<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> 

回答

1

我想在我Xamarin使用步進窗體項目,但不會在Android的正常調整寬度。 WithRequest不會在所有的工作,當我設置邊距它只調整其中一個按鈕,另一個仍然在它的正常大小。

按鈕的尺寸未根據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使用該渲染器。

和步進按鈕寬度將適當地改變:

enter image description here

相關問題