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.
Related
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.
I'm trying to disable the maximize capacity (not the maximize button) in a wpf window, but so far nothing has succeded.
I'm using a window with WindowStyle="none", but when I drag the window to the far top of the screen, the OS "maximizes" the window (terribly bad, by the way).
I uploaded 3 pictures to show what is happening exactly.
(however, due to the fact that I don't have 10 reputation, I have to post the links instead. Sorry about that. And I can't put all 3 links, only 2 of them, but the first one is just of the window working normally)
During:
After:
use the window state change event:
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.WindowState.Maximized)
{
this.WindowState = System.Windows.WindowState.Normal;
}
}
Set MaxHeight,MinHeight and MaxWidth,MinWidth property for the window.
Example
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MaxHeight="350" MaxWidth="525" MinHeight="350" MinWidth="525">
</Window>
How do you disable Aero Snap in an application?
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.
I have a slight issue creating a new window. The weird part is that the window seems to be created, but calling .Show() or .Activate() does nothing.
The code I'm using to create the window is:
TicketView tv = new TicketView();
tv.Activate();
I was originally trying to set the data context of the window at the same time, but I've taken that out to see if it was the issue, but I'm still seeing the same behaviour. Regardless, that's what the code is at this time. If anybody has any ideas, I'd be very grateful!
For reference - This is the ticket view XAML
<Window x:Class="Helpdesk.View.TicketView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TicketView" Height="300" Width="300">
<Grid>
</Grid>
</Window>
It depends on what you are trying to do. What I have done in the past if it is my main window is to use the Application.Run method. If I am trying to run another window from my application I use the ShowDialog method.
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
}
}