【问题标题】:Close first window when i'm using second window using threads in wpf application using c#使用 c# 在 wpf 应用程序中使用线程使用第二个窗口时关闭第一个窗口
【发布时间】:2015-03-26 05:55:55
【问题描述】:

我使用 c# 在 Wpf Applicatoin 中将 MainWindow 作为 LoginWindow。当我单击 Loginbutton 时,它将打开 SecondWindow。现在我想使用线程关闭 MainWindow。请告诉我这个问题的代码。

【问题讨论】:

  • 你试过了吗?
  • 一点JJ都没有stackoverflow.com/help/how-to-ask
  • 不。我不知道该怎么做。
  • 只是一些想法: 1. 让登录对话框成为主窗口可能不是最好的想法。 2. 这肯定不是 threads 是一个好习惯的情况 3. 花点时间阅读这里有关问题的常见问题解答,否则你只会得到反对票。
  • @MarkHall:回复:建议的重复问题。这个问题是关于 WPF 的,而另一个问题肯定是关于 Winforms 的,它的消息循环范式与 WPF 非常不同。这个问题应该因为过于宽泛而关闭,但不能重复 Winforms 问题。

标签: c# wpf


【解决方案1】:

您不需要为此使用线程。将登录窗口作为对话窗口打开。然后根据登录状态,您可以在单击登录按钮时打开下一个窗口。 请找到以下示例并根据您的要求进行必要的更改。我将在后面的代码中显示这一点,如果需要,您可以为 MVVM 更改它。

在 App.xaml.cs 中

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    LoginWindow lw = new LoginWindow();
    bool? rslt = lw.ShowDialog();
    if (rslt == true)
    {
        MainWindow mw = new MainWindow();
        mw.Show();
    }
    else
        this.Shutdown();
}

还将 ShutdownMode="OnExplicitShutdown" 添加到 App.xaml

<Application x:Class="YourApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         ShutdownMode="OnExplicitShutdown">
<Application.Resources>

</Application.Resources>

在您的登录窗口中根据需要添加按钮和文本框

<Window x:Class="YourApp.LoginWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LoginWindow" Height="300" Width="300">
   <Grid>
       <Button x:Name="ButtonLogin" Content="Login" HorizontalAlignment="Left" Height="21" Margin="95,192,0,0" VerticalAlignment="Top" Width="66" RenderTransformOrigin="1.485,0.81" Click="ButtonLogin_Click"/>

    <Button x:Name="ButtonClose" Content="Close" HorizontalAlignment="Left" Height="21" Margin="95,192,0,0" VerticalAlignment="Top" Width="66" RenderTransformOrigin="1.485,0.81" Click="ButtonClose_Click"/>

    </Grid>
</Window>

现在在 LoginWindow.xaml.cs 后面的代码中

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

    private void ButtonLogin_Click(object sender, RoutedEventArgs e)
    {
       //code here for checking login status
       //.........
       if(loginSuccess)
       {
           DialogResult = true;
       }
       else
       {
           DialogResult = false;
       }

       Close();
    }

    private void ButtonClose_Click(object sender, RoutedEventArgs e)
    {
       DialogResult = false;
       Close();
    }
}

然后在 MainWindow.xaml.cs 中连接到 Explicit Shout down

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Closed += MainWindow_Closed;
    }

    void MainWindow_Closed(object sender, EventArgs e)
    {
        App.Current.Shutdown();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多