目前我正在使用MvvmCross开发Xamarin Forms Android项目。关于Frame我有一个奇怪的问题。每当我设置OutlineColor时,它仅在iOS中显示,而不在Android中显示。我已尝试使用不同的Xamarin Forms项目,并且两个平台都显示它没有任何问题。我没有任何迹象表明为什么会这样。 MvvmCross能否以某种方式与此问题相关?
以下是一个示例:
<core:BasePage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:core="clr-namespace:Core.Base.Views;assembly=Core"
x:Class="Views.TestPage"
BackgroundImage="background_secret.png"
Title="Test">
<ContentPage.Content>
<Grid
HorizontalOptions="FillAndExpand"
Padding="12,20,12,20"
VerticalOptions="FillAndExpand">
<Frame
HasShadow="false"
VerticalOptions="Fill"
BackgroundColor="White"
OutlineColor="#1961ac">
<StackLayout>
<Frame
VerticalOptions="Start"
Padding="8,4,8,4"
HasShadow="false"
OutlineColor="#9DB0BB">
<Label Text="Test"></Label>
</Frame>
</StackLayout>
</Frame>
</Grid>
</ContentPage.Content>
</core:BasePage>
Xamarin表格版本2.1 MvvmCross版本4.1
答案 0 :(得分:8)
即使我遇到了同样的问题,为了解决这个问题,我已经为Frame控件添加了自定义渲染器。 在framerenderer中需要重写方法Draw和私有方法DrawOutline如下,
public override void Draw(ACanvas canvas)
{
base.Draw(canvas);
DrawOutline(canvas, canvas.Width, canvas.Height, 4f);//set corner radius
}
void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius)
{
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, width, height))
{
float rx = Forms.Context.ToPixels(cornerRadius);
float ry = Forms.Context.ToPixels(cornerRadius);
path.AddRoundRect(rect, rx, ry, direction);
paint.StrokeWidth = 2f; //set outline stroke
paint.SetStyle(style);
paint.Color = Color.ParseColor("#A7AE22");//set outline color //_frame.OutlineColor.ToAndroid();
canvas.DrawPath(path, paint);
}
}
在另一种方法中,您还可以考虑使用圆角的android选择器xml作为后台资源。 有关此问题的更多详细信息,请查看我的博文:http://www.appliedcodelog.com/2016/11/xamarin-form-frame-outline-color_21.html
答案 1 :(得分:1)
Suchith的答案是正确的,可以在这里解决我的问题,但是0
自Xamarin 2.5版以来已经过时了。
现在,更好的方法是使用Xamarin.Forms.Forms.Context
,所以这就是现在的代码。
Android.App.Application.Context
在此link中,我们对为什么应该使用这种新方法以及为什么 public override void Draw(Canvas canvas)
{
base.Draw(canvas);
DrawOutline(canvas, canvas.Width, canvas.Height, 4f);//set corner radius
}
void DrawOutline(Canvas canvas, int width, int height, float cornerRadius)
{
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, width, height))
{
float rx = Android.App.Application.Context.ToPixels(cornerRadius);
float ry = Android.App.Application.Context.ToPixels(cornerRadius);
path.AddRoundRect(rect, rx, ry, direction);
paint.StrokeWidth = 2f; //set outline stroke
paint.SetStyle(style);
paint.Color = Android.Graphics.Color.ParseColor("#FFFFFF");//set outline color //_frame.OutlineColor.ToAndroid();
canvas.DrawPath(path, paint);
}
}
已经过时有了很好的解释。
答案 2 :(得分:0)
MyCustomRenderer,见到你;)
using Xamarin.Forms.Platform.Android;
using Android.Graphics;
using Android.Graphics.Drawables;
[assembly: ExportRenderer(typeof(Frame), typeof(YourProject.Droid.Renderers.BorderFrameRenderer))]
namespace YourProject.Droid.Renderers
{
public class BorderFrameRenderer : FrameRenderer
{
public override void Draw(Canvas canvas)
{
base.Draw(canvas);
using (var strokePaint = new Paint())
using (var rect = new RectF(0, 0, canvas.Width, canvas.Height))
{
// stroke
strokePaint.SetStyle(Paint.Style.Stroke);
strokePaint.Color = Element.OutlineColor.ToAndroid();
strokePaint.StrokeWidth = 5;
canvas.DrawRoundRect(rect, Element.CornerRadius * 2, Element.CornerRadius * 2, strokePaint); // stroke
}
}
public BorderFrameRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
}
}
}