首页 > 解决方案 > Xamarin 表单上带有绿色边框的多个圆形图像作为地图引脚,Xamarin android Xamarin IOS

问题描述

我有一个应该有圆形边框的图像。请帮助我从现在开始挣扎

标签: xamarinmaps

解决方案


解决方案1:

您可以将图像放在Frame中,并设置CornerRadiusBorderColor

<Frame CornerRadius="50" Padding="0" BorderColor="Green" HeightRequest="100" WidthRequest="100">
    <Image Source="xxx.png" />
</Frame>

解决方案2:

您可以 Custom Renderer在 xamarin.forms中使用

在 iOS 中

//...
using xxx;
using xxx.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MyImage), typeof(MyImageRenderer))]
namespace xxx.iOS
{
  public class MyImageRenderer:ImageRenderer
  {
    protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.Layer.MasksToBounds = true;
            Control.Layer.CornerRadius = 50;  //set the rounded corner
            Control.Layer.BorderColor = UIColor.Green.CGColor;
            Control.Layer.BorderWidth = 1;
        }
    }
 }
}

在安卓中

在文件夹Resource->drawable中创建一个 xml 文件 image_view_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item>
   <shape
     android:shape="rectangle">
    <solid
      android:color="#00ff00" />
    <corners
      android:radius="50dp" />
    <stroke
      android:width="1dp"
      android:color="#00ff00" />
   </shape>
</item>
using Android.Support.V4.Content.Res;
using xxx;
using xxx.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyImage), typeof(MyImageRenderer))]
namespace xxx.Droid
{
 public class MyImageRenderer:ImageRenderer
 {
   public MyAndriodEntry(Context context):base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);

        if(Control!=null)
        {
           Control.SetBackgroundResource(Resource.Drawable.image_view_style);
        }

    }
  }
}

在表格中

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace xxx
{
 public class MyImage : Image
 {
    public MyImage()
    {

    }
 }
}

推荐阅读