答案 0 :(得分:39)
您也可以通过使用StackLayout
BackgroundColor="your color"
和Padding="1"
包装编辑器并将编辑器的BackgroundColor
设置为与表单相同的颜色来实现此目的。
这样的事情:
<StackLayout BackgroundColor="White"> <StackLayout BackgroundColor="Black" Padding="1"> <Editor BackgroundColor="White" /> </StackLayout> ... </StackLayout>
不是那么花哨,但这至少会让你成为一个边界!
答案 1 :(得分:26)
这是我使用的完整解决方案。你需要三件事。
1 - 在表单项目中实现Editor
的自定义类。
public class BorderedEditor : Editor
{
}
2 - iOS项目中自定义Editor
的自定义渲染器。
public class BorderedEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Layer.CornerRadius = 3;
Control.Layer.BorderColor = Color.FromHex("F0F0F0").ToCGColor();
Control.Layer.BorderWidth = 2;
}
}
}
3 - iOS项目中的ExportRenderer
属性告诉Xamarin将自定义渲染器用于自定义编辑器。
[assembly: ExportRenderer(typeof(BorderedEditor), typeof(BorderedEditorRenderer))]
然后在Xaml中使用自定义编辑器:
<custom:BorderedEditor Text="{Binding TextValue}"/>
答案 2 :(得分:4)
添加此控件
public class PlaceholderEditor : Editor
{
public static readonly BindableProperty PlaceholderProperty =
BindableProperty.Create("Placeholder", typeof(string), typeof(string), "");
public PlaceholderEditor()
{
}
public string Placeholder
{
get
{
return (string)GetValue(PlaceholderProperty);
}
set
{
SetValue(PlaceholderProperty, value);
}
}
}
你的android项目中的添加这个渲染器:
[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace Tevel.Mobile.Packages.Droid
{
public class PlaceholderEditorRenderer : EditorRenderer
{
public PlaceholderEditorRenderer() { }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var element = e.NewElement as PlaceholderEditor;
this.Control.Background = Resources.GetDrawable(Resource.Drawable.borderEditText);
this.Control.Hint = element.Placeholder;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == PlaceholderEditor.PlaceholderProperty.PropertyName)
{
var element = this.Element as PlaceholderEditor;
this.Control.Hint = element.Placeholder;
}
}
}
}
资源中的&gt; drawable添加XML文件borderEditText.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#F8B334" />
<corners android:radius="12dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF" android:angle="270" />
<stroke android:width="3dp" android:color="#ccc" />
<corners android:radius="12dp" />
</shape>
</item>
</selector>
<强>的Xaml:强>
标题 - xmlns:ctrls="clr-namespace:my control namespace;assembly= my assembly"
控制:
<ctrls:PlaceholderEditor VerticalOptions="Fill" HorizontalOptions="StartAndExpand" Placeholder="add my comment title">
</ctrls:PlaceholderEditor>
答案 3 :(得分:3)
最简单的方法是在其周围添加框架。
<Frame BorderColor="LightGray" HasShadow="False" Padding="0">
<Editor/>
</Frame>
答案 4 :(得分:1)
由于Custom Renderer
尚不支持自定义BorderColor
的{{1}},因此您需要为每个平台实施Entry
(guide from Xamarin)。
由于您已经设法更改了Android上的Xamarin.Forms
,因此您可以在此处找到适用于iOS的解决方案:http://forums.xamarin.com/discussion/comment/102557/#Comment_102557
答案 5 :(得分:0)
这肯定有用,试试这个。
Android渲染器
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics;
[assembly: ExportRenderer(typeof(Entry), typeof(some.namespace.EntryRenderer))]
namespace some.namespace
{
public class EntryRenderer : Xamarin.Forms.Platform.Android.EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || Element == null || e.OldElement != null) return;
var customColor = Xamarin.Forms.Color.FromHex("#0F9D58");
Control.Background.SetColorFilter(customColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
}
}
答案 6 :(得分:0)
简单的Android渲染器方法
if (((Editor)Element).HasBorder)
{
GradientDrawable gd = new GradientDrawable();
gd.SetColor(Android.Resource.Color.HoloRedDark);
gd.SetCornerRadius(4);
gd.SetStroke(2, Android.Graphics.Color.LightGray);
Control.SetBackground(gd);
}
答案 7 :(得分:0)
将自定义控件构建为网格。在它周围构建BoxView并将编辑器放在中间并带有边距。 不好但很简单...
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:CustomControls="clr-namespace:xxx.CustomControls"
x:Class="xxx.CustomControls.EditorWithBorder" BackgroundColor="White"
x:Name="this">
<Grid.RowDefinitions>
<RowDefinitionCollection>
<RowDefinition Height="1"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1"/>
</RowDefinitionCollection>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinitionCollection>
<ColumnDefinition Width="1"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1"/>
</ColumnDefinitionCollection>
</Grid.ColumnDefinitions>
<Editor Text="{Binding Source={x:Reference this},Path=EditorText, Mode=TwoWay}" TextColor="Orange" Margin="20,10,20,10" FontSize="{StaticResource FontSizeLarge}"
Grid.Row="1" Grid.Column="1" />
<BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BackgroundColor="Orange"
></BoxView>
<BoxView Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" BackgroundColor="Orange"
></BoxView>
<BoxView Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" BackgroundColor="Orange" HeightRequest="1"
></BoxView>
<BoxView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" BackgroundColor="Orange" HeightRequest="1"
></BoxView>
</Grid>