条目的圆角 - Xamarin 形成 UWP [英] Rounded corner for Entry - Xamarin forms UWP
问题描述
有没有什么办法可以将 Entry 的半径自定义为稍微圆角?
您可以在 xamarin.forms 中使用 Custom Renderer
在 iOS 中
//...使用 App11;使用 App11.iOS;使用 Xamarin.Forms;使用 Xamarin.Forms.Platform.iOS;[程序集:ExportRenderer(typeof(MyEntry), typeof(MyiOSEntry))]命名空间 App11.iOS{公共类 MyiOSEntry:EntryRenderer{protected override void OnElementChanged(ElementChangedEventArgs e){base.OnElementChanged(e);如果(控制!= null){Control.Layer.MasksToBounds = true;Control.Layer.CornerRadius = 10;//设置圆角Control.Layer.BorderColor = UIColor.Red.CGColor;Control.Layer.BorderWidth = 3;}}}}
<块引用>
在安卓中
在文件夹Resource->drawable中创建一个xml文件edit_text_style.xml
<固体机器人:颜色=#ffffff"/><角落机器人:半径=10dp"/><中风机器人:宽度=2dp"android:color="#3bbdfa"/></形状></项目>
在自定义渲染器
使用Android.Support.V4.Content.Res;使用 App11;使用 App11.Droid;使用 Xamarin.Forms;使用 Xamarin.Forms.Platform.Android;[组装:ExportRenderer(typeof(MyEntry), typeof(MyAndriodEntry))]命名空间 App11.Droid{公共类 MyAndriodEntry:EntryRenderer{公共 MyAndriodEntry(上下文上下文):基础(上下文){}protected override void OnElementChanged(ElementChangedEventArgs e){base.OnElementChanged(e);如果(控制!=空){Control.SetBackground(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.edit_text_style, null));}}}}
<块引用>
在 UWP 中
创建一个名为 Styles 的文件夹并添加一个新项目作为类型 Resource Dictionary 并将其命名为 Dictionary1.xaml在 Dictionary1.xaml 中将此代码用于圆形文本框.
在自定义渲染器
使用App11;使用 App11.UWP;使用 Windows.UI.Xaml.Controls;使用 Xamarin.Forms;使用 Xamarin.Forms.Platform.UWP;[组装:ExportRenderer(typeof(MyEntry), typeof(MyUWPEntry))]命名空间 App11.UWP{公共类 MyUWPEntry:EntryRenderer{protected override void OnElementChanged(ElementChangedEventArgs e){base.OnElementChanged(e);如果(控制!=空){Control.Style = (Windows.UI.Xaml.Style)App11.UWP.App.Current.Resources["StyleRoundedTextBox"];}}}}
如何更改此样式以及如何创建此代码?很简单,在 msdn.com 中在 uwp 中搜索objectName"默认样式,然后您将找到所需对象的默认样式.以您想要的方式更改它并直接将其添加到应用程序资源或链接它(就像我在这里所做的那样)然后在 CustomRenderer 中加载您的样式
有关 UWP 的更多详细信息,您可以参考此处
<块引用>在表单中
使用系统;使用 System.Collections.Generic;使用 System.Text;使用 Xamarin.Forms;命名空间 App11{公共类 MyEntry : 条目{公共 MyEntry(){}}}
在xxx.cs文件中
Content = new StackLayout{儿童 = {new MyEntry {Text = "In Shared Code",}},VerticalOptions = LayoutOptions.CenterAndExpand,HorizontalOptions = LayoutOptions.CenterAndExpand,};
Is there anything possible to customize the radius of Entry to having a slightly rounded corner?
You can use Custom Renderer
in xamarin.forms
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
creat a xml file in the folder Resource->drawable 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>
in 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
create a folder named Styles and add a new item as type Resource Dictionary and name it Dictionary1.xaml in Dictionary1.xaml put this code for a rounded Textbox .
in 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"];
}
}
}
}
how do I changed this style and how do I create this code ? It's simple , in msdn.com search for "objectName" default style in uwp then you will find default style for the object you need . change it in the way you want and add it to application resources directly or link it (like what I did here) then load your style in CustomRenderer
for more detail about UWP yo can refer here
in Forms
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace App11
{
public class MyEntry : Entry
{
public MyEntry()
{
}
}
}
in xxx.cs file
Content = new StackLayout
{
Children = {
new MyEntry {Text = "In Shared Code",}
},
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
这篇关于条目的圆角 - Xamarin 形成 UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!