2016-06-21 1 views
1

저는 Xamarin.Forms를 사용하고 있으며 버튼 색상으로 그래디언트를 설정하려고합니다. 나는이 사람과 정확히 같은 문제가있다 : Gradient Layer form as the form of a button in Xamarin.Forms Android버튼 색상 렌더러로 그래디언트 설정

그러나 주어진 대답은 내가 바라는 것이 아니다.

대답은 성공적이지만 모든 버튼을 그라디언트로 설정합니다.

I.E : 사람은 배경 만 칠해졌고, 주어진 대답은 전체 버튼을 설정하고, 버튼 자체의 색상을 설정하고 패딩을 유지하려고합니다.

Here 나는 왜 코드가 작동하지 않는지, 어떻게 대답을 사용하고이 작업을 할 수 있습니까? 편집 이와 나는 올바른 그라데이션하지만 버튼을 얻을, 나는 시도

public class GenericButtonRenderer :ButtonRenderer 
{ 
    private Xamarin.Forms.Color StartColor { get; set; } 
    private Xamarin.Forms.Color EndColor { get; set; } 

    protected override void DispatchDraw(Canvas canvas) 
    { 
     var gradient = new Android.Graphics.LinearGradient(0, 0, Width, Height, 
      this.StartColor.ToAndroid(), 
      this.EndColor.ToAndroid(), 
      Android.Graphics.Shader.TileMode.Clamp); 
     var paint = new Android.Graphics.Paint() 
     { 
      Dither = true, 
     }; 
     paint.SetShader(gradient); 
     canvas.DrawPaint(paint); 
     base.DispatchDraw(canvas); 
    } 

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
    { 
     base.OnElementChanged(e); 
     if (e.OldElement != null || Element == null) 
     { 
      return; 
     } 
     try 
     { 
      var btn = e.NewElement as GenericButton; 
      this.StartColor = btn.StartColor; 
      this.EndColor = btn.EndColor;     
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(@"   ERROR: ", ex.Message); 
     } 
    } 
}//See result here: http://i.stack.imgur.com/3ZpuX.png 

두 번째 일 : I 버튼을 넘어 backround에 얻을이 코드

public class GenericButton:Button 
{ 
    public Xamarin.Forms.Color StartColor { get; set; } 
    public Xamarin.Forms.Color EndColor { get; set; } 
} 

: 여기 내 코드입니다 레이아웃 잃고 : 나는 더 나은 답을 찾을 때까지 내가 무엇을 찾고 있어요 정확하지 않은 해결에 도착

public class GenericButtonRenderer :ButtonRenderer 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
    { 
     base.OnElementChanged(e); 
     if (e.OldElement != null || Element == null) 
     { 
      return; 
     } 
     try 
     { 
      var btn = e.NewElement as GenericButton; 
      var gradient = new GradientDrawable(GradientDrawable.Orientation.TopBottom, new[] { 
      btn.StartColor.ToAndroid().ToArgb(), 
      btn.EndColor.ToAndroid().ToArgb() 
     }); 
      Control.Background=gradient;     
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(@"   ERROR: ", ex.Message); 
     } 
    } 
}//See result here: http://i.stack.imgur.com/4CDtG.png 
+0

여기에 전체 코드를 게시하십시오. 다른 주제에 대한 링크는 그대로두고 문제가있는 장소와 문제를 해결할 수있는 방법을 보여주는 코드를 게시하십시오. – sphinks

+0

내 코드는 링크의 코드와 정확히 같습니다. – Jaspion

+0

그건 중요하지 않습니다. 다른 사람들이 당신에게 답을 줄 수 있도록하십시오. 당신은 답을 원합니다 - 여기에 모든 코드를 게시하십시오. 한 곳에서 모든 것을 보았을 때 여러 링크로가는 것이 훨씬 쉽습니다. – sphinks

답변

0

이 함께 갈 것입니다 (에 작동하지 않습니다 이 이상).

