This commit is contained in:
Bart Lannoeye 2017-01-13 20:45:53 +01:00
Родитель afae7ab0a5
Коммит 61e0f039ca
8 изменённых файлов: 566 добавлений и 566 удалений

Просмотреть файл

@ -5,119 +5,119 @@ using Xamarin.Forms;
namespace Behaviors.Tests
{
[TestFixture]
public class EntryEmailValidationTests
{
[TestFixture]
public class EntryEmailValidationTests
{
EntryEmailValidation behavior;
Entry entry;
Color textColor;
EntryEmailValidation behavior;
Entry entry;
Color textColor;
[SetUp]
public void Setup()
{
behavior = new EntryEmailValidation();
behavior.TextColorInvalid = Color.Red;
entry = new Entry();
entry.TextColor = Color.Green;
[SetUp]
public void Setup()
{
behavior = new EntryEmailValidation();
behavior.TextColorInvalid = Color.Red;
entry = new Entry();
entry.TextColor = Color.Green;
textColor = entry.TextColor;
entry.Behaviors.Add(behavior);
}
textColor = entry.TextColor;
entry.Behaviors.Add(behavior);
}
[Test]
public void ValidEmail()
{
var quote = "\"";
var emails = new []
{
@"email@example.com",
@"firstname.lastname@example.com",
@"email@subdomain.example.com",
@"firstname+lastname@example.com",
@"email@123.123.123.123",
@"email@[123.123.123.123]",
$"{quote}email{quote}@example.com",
@"1234567890@example.com",
@"email@example-one.com",
//@"_______@example.com",
@"email@example.name",
@"email@example.museum",
@"email@example.co.jp",
@"firstname-lastname@example.com"
};
foreach (var email in emails)
{
entry.Text = email;
[Test]
public void ValidEmail()
{
var quote = "\"";
var emails = new []
{
@"email@example.com",
@"firstname.lastname@example.com",
@"email@subdomain.example.com",
@"firstname+lastname@example.com",
@"email@123.123.123.123",
@"email@[123.123.123.123]",
$"{quote}email{quote}@example.com",
@"1234567890@example.com",
@"email@example-one.com",
//@"_______@example.com",
@"email@example.name",
@"email@example.museum",
@"email@example.co.jp",
@"firstname-lastname@example.com"
};
foreach (var email in emails)
{
entry.Text = email;
Assert.IsTrue(behavior.IsValid, $"Value should be valid: {email}, but is not.");
Assert.IsTrue(behavior.IsValid, $"Value should be valid: {email}, but is not.");
Assert.AreEqual(entry.TextColor, textColor, "Color was changed, but shouldn't have.");
}
}
Assert.AreEqual(entry.TextColor, textColor, "Color was changed, but shouldn't have.");
}
}
[Test]
public void InvalidEmail()
{
var emails = new[]
{
@"plainaddress",
@"#@%^%#$@#$@#.com",
@"@example.com",
@"Joe Smith <email@example.com>",
@"email.example.com",
@"email@example@example.com",
@".email@example.com",
@"email.@example.com",
@"email..email@example.com",
@"あいうえお@example.com",
@"email@example.com (Joe Smith)",
@"email@example",
@"email@-example.com",
//@"email@example.web",
//@"email@111.222.333.44444",
@"email@example..com",
@"Abc..123@example.com",
};
foreach (var email in emails)
{
entry.Text = email;
[Test]
public void InvalidEmail()
{
var emails = new[]
{
@"plainaddress",
@"#@%^%#$@#$@#.com",
@"@example.com",
@"Joe Smith <email@example.com>",
@"email.example.com",
@"email@example@example.com",
@".email@example.com",
@"email.@example.com",
@"email..email@example.com",
@"あいうえお@example.com",
@"email@example.com (Joe Smith)",
@"email@example",
@"email@-example.com",
//@"email@example.web",
//@"email@111.222.333.44444",
@"email@example..com",
@"Abc..123@example.com",
};
foreach (var email in emails)
{
entry.Text = email;
Assert.IsFalse(behavior.IsValid, $"Value should be not valid: {email}, but is.");
Assert.IsFalse(behavior.IsValid, $"Value should be not valid: {email}, but is.");
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
[Test]
public void InvalidEmailOdd()
{
var emails = new[]
{
@"(),:;<>[\]@example.com",
@"just""not""right@example.com",
@"this\ is""really""not\allowed@example.com"
};
foreach (var email in emails)
{
entry.Text = email;
[Test]
public void InvalidEmailOdd()
{
var emails = new[]
{
@"(),:;<>[\]@example.com",
@"just""not""right@example.com",
@"this\ is""really""not\allowed@example.com"
};
foreach (var email in emails)
{
entry.Text = email;
Assert.IsFalse(behavior.IsValid, $"Value should be not valid: {email}, but is.");
Assert.IsFalse(behavior.IsValid, $"Value should be not valid: {email}, but is.");
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
[Test]
public void NoText()
{
entry.Text = string.Empty;
[Test]
public void NoText()
{
entry.Text = string.Empty;
Assert.IsFalse(behavior.IsValid, "Value should be not valid, but is.");
Assert.IsFalse(behavior.IsValid, "Value should be not valid, but is.");
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not changed, but should have.");
}
}
}

Просмотреть файл

@ -9,43 +9,43 @@ using Xamarin.Forms;
namespace Behaviors.Tests
{
[TestFixture]
public class EntryEmptyValidationTests
{
EntryEmptyValidation behavior;
Entry entry;
Color textColor;
[TestFixture]
public class EntryEmptyValidationTests
{
EntryEmptyValidation behavior;
Entry entry;
Color textColor;
[SetUp]
public void Setup()
{
behavior = new EntryEmptyValidation();
behavior.TextColorInvalid = Color.Red;
entry = new Entry();
entry.TextColor = Color.Green;
[SetUp]
public void Setup()
{
behavior = new EntryEmptyValidation();
behavior.TextColorInvalid = Color.Red;
entry = new Entry();
entry.TextColor = Color.Green;
textColor = entry.TextColor;
entry.Behaviors.Add(behavior);
}
textColor = entry.TextColor;
entry.Behaviors.Add(behavior);
}
[Test]
public void HasText()
{
entry.Text = "Hello World";
[Test]
public void HasText()
{
entry.Text = "Hello World";
Assert.IsTrue(behavior.IsValid, "Value should be valid, but is not.");
Assert.IsTrue(behavior.IsValid, "Value should be valid, but is not.");
Assert.AreEqual(entry.TextColor, textColor, "Color was changed, but shouldn't have.");
}
Assert.AreEqual(entry.TextColor, textColor, "Color was changed, but shouldn't have.");
}
[Test]
public void NoText()
{
entry.Text = string.Empty;
[Test]
public void NoText()
{
entry.Text = string.Empty;
Assert.IsFalse(behavior.IsValid, "Value should not be valid, but is.");
Assert.IsFalse(behavior.IsValid, "Value should not be valid, but is.");
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not set to invalid text color.");
}
Assert.AreEqual(entry.TextColor, behavior.TextColorInvalid, "Color was not set to invalid text color.");
}
}
}

Просмотреть файл

@ -4,74 +4,74 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
public class BaseBehavior<T> : Behavior<T> where T : BindableObject
{
public class BaseBehavior<T> : Behavior<T> where T : BindableObject
{
public T AssociatedObject { get; private set; }
public T AssociatedObject { get; private set; }
public static readonly BindableProperty AttachBehaviorProperty =
BindableProperty.CreateAttached("AttachBehavior",
typeof(bool),
typeof(BaseBehavior<T>),
false, propertyChanged: OnAttachBehaviorChanged);
public static readonly BindableProperty AttachBehaviorProperty =
BindableProperty.CreateAttached("AttachBehavior",
typeof(bool),
typeof(BaseBehavior<T>),
false, propertyChanged: OnAttachBehaviorChanged);
public static bool GetAttachBehavior(T view)
{
return (bool)view.GetValue(AttachBehaviorProperty);
}
public static bool GetAttachBehavior(T view)
{
return (bool)view.GetValue(AttachBehaviorProperty);
}
public static void SetAttachBehavior(T view, bool value)
{
view.SetValue(AttachBehaviorProperty, value);
}
public static void SetAttachBehavior(T view, bool value)
{
view.SetValue(AttachBehaviorProperty, value);
}
static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
{
var control = view as Xamarin.Forms.View;
bool attachBehavior = (bool)newValue;
if (attachBehavior)
{
control.Behaviors.Add(new BaseBehavior<T>());
}
else
{
var toRemove = control.Behaviors.FirstOrDefault(b => b is BaseBehavior<T>);
if (toRemove != null)
{
control.Behaviors.Remove(toRemove);
}
}
}
static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
{
var control = view as Xamarin.Forms.View;
bool attachBehavior = (bool)newValue;
if (attachBehavior)
{
control.Behaviors.Add(new BaseBehavior<T>());
}
else
{
var toRemove = control.Behaviors.FirstOrDefault(b => b is BaseBehavior<T>);
if (toRemove != null)
{
control.Behaviors.Remove(toRemove);
}
}
}
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null)
{
BindingContext = bindable.BindingContext;
}
if (bindable.BindingContext != null)
{
BindingContext = bindable.BindingContext;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
}

Просмотреть файл

@ -4,98 +4,98 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
/// <summary>
/// Email validator behavior.
/// </summary>
public class EntryEmailValidation : BaseBehavior<Entry>
{
bool colorSet;
Color color = Color.Default;
/// <summary>
/// Email validator behavior.
/// </summary>
public class EntryEmailValidation : BaseBehavior<Entry>
{
bool colorSet;
Color color = Color.Default;
const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
static readonly BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmailValidation), Color.Default);
static readonly BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmailValidation), Color.Default);
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
static readonly BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
static readonly BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
IsValid = (Regex.IsMatch(text, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
IsValid = (Regex.IsMatch(text, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
var entry = sender as Entry;
var entry = sender as Entry;
if (entry == null)
return;
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
if (entry == null)
return;
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
}
}

Просмотреть файл

@ -3,95 +3,95 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
/// <summary>
/// Empty validator behavior.
/// </summary>
public class EntryEmptyValidation : BaseBehavior<Entry>
{
/// <summary>
/// Empty validator behavior.
/// </summary>
public class EntryEmptyValidation : BaseBehavior<Entry>
{
bool colorSet;
Color color = Color.Default;
bool colorSet;
Color color = Color.Default;
static BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmptyValidation), Color.Default);
static BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmptyValidation), Color.Default);
/// <summary>
/// The is valid property.
/// </summary>
public static BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
static BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
static BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
/// <summary>
/// The is valid property.
/// </summary>
public static BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)GetValue(IsValidProperty); }
private set { SetValue(IsValidPropertyKey, value); }
}
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)GetValue(IsValidProperty); }
private set { SetValue(IsValidPropertyKey, value); }
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
IsValid = !string.IsNullOrWhiteSpace(text);
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
IsValid = !string.IsNullOrWhiteSpace(text);
var entry = sender as Entry;
var entry = sender as Entry;
if (entry == null)
return;
if (entry == null)
return;
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
}
}
}

Просмотреть файл

@ -4,97 +4,97 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
public class NumericValidationBehavior : BaseBehavior<Entry>
{
bool colorSet;
Color color = Color.Default;
public class NumericValidationBehavior : BaseBehavior<Entry>
{
bool colorSet;
Color color = Color.Default;
static readonly BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmailValidation), Color.Default);
static readonly BindablePropertyKey TextColorInvalidKey =
BindableProperty.CreateReadOnly(nameof(TextColorInvalid), typeof(Color),
typeof(EntryEmailValidation), Color.Default);
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty TextColorInvalidProperty =
TextColorInvalidKey.BindableProperty;
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
/// <summary>
/// Gets or sets the text color invalid.
/// </summary>
/// <value>The text color invalid.</value>
public Color TextColorInvalid
{
get { return (Color)GetValue(TextColorInvalidProperty); }
set { SetValue(TextColorInvalidKey, value); }
}
static readonly BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
static readonly BindablePropertyKey IsValidPropertyKey =
BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool),
typeof(EntryEmptyValidation), false);
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// The is valid property.
/// </summary>
public static readonly BindableProperty IsValidProperty =
IsValidPropertyKey.BindableProperty;
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get { return (bool)base.GetValue(IsValidProperty); }
private set { base.SetValue(IsValidPropertyKey, value); }
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the attached to event.
/// </summary>
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
HandleTextChanged(bindable, new TextChangedEventArgs(string.Empty, bindable.Text));
base.OnAttachedTo(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
/// <param name="bindable">To be added.</param>
/// <summary>
/// Raises the detaching from event.
/// </summary>
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
var text = e?.NewTextValue ?? string.Empty;
double result;
IsValid = double.TryParse(text, out result);
double result;
IsValid = double.TryParse(text, out result);
var entry = sender as Entry;
var entry = sender as Entry;
if (entry == null)
return;
if (entry == null)
return;
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
if (!colorSet)
{
colorSet = true;
color = entry.TextColor;
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
entry.TextColor = IsValid ? color : TextColorInvalid;
}
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
}
}
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
}
}
}

