Entry 的圆角 - Xamarin 形成 UWP

Rounded corner for Entry - Xamarin forms UWP

是否可以将 Entry 的半径自定义为略带圆角?

您可以在 xamarin.forms

中使用 Custom Renderer

in iOS

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

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyiOSEntry))]
namespace App11.iOS
{
  public class MyiOSEntry:EntryRenderer
  {
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

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

in Android

在文件夹 Resource->drawable 中创建一个 xml 文件 edit_text_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="#ffffff" />
    <corners
      android:radius="10dp" />
    <stroke
      android:width="2dp"
      android:color="#3bbdfa" />
   </shape>
</item>

Custom Renderer

using Android.Support.V4.Content.Res;
using App11;
using App11.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyAndriodEntry))]
namespace App11.Droid
{
 public class MyAndriodEntry:EntryRenderer
 {
   public MyAndriodEntry(Context context):base(context)
    {

    }

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

        if(Control!=null)
        {
            Control.SetBackground(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.edit_text_style, null) );
        }

    }
  }
}

in UWP

创建一个名为 Styles 的文件夹并添加一个类型为 Resource Dictionary 的新项目并将其命名为 Dictionary1.xaml 在 Dictionary1.xaml 中将此代码放入圆形文本框。

Custom Renderer

using App11;
using App11.UWP;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyUWPEntry))]
namespace App11.UWP
{
  public class MyUWPEntry:EntryRenderer
  {

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

        if(Control!=null)
        {
           Control.Style = (Windows.UI.Xaml.Style)App11.UWP.App.Current.Resources["StyleRoundedTextBox"];
        }

    }
  }
}

如何更改此样式以及如何创建此代码? 很简单,在 msdn.com 中搜索 "objectName" default style in uwp 然后你会找到你需要的对象的默认样式。以您想要的方式更改它并将其直接添加到应用程序资源或 link 它(就像我在这里所做的那样)然后在 CustomRenderer

中加载您的样式

更多关于UWP的细节你可以参考here

in Forms

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

namespace App11
{
 public class MyEntry : Entry
 {
    public MyEntry()
    {

    }
 }
}

在 xxx.cs 文件中

Content = new StackLayout
 {
    Children = {
                 new MyEntry {Text = "In Shared Code",}
                },
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand,
 };

对于 Windows 应用,您可以使用渲染器自定义条目。

public class CustomEntryRenderer : ViewRenderer<CustomEntry, TextBox>
{
    protected override void OnElementChanged(ElementChangedEventArgs<CustomEntry> e)
    {
        base.OnElementChanged(e);
        var textBox = new TextBox();
        textBox.BorderThickness = new Windows.UI.Xaml.Thickness(1);
        textBox.BorderBrush = new SolidColorBrush(GetSolidColorBrush("#444444").Color);
        textBox.CornerRadius = new Windows.UI.Xaml.CornerRadius(10);
        this.SetNativeControl(textBox);
    }
    public SolidColorBrush GetSolidColorBrush(string hex)
    {
        hex = hex.Replace("#", string.Empty);
        byte r = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
        byte g = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
        byte b = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));

        SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(255, r, g, b));
        return myBrush;
    }
}

天哪,没那么难。

除非我遗漏了什么,只需将其包裹在 IsClippedToBounds 设置为 true 的 Frame 中,然后在框架上放置一个圆角半径。

我猜也许有一些原因这不是一个好的解决方案,但我经常使用它。