Start two windows in sequence from App.xaml.cs WPF - c#

I want to let the user login before the MainWindow shows up.
The first window shows up, works fine and after closing all other WindowName.ShowDialog() calls do not work.
My code:
In app.xaml I removed StartupUri and replaced it with
Startup="Application_Startup"
Which looks like this (App.xaml.cs):
private void Application_Startup(object sender, StartupEventArgs e)
{
Window1 window1 = new Window1();
window1.ShowDialog();
MainWindow window2 = new MainWindow();
window2.ShowDialog();
}
I have two completly untouched windows (besides the title) second one looks the same:
<Window x:Class="Delete_Me.Window1"
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:Delete_Me"
mc:Ignorable="d"
Title="First window" Height="300" Width="300">
<Grid>
</Grid>
</Window>
Code behind is also untouched.
Question
How can I start two windows from App.xaml.cs in sequence?
Or what would be a better way to do this?
My approaches
I tried to swap out the two windows, which didn't change anything.
Of course I could start the LoginForm in the contructor / OnLoaded from the MainWindow, but that does not seem to be a beautiful solution to the problem.
I would like to not even start the MainWindow, if the authentication fails.
Any suggestions? Thank you.

A WPF application quits if no more window is existing. This means your application is stopped after closing the first window before the second window is created.
To solve your problem you need to create the second window before closing the first one. Just change your code to
Window1 window1 = new Window1();
MainWindow window2 = new MainWindow();
window1.ShowDialog();
window2.ShowDialog();
to fix it.

Related

How to close a hidden main window in WPF?

I have two windows in a single application.
The first window has one button. When I click the button, the second window opens. (The first window needs to close or hide). Then I need to close my second window and need to stop the debugging. How to do it?
<Window x:Class="WpfApp5.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:WpfApp5"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
xmlns:Control="clr-namespace:Syncfusion.Windows.Tools.Controls;assembly=Syncfusion.Tools.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Create" Height="50" Width="100" Click="btncreate"></Button>
</Grid>
MainWindow code-behind:
public partial class MainWindow : Window
{
public static TextBox textBox;
public MainWindow()
{
InitializeComponent();
}
private void btncreate(object sender, RoutedEventArgs e)
{
SecondWindow secondWindow = new SecondWindow();
secondWindow.Owner = this;
this.Hide();
secondWindow.ShowDialog();
}
}
Second window:
<Window x:Class="WpfApp5.SecondWindow"
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:WpfApp5"
mc:Ignorable="d"
Title="SecondWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBlock Text="New Project Loaded" HorizontalAlignment="Center" Margin="50"></TextBlock>
</StackPanel>
</Grid>
Your problem seems to be that you're running code which is showing a window, maybe modally.
At the same time, you're trying to shut the app down.
You've not shown this code but if your btnCreate handler has shutdown in it just before
secondWindow.ShowDialog();
You'd be trying to shut the app down effectively at the same time as showing that window as a dialog.
What you want is your code to shut the app down but not whilst the UI is busy doing something else like show a window.
You could do that by moving your shutdown so it's in say the contentrendered event handler of that window you're showing.
Another option is to schedule the shutdown using dispatcher.
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
Application.Current.Shutdown();
}), DispatcherPriority.ContextIdle);
If you still have errors you could consider instead using.
Environment.Exit(0);
This is a rather brute force approach compared to shutdown but if you're only doing this whilst debugging that probably won't matter.
In any case, you should ensure you don't have code which shows another window or something from the window.closing events of any of your windows. Since closing the app will necessarily involve closing windows.

How can hide the opened child windows from taskbar (WPF)?

