【问题标题】:Component does not exist in the current context - C#当前上下文中不存在组件 - C#
【发布时间】:2014-12-06 10:11:07
【问题描述】:

我有这段代码(问题行上面有 cmets):

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 应用商店 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'

【问题讨论】:

  • 为什么您的 Click 处理程序被标记为异步?
  • 使 MessageDialog 工作。我尝试删除它们,但没有解决问题。
  • 我明白了。没有使用 WP 的经验,但在我看来与异步和线程有关。在这里使用 MVVM 可能会对您有所帮助。
  • 我听到很多。但我不知道如何开始。
  • @Wix 你确定你有一张有效的支票txt_adminId.Text = "root" 吗?不应该是==吗?在这条线上同样if (txt_adminPw.Password = "password")

标签: c# windows scope store


【解决方案1】:

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

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 ==?

【讨论】:

    【解决方案2】:

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

    应该是这样的:

    x:Class = "ProjectName.C#ClassName"
    

    【讨论】:

    • 它们完全一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多