내 단추가 표 안에 있고 그리드의 유일한 구성 요소이므로 그리드에 다음 단추를 추가하면 단추가 화면 아래쪽에 표시되지 않습니다. (단, 상단의 패딩 제대로 동작하지 않습니다.) 난 아직도 누군가가 더 나은 솔루션을 찾을 수 있다는 희망을 가지고

Grid grid = new Grid 
     { 
      VerticalOptions = LayoutOptions.EndAndExpand, 
      HorizontalOptions = LayoutOptions.FillAndExpand, 
      Padding = new Thickness(5,5,5,5), 
      ColumnDefinitions =.... 

아직이 답변을 선택하지 않음.

1

ButtonRenderer를 확장하고 네이티브 컨트롤의 Background 속성을 사용자 지정해야합니다.

/// <summary> 
    /// Extends <see cref="Xamarin.Forms.Button"/>. 
    /// </summary> 
    public class ExtendedButton : Button 
    { 
     /// <summary> 
     /// Bindable property for button background gradient start color 
     /// </summary> 
     public static readonly BindableProperty StartColorProperty = 
      BindableProperty.Create("StartColor", typeof(Color), typeof(ExtendedButton), Color.Gray); 

     /// <summary> 
     /// Gets or sets the background gradient start color 
     /// </summary> 
     public Color StartColor 
     { 
      get { return (Color)this.GetValue(StartColorProperty); } 
      set { this.SetValue(StartColorProperty, value); } 
     } 

     /// <summary> 
     /// Bindable property for button background gradient end color 
     /// </summary> 
     public static readonly BindableProperty EndColorProperty = 
      BindableProperty.Create("EndColor", typeof(Color), typeof(ExtendedButton), Color.Black); 

     /// <summary> 
     /// Gets or sets the background gradient end color 
     /// </summary> 
     public Color EndColor 
     { 
      get { return (Color)this.GetValue(EndColorProperty); } 
      set { this.SetValue(EndColorProperty, value); } 
     } 
    } 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Graphics.Drawables; 

using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 

using NewHelloWorld.Droid.Renderers; 
using NewHelloWorld; 



[assembly: ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer))] 
namespace NewHelloWorld.Droid.Renderers 
{ 
    /// <summary> 
    /// Class ExtendedButtonRenderer. 
    /// </summary> 
    public class ExtendedButtonRenderer : ButtonRenderer 
    { 
     /// <summary> 
     /// Called when [element changed]. 
     /// </summary> 
     /// <param name="e">The e.</param> 
     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
     { 
      base.OnElementChanged(e); 
      UpdateGradientBackground(); 
     } 

     /// <summary> 
     /// Handles the <see cref="E:ElementPropertyChanged" /> event. 
     /// </summary> 
     /// <param name="sender">The sender.</param> 
     /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param> 
     protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      if(e.PropertyName == ExtendedButton.StartColorProperty.PropertyName || 
       e.PropertyName == ExtendedButton.EndColorProperty.PropertyName || 
       e.PropertyName == ExtendedButton.BorderColorProperty.PropertyName || 
       e.PropertyName == ExtendedButton.BorderRadiusProperty.PropertyName || 
       e.PropertyName == ExtendedButton.BorderWidthProperty.PropertyName) 
      { 
       UpdateGradientBackground(); 
      } 

      base.OnElementPropertyChanged(sender, e); 
     } 

     //Called 
     protected override void UpdateBackgroundColor() 
     { 
      base.UpdateBackgroundColor(); 

      UpdateGradientBackground(); 
     } 

     //Sets the background gradient color 
     private void UpdateGradientBackground() 
     { 
      var button = this.Element as ExtendedButton; 
      if (button != null) 
      { 
       int[] colors = new int[] { button.StartColor.ToAndroid(), button.EndColor.ToAndroid() }; 
       var gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TopBottom, colors); 
       gradientDrawable.SetGradientType(GradientType.LinearGradient); 
       gradientDrawable.SetShape(ShapeType.Rectangle); 
       gradientDrawable.SetCornerRadius(button.BorderRadius); 
       gradientDrawable.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); 
       this.Control.Background = gradientDrawable; 
      } 
     } 
    } 
}