Reputation: 39
I'm completely new to C#, Xamarin Forms and coding in general. I have tried following tutorials, And Microsofts documentation. However, there is still some things I really can't seem to get. Here I have an Entry in Xaml:
<Entry Placeholder="CPR nummer"
HorizontalOptions="FillAndExpand"
HeightRequest="50"
MinimumHeightRequest="40"
PlaceholderColor="Silver"
Keyboard="Numeric"
TextColor="Gray"
x:Name="CPRnummer"
MaxLength="11"
TextChanged="CPRnummer_TextChanged"
ReturnType="Go">
</Entry>
As you can see, not data binded (I just can't seem to find the right way to do it). So I have the Event for Text Changed placed in the .cs file (View):
private void CPRnummer_TextChanged(object sender, TextChangedEventArgs e)
{
Regex r = new Regex(@"^\d{6}-\d{4}$");
Regex r2 = new Regex(@"^\d{1,6}");
Regex r3 = new Regex(@"^\d{6}-\d{0,4}$");
CPRnummer.Text = Regex.Replace(e.NewTextValue, "[^-0-9]", "");
CPRAccept.IsEnabled = false;
CPRAccept.Opacity = 0.5;
try
{
C1 = e.OldTextValue.Length;
}
catch (NullReferenceException)
{
if (e.OldTextValue == null)
{
C1 = 0;
}
else
{
C1 = e.OldTextValue.Length;
}
}
if (!r.IsMatch(e.NewTextValue))
{
if (e.NewTextValue.Length<7&&!r2.IsMatch(e.NewTextValue))
{
CPRnummer.Text = Regex.Replace(e.NewTextValue, "[^0-9]", "");
}
else if (e.NewTextValue.Length==6&&r2.IsMatch(e.NewTextValue) && C1<e.NewTextValue.Length) {
if (CPRnummer.Text.Length == 6)
{
CPRnummer.Text = e.NewTextValue.Insert(6, "-");
}
}
else if (e.NewTextValue.Length > 6 && !r3.IsMatch(e.NewTextValue) && C1<e.NewTextValue.Length)
{
CPRnummer.Text = Regex.Replace(e.NewTextValue, "[^0-9]", "");
if (CPRnummer.Text.Length > 6)
{
CPRnummer.Text = e.NewTextValue.Insert(6, "-");
}
}
else if (!r.IsMatch(e.NewTextValue) && e.NewTextValue.Length == 11)
{
CPRnummer.Text = "";
}
else if (e.NewTextValue.Length == 10 && e.NewTextValue.All(char.IsDigit))
{
CPRnummer.Text = e.NewTextValue.Insert(6, "-");
}
}
else
{
CPRAccept.IsEnabled = true;
CPRAccept.BackgroundColor = Color.Green;
CPRAccept.Opacity = 1;
CPRAccept.Focus();
}
Don't mind the bad coding (I'm a total beginner). The code works well enough for me. It analyzes the input each time the user enters something in the entry field, and deletes invalid chars, and inserts a dash at the time I want it. I take advantage of the e.OldTextValue + NewValue a few times And disabling/enabling the Accept button directly with code. I do want to understand the MVVM way of doing this. As far as I can read, the way to do it is an ICommand? But how would I be able to react on "text changed" like the event listener is doing? And how would this all be setup in the ViewModel? If the entry field can be "data binded", can I still use the e.XXX methods + do the Try catch?
Upvotes: 0
Views: 2989
Reputation: 929
Try "EventToCommandBehavior".
The EventToCommandBehavior is a behavior that allows the user to invoke a Command through an event.
And two options:
Code one by yourself:
Check the official sample project, or the class folder here. Check the class in "Behaviors" folder, and need extra converters for some events in "Converters". And it is used like this
Take from Xamarin.CommunityToolkit:
Install XCT NuGet for your solution and implement it like eventtocommandbehavior.
(Xamarin.CommunityToolkit is released by the official team and supported by the community.)
//sample code in xaml
<Entry ...>
<Entry.Behaviors>
<xct:EventToCommandBehavior
EventName="TextChanged"
Command="{Binding MVVMCommand}" />
</Entry.Behaviors>
</Entry>
Upvotes: 1