使用 MvvmCross 的 Xamarin Forms Android 项目中未显示框架的轮廓颜色
Outline color of the Frame is not displayed in Xamarin Forms Android Project using MvvmCross
目前我正在使用 MvvmCross 开发 Xamarin Forms Android 项目。我有一个关于框架的奇怪问题。每当我设置 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
即使我遇到了同样的问题,为了解决这个问题,我已经为帧控制添加了自定义渲染器。
在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 作为背景资源。
有关此的更多详细信息,请查看我的博客 post:http://www.appliedcodelog.com/2016/11/xamarin-form-frame-outline-color_21.html
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);
}
}
}
Suchith 的回答是正确的,在这里解决了我的问题,但是 Xamarin.Forms.Forms.Context
自 Xamarin 2.5 版以来已经过时了。
现在,更好的方法是使用 Android.App.Application.Context
,所以这就是现在的代码。
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);
}
}
在这篇 中,我们很好地解释了为什么我们应该使用这种新方法以及为什么 Xamarin.Forms.Forms.Context
已过时。
目前我正在使用 MvvmCross 开发 Xamarin Forms Android 项目。我有一个关于框架的奇怪问题。每当我设置 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
即使我遇到了同样的问题,为了解决这个问题,我已经为帧控制添加了自定义渲染器。 在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 作为背景资源。 有关此的更多详细信息,请查看我的博客 post:http://www.appliedcodelog.com/2016/11/xamarin-form-frame-outline-color_21.html
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);
}
}
}
Suchith 的回答是正确的,在这里解决了我的问题,但是 Xamarin.Forms.Forms.Context
自 Xamarin 2.5 版以来已经过时了。
现在,更好的方法是使用 Android.App.Application.Context
,所以这就是现在的代码。
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);
}
}
在这篇 Xamarin.Forms.Forms.Context
已过时。