Locking focus to a window WPF - c#

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
}
}

Related

Wpf c# how to close window in page?

I found the problem is It cannot close this window. But it can open the MainWindow.
pls help
Code in button
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
MainWindow MainView = new MainWindow();
MainView.Show();
AuthWindow AuthView = new AuthWindow();
AuthView.Close();
}
I want to press the button inside the page and close that window and open another window.
For such scenarios, I advise you to use RoutedCommand. In this case, you can use the ready-made command ApplicationCommands.Close.
In the page button, specify the command name:
<Button Content="Close Window" Command="Close" />
In the Window, set command executing:
<Window.CommandBindings>
<CommandBinding Command="Close" Executed="OnCloseWindow"/>
</Window.CommandBindings>
<x:Code>
<![CDATA[
private void OnCloseWindow(object sender, ExecutedRoutedEventArgs e)
{
this.Close();
}
]]>
</x:Code>
P.S. I also do not advise you to open new windows. Since you are using Pages, you should change Pages in a single Window. And closing the Window is regarded as an Exit from the Application.

WPF call Window_Closing event from code behind

Recently I have been experimenting with WPF. I was building a little program, but then I stumbled across a little problem. I tried to call the Window_Closing method from the code behind, it told me that I needed to give it cerntain parameters, but I don't have those parameters in the method I am trying to call it from.
This is my code:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// My Window Closing code
}
private void Application_Exit(object sender, RoutedEventArgs e)
{
// Here is where I am trying to call it, giving a empty parameter with it.
// But it doesn't seem to work.
Window_Closing(null, EventArgs.Empty)
}
What I want
If a cenrtain button is called, I want the Window_Closing event to be called.
Anyone who knows the solution?
In your Designer, click on the Button and open its property window. There you select the "Events" - the "lightning" and choose the Click event to execute your Window_Closing method. There is a drop down in which you should be able to select it. If not, enter your method name there and press "Enter" to let VS generate the code-behind method.
Add "Window_Closing" & "Close_Click" in the XAML file (MainWindow.xaml) as follows.
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Closing="Window_Closing">
<Grid>
<Button x:Name="Close" Content="Close" Click="Close_Click" Width="100" Height="30"/>
</Grid>
Then in the code behind file (MainWindow.xaml.cs) add the following code. Here the window close command is called from the button click event handler which in turn closes the window, and then the 'Window_Closing' event handler will be automatically called.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void Close_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
Just close the window. Then the event will be called automatically, thats what it is there for to begin with.
And as it seems, you try calling it from your Application_Exit, which closes all the windows ... so .... What is your problem?
Just put a breakpoint in the Window_Closing and see, that is executed automatically. If not, maybe you should add the eventhander to all the windows Window_Closing events.

Start two windows in sequence from App.xaml.cs WPF

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.

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.

SystemEvents.PowerModeChanged event handler not being called in VSTO & WPF apps

I am developing a VSTO application-level Word AddIn and a WPF application, both need to be notified when the system goes to sleep and subsequently resumes. I have bound my event handlers to the SystemEvents.PowerModeChanged event in each application, but for some reason it still never gets called when the system goes to sleep or resumes. Just to test, I am simply trying to write to the console when the system goes to sleep. I also tried setting breakpoints, but that didn't work either; although I'm not sure if they would've anyway, given that the system is suspending applications. With either attempt, it never prints nor breaks:
VSTO Addin
void ThisAddIn_Startup(object sender, EventArgs e)
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(powerModeChanged);
}
public void powerModeChanged(object sender, PowerModeChangedEventArgs args)
{
Console.WriteLine("Sleeping.....");
}
WPF
internal MyControl()
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(powerModeChanged);
}
public void powerModeChanged(object sender, PowerModeChangedEventArgs args)
{
Console.WriteLine("Sleeping!!");
}
I have tried changing the access level of the event handlers from public to private to internal and vice-versa as well as moving the binding to other parts of the code in each application, but it didn't solve the issue. Any help would be greatly appreciated thank you!
SOLUTION: As per the comments and helpful answer, the event wasn't firing because I was running windows through a VirtualBox VM. Once my coworker ran the code on a native windows machine, it worked.
Try this:
XAML:
<Window x:Class="WpfApplication279.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:WpfApplication279"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<ListView x:Name="listView1" Margin="0">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
</Grid>
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
listView1.Items.Add(string.Format("{0} : Power mode changed = {1}", DateTime.Now, e.Mode));
}
}
In order to trigger the event in a Windows 10 machine, right-click the Start button and then:
Letting the machine go into sleep mode by timeout works the same way. These are my settings:
Same result:

Categories