-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ValidationExample.xaml.cs
65 lines (52 loc) · 1.49 KB
/
ValidationExample.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using Xamarin.Forms.Xaml;
namespace Xamarin.Forms.Controls.GalleryPages.VisualStateManagerGalleries
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ValidationExample : ContentPage
{
public ValidationExample ()
{
InitializeComponent ();
Message.TextChanged += EditorTextChanged;
}
void EditorTextChanged(object sender, TextChangedEventArgs e)
{
var count = e.NewTextValue.Length;
CharacterCount.Text = $"{count} characters";
CheckMessageValid();
}
bool IsValid()
{
var messageValid = CheckMessageValid();
var subjectValid = CheckSubjectValid();
return messageValid && subjectValid;
}
bool CheckMessageValid()
{
var isValid = Message.Text == null || Message.Text.Length <= 40;
var state = isValid ? "Normal" : "Invalid";
VisualStateManager.GoToState(Message, state);
VisualStateManager.GoToState(MessageError, state);
VisualStateManager.GoToState(CharacterCount, state);
VisualStateManager.GoToState(MessageLabel, state);
return isValid;
}
bool CheckSubjectValid()
{
var isValid = Subject.Text?.Length > 0;
var state = isValid ? "Normal" : "Invalid";
VisualStateManager.GoToState(Subject, state);
VisualStateManager.GoToState(SubjectError, state);
VisualStateManager.GoToState(SubjectLabel, state);
return isValid;
}
void Submit_OnClicked(object sender, EventArgs e)
{
if (IsValid())
{
DisplayAlert("Submitted", "Thank you for submitting", "OK");
}
}
}
}