You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

XAML登录表单验证异常:输入正确账号密码仍提示错误

Troubleshooting Your XAML Login Form Issue

Hey there, let's walk through why your login is failing even when you enter the correct credentials! It's super common for new devs to run into small quirks like this, so let's break down the most likely fixes:

1. Trim Unwanted Whitespace from Inputs

The biggest culprit here is probably accidental extra spaces in your email/password input. Your code does a strict equality check (==) against exact strings, but if you accidentally add a space before/after the email or password (even without noticing), the check will fail immediately.

Fix this by using the Trim() method to strip leading/trailing whitespace from both inputs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Trim whitespace to eliminate accidental spaces causing mismatches
    string email = txtEmail.Text.Trim();
    string password = txtPass.Text.Trim();

    if(email == "test@test.com" && password == "test") 
    {
        mainWindow.mainFrame.Navigate(new Uri("/pages/dashboard.xaml", UriKind.RelativeOrAbsolute));
        MessageBox.Show("Successfully Logged in");
    } 
    else 
    {
        MessageBox.Show("Email/Password is incorrect, try again.");
        txtPass.Text = ""; // Use empty string instead of a space to avoid leftover characters
        txtEmail.Text = "";
        txtEmail.Focus();
    }
}

I also adjusted txtPass.Text = " "; to txtPass.Text = ""—setting it to a space would leave a hidden blank character in the input box, which could cause issues if you start typing without deleting it first.

2. Double-Check Your Textbox Names Match

Make sure the x:Name values of your XAML textboxes exactly match txtEmail and txtPass. A tiny typo (like txtEamil or txtPassword) would mean your code is reading from the wrong control (or a non-existent one, leading to empty text values that fail the check).

Your XAML should look something like this:

<TextBox x:Name="txtEmail" PlaceholderText="Enter your email" />
<TextBox x:Name="txtPass" PlaceholderText="Enter your password" PasswordChar="*" />

3. Debug to See What's Actually Being Read

If the above fixes don't work, add a debug line to peek at exactly what values your code is pulling from the textboxes. This will help you spot if the input isn't being captured correctly:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Print input values to the Output window for debugging
    System.Diagnostics.Debug.WriteLine($"Email entered: '{txtEmail.Text}', Password entered: '{txtPass.Text}'");

    string email = txtEmail.Text.Trim();
    string password = txtPass.Text.Trim();

    // Rest of your login logic...
}

Open the Output window in Visual Studio (Debug > Windows > Output) and check the printed values when you click the login button. If you see empty strings or unexpected text, you'll know exactly where the problem lies.

Give these steps a try—chances are the whitespace issue is the main culprit here!

内容的提问来源于stack exchange,提问作者Raihan Kusmanto

火山引擎 最新活动