Seit Xamarin 2.4 (und wechseln zu .Net Standard statt PCL) erhalte ich den folgenden Fehler, indem ich mein eigenes Verhalten (XAMLC ist:Keine Eigenschaft, bindbare Eigenschaft oder Ereignis für 'MaxLength' gefunden, oder falscher Typ zwischen Wert und Eigenschaft
using Xamarin.Forms;
namespace com.rsag.xflib.Behaviors
{
/// <summary>
/// Constrain the number of charachters on entry to the given length
/// </summary>
public class MaxLengthEntryBehavior : Behavior<Entry>
{
/// <summary>
/// Value to prevent constraint
/// </summary>
public const int NOT_CONSTRAINED = 0;
/// <summary>
/// Bindable property for <see cref="MaxLength" />
/// </summary>
public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create(nameof(MaxLength),
typeof(int), typeof(MaxLengthEntryBehavior), NOT_CONSTRAINED, validateValue: ValidateMaxValue);
/// <summary>
/// Max. length for the text (-1: not constrained)
/// </summary>
public int MaxLength
{
get => (int) GetValue(MaxLengthProperty);
set => SetValue(MaxLengthProperty, value);
}
private static bool ValidateMaxValue(BindableObject bindable, object value)
{
if (value is int intValue)
{
return intValue >= NOT_CONSTRAINED;
}
return false;
}
/// <inheritdoc />
protected override void OnAttachedTo(Entry bindable)
{
if (bindable != null)
{
bindable.TextChanged += OnTextChanged;
}
}
/// <inheritdoc />
protected override void OnDetachingFrom(Entry bindable)
{
if (bindable != null)
{
bindable.TextChanged -= OnTextChanged;
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (MaxLength == NOT_CONSTRAINED)
{
return;
}
if (string.IsNullOrEmpty(e.NewTextValue))
{
return;
}
var entry = (Entry) sender;
if (e.NewTextValue.Length > MaxLength)
{
entry.Text = e.NewTextValue.Substring(0, MaxLength);
}
}
}
}
die Verwendung in der App ist auch sehr einfach:
<Entry Text="{Binding ServerPort.Value Keyboard="Numeric">
<Entry.Behaviors>
<libBehav:MaxLengthEntryBehavior MaxLength="{x:Static ac:Constants.MAX_PORT_LENGTH}" />
</Entry.Behaviors>
</Entry>
No property, bindable property, or event found for 'MaxLength', or mismatching type between value and property.
Hier ist die Implementierung (sehr einfach) ist
Diese Kompilierung funktioniert mit einem Literal MaxLength="10"
und mit Bindungen MaxLength="{StaticResource MyValue}"
, aber nicht mit einem Wert aus einer statischen Klasse. Ich brauche den Wert in XAML und in einigen C# -Code, also möchte ich die Constants
Klasse verwenden.
Der Wert in einer statischen Klasse wird wie folgt definiert:
public const int MAX_PORT_LENGTH = 5;
bearbeitet 2018-01-09
Die Probleme bei der Verwendung von inneren Klassen zu sein scheinen. Die folgenden Werke:
MaxLength="{x:Static ac:Constants.MAX_PORT_LENGTH}"
Aber nicht dies:
MaxLength="{x:Static ac:Constants.ServerConstraints.MAX_PORT_LENGTH}"
Nein, es gibt keine fehlenden '}'. Wenn ich ein statisches Feld auf der ersten Ebene verwende (z. B. 'MaxLength =" {x: Static ac: Constants.MAX_PORT_LENGTH} "), funktioniert es. Wenn das konstante Feld in einer inneren statischen Klasse ist, funktioniert es nicht mehr (z.B.: MaxLength = "{x: Static ac: Constants.Lengths.MAX_PORT_LENGTH}"). – WebDucer