简体   繁体   English

组件在当前上下文中不存在-C#

[英]Component does not exist in the current context - C#

I have this code (the problem lines have comments above them): 我有这段代码(问题行上面有注释):

private async void btn_loginAdmin_Click(object sender, RoutedEventArgs e)
        {
            // 'txt_adminId' does not exist in the current context.
            if (txt_adminId.Text = "root")
            {
                // 'txt_adminPw' does not exist in the current context.
                if (txt_adminPw.Password = "password")
                {
                    var msg_login = new MessageDialog("Logged in!");
                    await msg_login.ShowAsync();
                }
                else
                {
                    var msg_login = new MessageDialog("Wrong password!");
                    await msg_login.ShowAsync();
                }
            }
            else
            {
                var msg_login = new MessageDialog("Wrong combo!");
                await msg_login.ShowAsync();
            }
        }

I have always had that problem with C#. 我一直在C#中遇到这个问题。 I don't know what it means. 我不知道这是什么意思 But I'm sure that in this file's .xaml, those 2 text boxes exist. 但我确定在此文件的.xaml中,存在这两个文本框。

  • This is a Windows Store C# program. 这是Windows Store C#程序。

EDIT: 编辑:

Here's the output: 这是输出:

1>C:\Database\GH3_WSE\GH3_WSE\login_admin.xaml.cs(119,17,119,42): error CS0029: Cannot implicitly convert type 'string' to 'bool'
1>C:\Database\GH3_WSE\GH3_WSE\login_admin.xaml.cs(121,21,121,54): error CS0029: Cannot implicitly convert type 'string' to 'bool'

Check the x:Class on xaml page has the exact name of your .cs class. 检查xaml页面上的x:Class是否具有您的.cs类的确切名称。

It should be like: 应该是这样的:

x:Class = "ProjectName.C#ClassName"

I guess you forgot twice the = operator on the following lines: 我猜您在以下几行忘记了两次=运算符:

if (txt_adminId.Text = "root")

should be 应该

if (txt_adminId.Text == "root")

and also on this line 而且在这条线上

if (txt_adminPw.Password = "password")

So, you might want to write this: 因此,您可能需要编写以下代码:

private async void btn_loginAdmin_Click(object sender, RoutedEventArgs e)
        {
            // 'txt_adminId' does not exist in the current context.

            if (txt_adminId.Text == "root")
            {
                // 'txt_adminPw' does not exist in the current context.
                if (txt_adminPw.Password == "password")
                {
                    var msg_login = new MessageDialog("Logged in!");
                    await msg_login.ShowAsync();
                }
                else
                {
                    var msg_login = new MessageDialog("Wrong password!");
                    await msg_login.ShowAsync();
                }
            }
            else
            {
                var msg_login = new MessageDialog("Wrong combo!");
                await msg_login.ShowAsync();
            }
        }

Also, I would recommend to use String.Equals instead of == , because you would like to compare values rather than reference. 另外,我建议使用String.Equals代替== ,因为您想比较值而不是引用。 Note that String.Equals compares proper values while == also takes into consideration their references. 请注意, String.Equals比较正确的值,而==也考虑了它们的引用。

See: Why would you use String.Equals over ==? 请参阅: 为什么要在==上使用String.Equals?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM