首页 > 解决方案 > 入口圆角 - Xamarin 形成 UWP

问题描述

有什么办法可以将入口半径自定义为稍微圆角?

在此处输入图像描述

标签: xamarin.formscustomizationxamarin.uwpcustom-rendererxamarin.forms.entry

解决方案


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

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

在安卓中

在文件夹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) );
        }

    }
  }
}

在 UWP 中

创建一个名为 Styles 的文件夹,并将一个新项目添加为 Resource Dictionary 类型,并将其命名为 Dictionary1.xaml 中的 Dictionary1.xaml 将此代码用于圆形 Textbox 。

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

有关 UWP 的更多详细信息,您可以参考这里

在表格中

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

推荐阅读