How can this be done
Login window appears first and if every thing is fine just close login window and open second Main window. in win forms we modify program.cs but in wpf there is no program.cs.
Any solutions.?
Actully i did most of the work in the window that is created By default and now want to make it secondary(mean it should appear and then close when wanted giving control to new window)
<Application x:Class="DevnMark_V1._0.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
var login = new MainWindow();
login.ShowDialog();
if (myAppSett.Default.validated == true)
{
var mainWindow = new DevNMarkMainWindow();
mainWindow.ShowDialog();
}
}
Login Window start XML
<Window x:Class="DevnMark_V1._0.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:Progress"
Title="MainWindow" Height="292" Width="563" WindowStyle="None" BorderBrush="#FF0A6277" AllowsTransparency="True" WindowStartupLocation="CenterScreen" Topmost="True">
Exception occurs when i close Login window and occurs at point InitializeComponent();
of second window when it is viewed when it is going to be initilized
To open a new window, use a keyboard shortcut: Windows & Linux: Ctrl + n. Mac: ⌘ + n.
Why Open Links in a New Window? The main reason is simple: you want people to stay on your website. If a user clicks a link on your website and is taken off your site, there's not a great chance they'll take the time to navigate back. Sure, they may hit the Back button in their browser or retype your URL.
Open Link in a Popup Window In order to open them in a new window, we add target="_blank" attribute to links. However to open the links in a separate popup window, we can make use of the onclick property and specifying a inline JavaScript code window.
A tab is more or less same as a window. A window can contain several tabs and all session data and cookies are shared across all tabs and open window. It's better to open a lot of tabs than opening several windows because too many window becomes too cluttered to handle.
I solved this problem in this way:
I removed from App.xaml the StartupUri="MainWinodw.xaml"
, leaving only Startup="Application_Startup"
.
In Application_Startup
, I IMMEDIATELY referenced both login and main windows:
loginwindow Login = new loginwindow();
mainwindow Main = new mainwindow();
I verified my Login, then closed the login window and opened the main window with a simple .Show()
:
Login.ShowDialog();
if (!Login.DialogResult.HasValue || !Login.DialogResult.Value)
{
Application.Current.Shutdown();
}
main.Show();
No changes in ShutdownMode
.
There may be no program.cs, but there is an App.xaml.cs in the default WPF program template and you can do the same thing there.
What you want to do is remove StartupUri="LoginWindow.xaml"
from App.xaml and then modify App.xaml.cs's constructor to invoke your login window and your main window, like this:
public App() : base() {
bool authenticated = false;
LoginWindow login;
while (!authenticated)
{
login = new LoginWindow();
login.ShowDialog();
authenticated = ValidUser(login.username, login.password);
}
MainWindow main = new MainWindow(login.username);
main.ShowDialog();
}
The above example assumes you've added username
and password
as public properties to LoginWindow, and that you've modified MainWindow's constructor to take a parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With