【问题标题】:Change IsEnabled & IsVisible on Password Confirmation in Xamarin在 Xamarin 中更改密码确认时的 IsEnabled 和 IsVisible
【发布时间】:2021-02-24 08:28:01
【问题描述】:

在我的 Xamarin 应用程序中,我尝试使用更改 IsVisibleFrameIsEnabledButton,当密码被确认时(密码和确认密码应该有相同的文本),但是它没有改变任何东西。

具有讽刺意味的是,当应用程序打开时,默认情况下(没有输入任何值),Password.Text == ConfirmPassword.Text 为 True。

当两个Entry 字段的值相同时,我想更改它。谢谢。

.xml 代码

<Entry
    x:Name="Password"
    IsPassword="True"
    Keyboard="Numeric"
    MaxLength="8"
    ReturnType="Next" />
                        
<Entry
    x:Name="ConfirmPassword"
    IsPassword="True"
    Keyboard="Numeric"
    MaxLength="8"
    ReturnType="Done" />


<Frame x:Name="RedBar" BackgroundColor="#E1444D" IsVisible="true">
    <BoxView />
</Frame>
                
<Frame x:Name="GreenBar" BackgroundColor="#24D27F" IsVisible="false">
    <BoxView />
</Frame>


<Button
    x:Name="PasswordButton"
    IsEnabled="False"
    Text="Submit">
</Button>

.xml.cs 代码

public PasswordPage()
{
    InitializeComponent();

    if (Password.Text == ConfirmPassword.Text)
    {
        RedBar.IsVisible = false;
        GreenBar.IsVisible = true;
        PasswordButton.IsEnabled = true;
    }

    else
    {
        RedBar.IsVisible = true;
        GreenBar.IsVisible = false;
        PasswordButton.IsEnabled = false;
    }
}

【问题讨论】:

  • 您有一个未定义的命名空间“x”。
  • @jdweng 感谢您的回复。当我使用Name 时,它说,找不到属性“名称”...并且它没有为x:Name 返回任何错误,我认为它是在.xml 页面中的页面x:Class="Osma.Mobile.App.Views.PasswordPage",我说的对吗?
  • t定义命名空间的地方会有xmlns:x="URL"
  • 您需要为您的 if 添加一个条件,检查 !string.IsNullOrWhite(Password)
  • 最初,当您在两个条目中启动应用程序时,有一个空格,密码和确认密码都相同,因此您编写的条件变为 true

标签: c# .net xamarin xamarin.forms


【解决方案1】:

Xaml 代码

<Entry
    x:Name="Password"
    IsPassword="True"
    Keyboard="Numeric"
    MaxLength="8"
    ReturnType="Next" Unfocused="Password_Unfocused" />

        <Entry
    x:Name="ConfirmPassword"
    IsPassword="True"
    Keyboard="Numeric"
    MaxLength="8"
    ReturnType="Done" Unfocused="ConfirmPassword_Unfocused"/>


        <Frame x:Name="RedBar" BackgroundColor="#E1444D" IsVisible="true">
            <BoxView />
        </Frame>

        <Frame x:Name="GreenBar" BackgroundColor="#24D27F" IsVisible="false">
            <BoxView />
        </Frame>


        <Button
    x:Name="PasswordButton"
            Clicked="PasswordButton_Clicked"
    IsEnabled="False"
    Text="Submit">
        </Button>

代码背后

public PasswordPage()
{
    InitializeComponent();

    ValidatePassword();
}

private void Password_Unfocused(object sender, FocusEventArgs e)
        {
            ValidatePassword();
        }

        private void ConfirmPassword_Unfocused(object sender, FocusEventArgs e)
        {
            ValidatePassword();
        }

 private void ValidatePassword()
            {
                if (!string.IsNullOrEmpty(Password.Text) && !string.IsNullOrEmpty(ConfirmPassword.Text))
                {
                    if (Password.Text == ConfirmPassword.Text)
                    {
                        RedBar.IsVisible = false;
                        GreenBar.IsVisible = true;
                        PasswordButton.IsEnabled = true;
                    }
    
                    else
                    {
                        RedBar.IsVisible = true;
                        GreenBar.IsVisible = false;
                        PasswordButton.IsEnabled = false;
                    }
                }
                else
                {
                    RedBar.IsVisible = true;
                    GreenBar.IsVisible = false;
                    PasswordButton.IsEnabled = false;
                }
            }

【讨论】:

    【解决方案2】:

    把你的 if 条件改成这个

    if (Password.Text == ConfirmPassword.Text && !string.IsNullOrWhiteSpace(Password.Text))
    {
    }
    

    【讨论】:

    • 感谢您的回复.. !string.IsNullOrWhiteSpace(Password.Text) 没有用,但我使用了string.IsNullOrWhiteSpace(Password.Text) 并且这个语句有效.. 但是它仍然没有改变IsEnabled & @ 的值987654325@,即使PasswordConfirmPassword的文字相同
    • 您需要检查空格和空格,并且您需要(!)不要在该语句之前签名
    • 另一种方式是 Password.Text != " "
    • 我的不好,它起作用了.. 但还有一件事要解决,如果不是 Password.Text == ConfirmPassword.Text,则更改 IsEnabled &amp; IsVisible 的值(事件)
    【解决方案3】:

    使用以下 linq:

           const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                string xml = File.ReadAllText(FILENAME);
                XDocument doc = XDocument.Parse(xml);
    
                List<XElement> entries = doc.Descendants("Entry").ToList();
    
                string name1 = (string)entries[0].Attributes().Where(x => x.Name.LocalName == "Name").FirstOrDefault();
                string name2 = (string)entries[1].Attributes().Where(x => x.Name.LocalName == "Name").FirstOrDefault();
     
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 2020-12-17
      相关资源
      最近更新 更多