How can hide the opened child windows from taskbar when I am showing and hiding the child windows even when I hide the child window the hidden window still appear in the taskbar WPF?
Thanks in advance,
Here is an Example how I show the dialogs:
AlignLocalAxisView alignLocalAxisView = Container.Resolve<AlignLocalAxisView>
(new ParameterOverride("model", AttributesSelectedItems));
OpenedWindowView = alignLocalAxisView;
alignLocalAxisView.Show();
There should be a ShowInTaskbar property for the window.
If your window is defined in XAML, you can set the property deliberately as shown below:
<Window
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"
mc:Ignorable="d"
x:Class="MyApplication.MainWindow"
Title="MainWindow"
Height="350" Width="425"
ShowInTaskbar="False">
You can also set this in your code behind:
this.ShowInTaskbar = false;
This is also applciable to a window created in code behind when called by name.
Window myNewWindow = new Window();
//Set the property to keep the window hidden in the task bar
myNewWindow.ShowInTaskbar = false;
//Then, show the window
myNewWindow.Show();
EDIT: Based on your example code, the following should work
AlignLocalAxisView alignLocalAxisView = Container.Resolve<AlignLocalAxisView>(new ParameterOverride("model", AttributesSelectedItems));
OpenedWindowView = alignLocalAxisView;
//Assuming your view extends the Window class, the following will work
alignLocalAxisView.ShowInTaskbar = false;
alignLocalAxisView.Show();
Hopefully, this will be enough to sort the problem out.
For future reference though, this was a fairly quick solution to look up on google - its generally worth searching for an answer first as it can sometimes be a faster way to solve the problem.
in this case, I reworded your issue to "hide task bar icon for window in wpf". The child window part wasn't really needed in the search, as all windows in WPF are basically the same.
I hope that's of some help.

How to stop debugging in VS17 when pressing close?

I am new to WPF and I am trying to understand how to transition between different windows.
I currently have two windows: a LoginWindow and a MainWindow. The LoginWindow opens first and transitions to the MainWindow. However, when this happens and I press close, the debugging in VS17 does not stop. Why is this and how can I make it work properly? I have looked around a bit whereas I found this:
<Application.MainWindow>
<NavigationWindow Source="MainWindow.xaml" Visibility="Visible"/>
</Application.MainWindow>
but it doesn't seem to work. VS simply enteres "break mode"... :(
This is how my App.xaml currently looks like
<Application x:Class="LearningWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LearningWPF"
StartupUri="LoginWindow.xaml">
<Application.MainWindow>
<NavigationWindow Source="MainWindow.xaml" Visibility="Visible"/>
</Application.MainWindow>
<Application.Resources>
</Application.Resources>
Any suggestions as to what I can do or what I am doing wrong?
The problem is because you still have the login window open.
What you can do is override the OnClose event and then forcefully close the application when the main window is closed. Write this in MainWindow.xaml.cs
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
Application.Current.Shutdown() will ensure all the windows are closed on close of mainwindow.

Maximized window with WindowStyle="None" doesn't move again using keyboard SHIFT+WIN+LEFT/RIGHT

I have a 2 monitor system under Windows 7 64 bit .NET 4.5.1
Here are the window and the steps to reproduce a nasty situation that I have:
xaml of the window
<Window x:Class="WindowStyleTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowStyle Test"
WindowStartupLocation="CenterOwner"
WindowStyle="None"
Height="350"
Width="525"
Loaded="MainWindow_OnLoaded">
<Grid>
</Grid>
</Window>
code behind of the window
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
// quick fix to show the taskbar, you can remove this, but it also doesn't work
this.WindowStyle = System.Windows.WindowStyle.ThreeDBorderWindow;
this.WindowState = System.Windows.WindowState.Maximized;
this.WindowStyle = System.Windows.WindowStyle.None;
}
}
Move the maximized window to other monitor with SHIFT+WIN+LEFT or SHIFT+WIN+RIGHT
Now the window should be moved to the other monitor and still maximized
Try to move the window back to the previous/first monitor with SHIFT+WIN+LEFT or SHIFT+WIN+RIGHT
So you wondered why nothing happens? The window doesn't move again!
Me too :-D
Test system: Windows 7 64 bit .NET 4.5.1
test repository
Any ideas to fix this? I think it's a windows issue.
And yes, I need the WindowStyle="None"
I don't see any problems here. The only problem I can see is the short cut key you are using.
You probably want WINDOW + LEFT or WINDOW + RIGHT or WINDOW + UP to move it.
or WINDOW + SHIFT + LEFT/RIGHT to move between monitors.

Locking focus to a window WPF

I am adding a disclosure window to the start of my application. I want the window to be the only window that the user can interact with until it is closed. I have googled the heck out of it and have come up empty.
Thanks,
Joseph
You want modal dialog maybe? See ShowDialog() method.
For example:
App.xaml file:
<Application x:Class="WpfApplication3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup" />
App.xaml.cs file:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
var w = new YourDisclosureWindowClass();
w.ShowDialog();
// whatever you need to run you entire application
}
}

Categories