2017-11-25 102 views
1

我正在开发Xamarin Forms中的应用程序,并且遇到了应该在很久以前修复的错误。 我试图为框架设置轮廓,它适用于ios,但不适用于android。Frame OutlineColor不适用于Android,Xamarin Forms

这是我的xaml框架。

<Frame 
    HasShadow="true" 
    CornerRadius="10" 
    OutlineColor="Red" 
    BackgroundColor="White"> 
    <StackLayout 
     Orientation="Horizontal"> 
     <Label 
     Text="TEST" 
     VerticalOptions="Center"/> 
     <Label 
     Text="For OUTLINE" 
     VerticalOptions="Center"/> 
    </StackLayout> 
</Frame> 

感谢您的帮助!

回答

2

这不适用于Android。 Bug报告为here

解决方案1:

你应该做一个RendererFrame为Android平台如下:

using System; 
using Android.Graphics.Drawables; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 
using XFormsUI.Droid; 

[assembly: ExportRenderer(typeof(Frame), typeof(RoundBorderFrameRenderer))] 
namespace XFormsUI.Droid 
{ 
    public class RoundBorderFrameRenderer : FrameRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Frame> e) 
     { 
      base.OnElementChanged(e); 

      if (e.NewElement != null && e.OldElement == null) 
      { 
       SetBackgroundResource(Resource.Drawable.round_border_frame); 

       //Write following lines if you want to bring the XAML Frame Elements's background Color into Native. Otherwise, Frame's BackgroundColor will not be effective. 
       GradientDrawable drawable = (GradientDrawable)Background; 
       string FrameBackgroundColorHex = String.Format("#{0:X2}{1:X2}{2:X2}", (int)(e.NewElement.BackgroundColor.R * 255), (int)(e.NewElement.BackgroundColor.G * 255), (int)(e.NewElement.BackgroundColor.B * 255)); 
       drawable.SetColor(Android.Graphics.Color.ParseColor(FrameBackgroundColorHex)); 
      } 
     } 
    } 
} 

这也需要一个xml(round_border_frame.xml)选择文件。将该文件放入您的Drawable文件夹中。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <!--We need to give Frame's 'BorderColor Here--> 
    <stroke android:width="1dp" android:color="#FF0000" /> 
    <corners android:radius="10dp" /> 
</shape> 

解决方案2:

使用Renderer而不选择XML文件。 Source

using System; 
using Android.Graphics; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 
using XFormsUI.Droid; 

[assembly: ExportRenderer(typeof(Frame), typeof(RoundBorderFrameRenderer))] 
namespace XFormsUI.Droid 
{ 
    public class RoundBorderFrameRenderer : FrameRenderer 
    { 
     public override void Draw(Canvas canvas) 
     { 
      base.Draw(canvas); 

      using (var paint = new Paint { AntiAlias = true }) 
      using (var path = new Path()) 
      using (Path.Direction direction = Path.Direction.Cw) 
      using (Paint.Style style = Paint.Style.Stroke) 
      using (var rect = new RectF(0, 0, canvas.Width, canvas.Height)) 
      { 
       float px = Forms.Context.ToPixels(Element != null ? Element.CornerRadius : 10); //Default Corner Radius = 10 
       float py = Forms.Context.ToPixels(Element != null ? Element.CornerRadius : 10); //Default Corner Radius = 10 
       path.AddRoundRect(rect, px, py, direction); 

       //Set the Width of the Border here 
       paint.StrokeWidth = 1f; 
       paint.SetStyle(style); 

       //Take OutLineColor from XAML Frame element and set it natively here. 
       string FrameBorderColorHex = String.Format("#{0:X2}{1:X2}{2:X2}", (int)(Element.OutlineColor.R * 255), (int)(Element.OutlineColor.G * 255), (int)(Element.OutlineColor.B * 255)); 
       paint.Color = Android.Graphics.Color.ParseColor(FrameBorderColorHex); 
       canvas.DrawPath(path, paint); 
      } 
     } 
    } 
} 

解决方案3:

然而,这是不是最好的解决方案,但它是一种解决方法的: 包装你的外Frame内的另一个FramePadding并设置其BackgroundColor到任何您的边框颜色应该是。

说明:我还没有在iOS系统上对此进行检查。

<Frame BackgroundColor="Red" CornerRadius="10" Padding="1,1,1,1"> 
    <Frame CornerRadius="10" BackgroundColor="White"> 
     <StackLayout Orientation="Horizontal"> 
      <Label Text="TEST" VerticalOptions="Center" /> 
      <Label Text="For OUTLINE" VerticalOptions="Center" /> 
     </StackLayout> 
    </Frame> 
</Frame> 

人们正在讨论这个thread

希望这会有所帮助!