2017-05-02 110 views
0

爲什麼WPF中的消息框沒有顯示? 我沒有任何錯誤。 來自XAML文件的名稱是正確的,所以我不知道發生了什麼。爲什麼MessageBox沒有顯示?

請幫幫我吧! 我不知道我應該改變什麼。 感謝您的幫助!

XAML:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Label x:Name="label" Content="App" HorizontalAlignment="Left" Margin="214,0,0,0" VerticalAlignment="Top" Height="33" Width="89"/> 
     <TextBox x:Name="loginBox" HorizontalAlignment="Left" Height="23" Margin="214,54,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> 
     <PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Margin="214,92,0,0" VerticalAlignment="Top" Width="120"/> 
     <Label x:Name="log" Content="Login:" HorizontalAlignment="Left" Margin="167,51,0,0" VerticalAlignment="Top"/> 
     <Label x:Name="pass" Content="Password:" HorizontalAlignment="Left" Margin="146,86,0,0" VerticalAlignment="Top"/> 
     <Button x:Name="log_in" Content="LOG IN" HorizontalAlignment="Left" Margin="279,117,0,0" VerticalAlignment="Top" Width="75"/> 

    </Grid> 
</Window> 

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication2 
{ 

    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void log_in_Click(object sender, RoutedEventArgs e) 
     { 

      string login = loginBox.Text; 
      string password = passwordBox.Password; 

      if(check(login, password)){ 
       MessageBox.Show("Welcome!"); 
      } 
      else 
      { 
       MessageBox.Show("Wrong login or password!"); 
      } 

     } 

     public bool check(string login, string password) 
     { 
      if(login == "admin" && password == "123") 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

    } 
} 
+1

如何調用'log_in_Click'?我們可以看到XAML嗎? –

+0

https://pastebin.com/09SZhS3n –

+0

請提出問題。 –

回答

0

你永遠不會調用log_in_Click。 WPF不會從名稱中猜出您想要將其與按鈕關聯。您必須明確地將其設置爲按鈕的點擊處理程序,如下所示:

<Button 
    Click="log_in_Click" 

    x:Name="log_in" 
    Content="LOG IN" 
    HorizontalAlignment="Left" 
    Margin="279,117,0,0" 
    VerticalAlignment="Top" 
    Width="75" 
    />