【问题标题】:How to show display alert message using HandleTextChanged event?如何使用 HandleTextChanged 事件显示显示警报消息?
【发布时间】:2017-09-10 03:53:05
【问题描述】:

我创建了两个用户输入字段,即用户名和密码。

这是我的 Xaml 代码:

 <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:LoginUser"
                 x:Class="LoginUser.MainPage">
      <StackLayout Spacing="20" Padding="50" VerticalOptions="Center">
        <Entry x:Name="txtUsername"  Placeholder="Username"></Entry>
        <Entry x:Name="txtPassword" Placeholder="Password" IsPassword="True">
          <Entry.Behaviors>
            <local:ValidationBehavior/>
          </Entry.Behaviors>
        </Entry>
        <Button Text="Log In" TextColor="White" BackgroundColor="##ff77D065" Clicked="Button_Onclick"></Button>
      </StackLayout>
    </ContentPage>

这是我的 ValidationBehaviour.cs 文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Text.RegularExpressions;
using UIKit;

namespace LoginUser
{
    public class ValidationBehavior: Behavior<Entry>
    {
        const string pwRegex = @"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";
        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += HandleTextChanged;
            base.OnAttachedTo(bindable);
        }

        void HandleTextChanged(object sender, TextChangedEventArgs e)
        {

            bool IsValid = false;
            IsValid = (Regex.IsMatch(e.NewTextValue, pwRegex));
            ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;

        }
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= HandleTextChanged;
            base.OnDetachingFrom(bindable);

        }

    }
}

如果从文本框中删除任何字符串字符,如小写、大写、特殊字符,我想显示显示警报。

【问题讨论】:

  • 这与您之前的问题有何不同?您不了解如何使用 DisplayAlert?或者您是在问如何检测文本是否已更改?
  • 例如,我在密码字段中提供了 App@1234 之类的密码,所以我的问题是如果我从密码字段中删除字符串 char,例如“A”,那么应该会发生错误。跨度>
  • TextChangedEventArgs 为您提供新值和旧值。您需要比较它们并提出符合您要求的算法来测试错误。我们无法为您做到这一点。
  • 这是你第三次问基本相同的问题了。

标签: c# xamarin.forms


【解决方案1】:

您可以通过静态App.CurrentMainPage 属性从Xamarin.Forms 项目中的任何位置显示警报,例如

await App.Current.MainPage.DisplayAlert("Test Title", "Test", "OK");

如果您想在HandleTextChanged 方法中调用警报,您必须先添加async 字样,如下所示:

async void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        IsValid = (Regex.IsMatch(e.NewTextValue, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
        await App.Current.MainPage.DisplayAlert("Test Title", "Test", "OK");
        ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
    }

我不知道显示警报是否是个好主意,也许它会在Entry 下方显示Label

更多信息请查看example in github

还有this tutorial,它解释了如何将绑定与行为一起使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2021-08-11
    • 2014-02-21
    • 1970-01-01
    • 2019-05-23
    • 2016-04-04
    相关资源
    最近更新 更多