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

时间:2014-10-11 09:09:25

标签: c# windows scope store

我有这个代码(问题行上面有注释):

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

我一直有C#的问题。我不知道这意味着什么。但我确信在这个文件的.xaml中,存在这两个文本框。

  • 这是一个Windows Store C#程序。

编辑:

这是输出:

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'

2 个答案:

答案 0 :(得分:0)

检查x:Class页面上的xaml是否具有.cs课程的确切名称。

应该是这样的:

x:Class = "ProjectName.C#ClassName"

答案 1 :(得分:0)

我猜您在以下行中忘记了两次=运算符:

if (txt_adminId.Text = "root")

应该是

if (txt_adminId.Text == "root")

也在这一行

if (txt_adminPw.Password = "password")

所以,你可能想写这个:

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

此外,我建议您使用String.Equals代替==,因为您希望比较值而不是引用。请注意,String.Equals会比较正确的值,而==也会考虑其参考值。

请参阅:Why would you use String.Equals over ==?