I have MainWindow as LoginWindow in Wpf Applicatoin using c#.When i click on Loginbutton it will open SecondWindow.Now i want to close MainWindow using threads .Please tell me the code for this problem.
Asked
Active
Viewed 949 times
-4
-
Have you tried something? – Rahul Tripathi Mar 26 '15 at 05:57
-
Not even a little JJ http://stackoverflow.com/help/how-to-ask. – Mathemats Mar 26 '15 at 05:58
-
No.I don't know how to do it. – jaya J Mar 26 '15 at 05:58
-
1just a few ideas: 1. maybe making the login-dialog the main-window is not the best idea. 2. This is surely not a case where *threads* are a good practice 3. Take some time and read the FAQ around questions here or you will get nothing but downvotes. – Random Dev Mar 26 '15 at 06:08
-
1@MarkHall: re: the proposed duplicate question. This question is about WPF, while the other question is decidedly about Winforms, which has a very different message loop paradigm than WPF. This question shoulbe closed for being over-broad, but not as a duplicate of the Winforms question. – Peter Duniho Mar 26 '15 at 06:10
1 Answers
0
You do not need to use thread for this. Open the Login Window as a dialog window. Then based on login status you can open the next windows when login button is clicked. Please find the following sample and make necessary changes for your requirements. I'll just show this in code behind, you can change this for MVVM if required.
In 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();
}
Also add ShutdownMode="OnExplicitShutdown" to 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>
In your Login Window add buttons and TextBoxes as required
<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>
Now in code behind 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();
}
}
Then in MainWindow.xaml.cs wire up for 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();
}
}
Dush
- 1,185
- 26
- 29