Xamarin.forms 入口圆角-Xamarin表单UWP

Xamarin.forms 入口圆角-Xamarin表单UWP,xamarin.forms,customization,xamarin.uwp,custom-renderer,xamarin.forms.entry,Xamarin.forms,Customization,Xamarin.uwp,Custom Renderer,Xamarin.forms.entry,是否有任何方法可以自定义入口半径,使其具有略微圆角 您可以在xamarin.forms中使用自定义呈现程序 在iOS中 在自定义渲染器中 using Android.Support.V4.Content.Res; using App11; using App11.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(MyEntry), typeof(

是否有任何方法可以自定义入口半径,使其具有略微圆角


您可以在xamarin.forms中使用
自定义呈现程序

在iOS中

自定义渲染器中

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) );
        }

    }
  }
}
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中搜索uwp中的“objectName”默认样式,然后您将找到所需对象的默认样式。以您想要的方式更改它,并将其直接添加到应用程序资源或链接它(就像我在这里所做的那样),然后在CustomRenderer中加载您的样式

有关UWP的更多详细信息,请参考

以形式

在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;
    }
}
公共类CustomEntryRenderer:ViewRenderer { 受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e) { 基础。一个要素发生变化(e); var textBox=新的textBox(); textBox.BorderThickness=newwindows.UI.Xaml.Thickness(1); textBox.BorderBrush=新的SolidColorBrush(GetSolidColorBrush(“#444444”).Color); textBox.CornerRadius=新的Windows.UI.Xaml.CornerRadius(10); 此.SetNativeControl(文本框); } 公共SolidColorBrush GetSolidColorBrush(字符串十六进制) { hex=hex.Replace(“#”,string.Empty); 字节r=(字节)(Convert.ToUInt32(十六进制子字符串(0,2,16)); 字节g=(字节)(Convert.ToUInt32(十六进制子字符串(2,2,16)); 字节b=(字节)(Convert.ToUInt32(十六进制子字符串(4,2,16)); SolidColorBrush myBrush=新的SolidColorBrush(Windows.UI.Color.FromArgb(255,r,g,b)); 归还我的画笔; } }
天哪,这没那么难

除非我遗漏了什么,否则只需将其包装在一个
帧中,该帧的
isclippedtobunds
设置为true,然后在帧上放置一个角半径

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

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

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

    }
 }
}
Content = new StackLayout
 {
    Children = {
                 new MyEntry {Text = "In Shared Code",}
                },
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand,
 };
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;
    }
}