2014-10-11 56 views
1

我有这样的代码(问题线的上方都有注释):组件并不在当前的背景下存在 - C#

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' 
+0

为什么您的Click处理器标记为异步? – 2014-10-11 09:24:00

+0

使MessageDialog工作。我尝试删除它们,但它不能解决问题。 – Wix 2014-10-11 09:26:09

+0

我明白了。没有WP的经验,但在我看来与异步和线程有关。也许使用MVVM可以帮助你。 – 2014-10-11 09:39:45

回答

0

检查x:Classxaml页都有你的.cs类的确切名称。

它应该是这样的:

x:Class = "ProjectName.C#ClassName" 
+0

他们完全一样。 – Wix 2014-10-11 09:21:14

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

相关问题