Просмотреть файл

@ -3,63 +3,63 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
public class AddEffect : Behavior<View>
{
public static readonly BindableProperty GroupProperty =
BindableProperty.Create(nameof(Group), typeof(string), typeof(AddEffect), null);
public class AddEffect : Behavior<View>
{
public static readonly BindableProperty GroupProperty =
BindableProperty.Create(nameof(Group), typeof(string), typeof(AddEffect), null);
public static readonly BindableProperty NameProperty =
BindableProperty.Create(nameof(Name), typeof(string), typeof(AddEffect), null);
public static readonly BindableProperty NameProperty =
BindableProperty.Create(nameof(Name), typeof(string), typeof(AddEffect), null);
public string Group
{
get { return (string)GetValue(GroupProperty); }
set { SetValue(GroupProperty, value); }
}
public string Group
{
get { return (string)GetValue(GroupProperty); }
set { SetValue(GroupProperty, value); }
}
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
protected override void OnAttachedTo(BindableObject bindable)
{
base.OnAttachedTo(bindable);
EffectAdd(bindable as View);
}
protected override void OnAttachedTo(BindableObject bindable)
{
base.OnAttachedTo(bindable);
EffectAdd(bindable as View);
}
protected override void OnDetachingFrom(BindableObject bindable)
{
EffectRemove(bindable as View);
base.OnDetachingFrom(bindable);
}
protected override void OnDetachingFrom(BindableObject bindable)
{
EffectRemove(bindable as View);
base.OnDetachingFrom(bindable);
}
void EffectAdd(View view)
{
var effect = GetEffect();
if (effect == null || view == null)
return;
void EffectAdd(View view)
{
var effect = GetEffect();
if (effect == null || view == null)
return;
view.Effects.Add(effect);
}
view.Effects.Add(effect);
}
void EffectRemove(View view)
{
var effect = GetEffect();
if (effect == null || view == null)
return;
void EffectRemove(View view)
{
var effect = GetEffect();
if (effect == null || view == null)
return;
view.Effects.Remove(effect);
}
view.Effects.Remove(effect);
}
Effect GetEffect()
{
if (!string.IsNullOrWhiteSpace(Group) && !string.IsNullOrWhiteSpace(Name))
{
return Effect.Resolve(string.Format("{0}.{1}", Group, Name));
}
return null;
}
}
Effect GetEffect()
{
if (!string.IsNullOrWhiteSpace(Group) && !string.IsNullOrWhiteSpace(Name))
{
return Effect.Resolve(string.Format("{0}.{1}", Group, Name));
}
return null;
}
}
}

