Ich versuche herauszufinden, wie Sie eine .XML-Layout-Datei für ein benutzerdefiniertes Steuerelement in Xamarin.Forms verwenden. Kann jemand ein Beispiel für die Verwendung einer .axml-Datei in einem benutzerdefinierten Renderer bereitstellen? Zum Beispiel möchte ich ein benutzerdefiniertes Eingabesteuerelement (EnhancedEntry) erstellen. Ich möchte, dass es konfigurierbare Hintergrund- und Rahmenfarben sowie eine konfigurierbare Rahmenbreite hat.Xamarin.Forms benutzerdefinierten Renderer für Android-Steuerelement von .XML-Layout
I Hintergrundform erstellt haben,
drawable/enchancedEntryBackground.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:bottomLeftRadius="3dp" android:bottomRightRadius="3dp" android:topLeftRadius="3dp" android:topRightRadius="3dp" />
<stroke android:width="0.5dp" android:color="@android:color/holo_blue_dark" />
<solid android:color="@android:color/white" />
</shape>
ich ein Layout in einer .axml Datei so definiert haben,
layout/EnhancedEntry.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/enhancedEntry"
android:background="@drawable/enhancedentrybackground" />
</LinearLayout>
einen benutzerdefinierten Renderer Skelett und eine Klasse mit Eigenschaften Aufgrund der Unterstützung Verbesserungen, ist es möglich, die Xml und Axml Spezifikationen zu verwenden, um das neue Aussehen zu erstellen?
[assembly: ExportRenderer(typeof(EnhancedEntry), typeof(EnhancedEntryRenderer))]
namespace eSiteMobile.Droid.CustomRenderers
{
public class EnhancedEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
}
}
public class EnhancedEntry : Entry
{
public static BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color),
typeof(EnhancedEntry), default(Color), defaultBindingMode: BindingMode.OneWay);
public static BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(BorderWidth), typeof(int),
typeof(EnhancedEntry), default(int), defaultBindingMode: BindingMode.OneWay);
public Color BorderColor
{
get { return (Color) GetValue(BorderColorProperty); }
set { SetValue(BorderColorProperty, value); }
}
public int BorderWidth
{
get { return (int) GetValue(BorderWidthProperty); }
set { SetValue(BorderWidthProperty, value); }
}
}