Xamarin Forms "TextChanged" 事件到 MVVM?

Xamarin Forms "TextChanged" event to MVVM?

我对 C#、Xamarin Forms 和一般编码完全陌生。我尝试了以下教程和 Microsoft 文档。但是,还是有些东西我真的好像得不到。我在 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>

如您所见,没有数据绑定(我似乎找不到正确的方法)。所以我将文本更改事件放在 .cs 文件(视图)中:

       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();
        }

不要介意糟糕的编码(我完全是初学者)。该代码对我来说效果很好。每次用户在输入字段中输入内容时,它都会分析输入,删除无效字符,并在我想要的时候插入破折号。我几次利用 e.OldTextValue + NewValue 和 disabling/enabling 接受按钮直接用代码。我确实想了解执行此操作的 MVVM 方式。据我所知,这样做的方法是 ICommand?但是我怎么能像事件监听器那样对“文本更改”做出反应呢?这一切将如何在 ViewModel 中设置?如果输入字段可以“数据绑定”,我还能使用 e.XXX 方法 + 执行 Try catch 吗?

试试“EventToCommandBehavior”。

The EventToCommandBehavior is a behavior that allows the user to invoke a Command through an event.

还有两个选项:

  1. 自己编码一个:
    检查 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

  2. 取自Xamarin.CommunityToolkit:
    安装 XCT NuGet for your solution and implement it like eventtocommandbehavior.
    (Xamarin.CommunityToolkit官方团队发布,社区支持。)

//sample code in xaml
<Entry ...>
    <Entry.Behaviors>
        <xct:EventToCommandBehavior
            EventName="TextChanged"
            Command="{Binding MVVMCommand}" />
    </Entry.Behaviors>
</Entry>