Просмотреть файл

@ -5,125 +5,125 @@ using Xamarin.Forms;
namespace FormsCommunityToolkit.Behaviors
{
public class EventToCommand : BaseBehavior<View>
{
Delegate eventHandler;
public class EventToCommand : BaseBehavior<View>
{
Delegate eventHandler;
public static readonly BindableProperty EventNameProperty =
BindableProperty.Create(nameof(EventName), typeof(string), typeof(EventToCommand), null, propertyChanged: OnEventNameChanged);
public static readonly BindableProperty EventNameProperty =
BindableProperty.Create(nameof(EventName), typeof(string), typeof(EventToCommand), null, propertyChanged: OnEventNameChanged);
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(EventToCommand), null);
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(EventToCommand), null);
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(EventToCommand), null);
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(EventToCommand), null);
public static readonly BindableProperty InputConverterProperty =
BindableProperty.Create(nameof(Converter), typeof(IValueConverter), typeof(EventToCommand), null);
public static readonly BindableProperty InputConverterProperty =
BindableProperty.Create(nameof(Converter), typeof(IValueConverter), typeof(EventToCommand), null);
public string EventName
{
get { return (string)GetValue(EventNameProperty); }
set { SetValue(EventNameProperty, value); }
}
public string EventName
{
get { return (string)GetValue(EventNameProperty); }
set { SetValue(EventNameProperty, value); }
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public IValueConverter Converter
{
get { return (IValueConverter)GetValue(InputConverterProperty); }
set { SetValue(InputConverterProperty, value); }
}
public IValueConverter Converter
{
get { return (IValueConverter)GetValue(InputConverterProperty); }
set { SetValue(InputConverterProperty, value); }
}
protected override void OnAttachedTo(View bindable)
{
base.OnAttachedTo(bindable);
RegisterEvent(EventName);
}
protected override void OnAttachedTo(View bindable)
{
base.OnAttachedTo(bindable);
RegisterEvent(EventName);
}
protected override void OnDetachingFrom(View bindable)
{
DeregisterEvent(EventName);
base.OnDetachingFrom(bindable);
}
protected override void OnDetachingFrom(View bindable)
{
DeregisterEvent(EventName);
base.OnDetachingFrom(bindable);
}
void RegisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name))
return;
void RegisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name))
return;
var eventInfo = AssociatedObject?.GetType()?.GetRuntimeEvent(name);
if (eventInfo == null)
{
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
}
var eventInfo = AssociatedObject?.GetType()?.GetRuntimeEvent(name);
if (eventInfo == null)
{
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't register the '{0}' event.", EventName));
}
var methodInfo = typeof(EventToCommand).GetTypeInfo().GetDeclaredMethod("OnEvent");
eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler(AssociatedObject, eventHandler);
}
var methodInfo = typeof(EventToCommand).GetTypeInfo().GetDeclaredMethod("OnEvent");
eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler(AssociatedObject, eventHandler);
}
void DeregisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name) || eventHandler == null)
return;
void DeregisterEvent(string name)
{
if (string.IsNullOrWhiteSpace(name) || eventHandler == null)
return;
var eventInfo = AssociatedObject?.GetType()?.GetRuntimeEvent(name);
if (eventInfo == null)
{
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
}
var eventInfo = AssociatedObject?.GetType()?.GetRuntimeEvent(name);
if (eventInfo == null)
{
throw new ArgumentException(string.Format("EventToCommandBehavior: Can't de-register the '{0}' event.", EventName));
}
eventInfo.RemoveEventHandler(AssociatedObject, eventHandler);
eventHandler = null;
}
eventInfo.RemoveEventHandler(AssociatedObject, eventHandler);
eventHandler = null;
}
void OnEvent(object sender, object eventArgs)
{
if (Command == null)
return;
void OnEvent(object sender, object eventArgs)
{
if (Command == null)
return;
object resolvedParameter;
if (CommandParameter != null)
{
resolvedParameter = CommandParameter;
}
else if (Converter != null)
{
resolvedParameter = Converter.Convert(eventArgs, typeof(object), null, null);
}
else
{
resolvedParameter = eventArgs;
}
object resolvedParameter;
if (CommandParameter != null)
{
resolvedParameter = CommandParameter;
}
else if (Converter != null)
{
resolvedParameter = Converter.Convert(eventArgs, typeof(object), null, null);
}
else
{
resolvedParameter = eventArgs;
}
if (Command.CanExecute(resolvedParameter))
{
Command.Execute(resolvedParameter);
}
}
if (Command.CanExecute(resolvedParameter))
{
Command.Execute(resolvedParameter);
}
}
static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
{
var behavior = bindable as EventToCommand;
if (behavior?.AssociatedObject == null)
return;
static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
{
var behavior = bindable as EventToCommand;
if (behavior?.AssociatedObject == null)
return;
var oldEventName = (string)oldValue;
var newEventName = (string)newValue;
var oldEventName = (string)oldValue;
var newEventName = (string)newValue;
behavior.DeregisterEvent(oldEventName);
behavior.RegisterEvent(newEventName);
}
}
behavior.DeregisterEvent(oldEventName);
behavior.RegisterEvent(newEventName);
}
}
}