UWP / WinRT: Detect when a popup is about to be closed - c#

How can I detect when a popup is about to be closed?
The Popup class does not have a Closing event in UWP, unlike in WPF where such an event exists.
I need this in order to persist the state of the Popup because the layout can be modified by the user.

As you already know, there is no Closing event. You might get lucky by registering to IsOpen property change (if IsLightDismissEnabled property is set to true...)
this.popup.RegisterPropertyChangedCallback(Popup.IsOpenProperty, (d, e) =>
{
if (!this.popup.IsOpen)
{
// do something, popup is closing?
}
});
because that happens before the LostFocus and Closed events get fired. Other than that, you can redesign the way you persist data to persist them all the time if it's not something very complex to avoid having to depend on the closing event.

Related

WPF: Is this behavior intended? PreviewLostKeyboardFocus and LostKeyboardFocus

I have a TextBox and I want to save the content, when the user leaves the TextBox. I planned to use PreviewLostKeyboardFocus, but it doesn't work as intended.
<TextBox PreviewLostKeyboardFocus="textBox2_PreviewLostKeyboardFocus"
LostKeyboardFocus="textBox2_LostKeyboardFocus" />
When I click on another control inside of the same application, I first get the PreviewLostKeyboardFocus event and then the LostKeyboardFocus event. But when I activate another application, the PreviewLostKeyboardFocus event simply doesn't happen. I only get LostKeyboardFocus.
This is the expected behaviour.
The PreviewLostKeyboardFocus event is not raised when you switch to another application.
The main purpose of handling the event in the first place is to prevent the keyboard focus from changing: https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewlostkeyboardfocus(v=vs.110).aspx
And if the event was raised when you switch to another application, you would be able to prevent the keyboard focus from changing by handling this event and set the the Handled property of the KeyboardFocusChangedEventArgs to true and this would effectively prevent the user from being able to focus any other element on the screen while running your application.

WPF Restore Visibility State on next start

I'm trying to convert my old windows forms code into wpf. Each of my windows stored the visibility state, size and position and restored this states on the next run of the application. I connect to the FormCloseQuery event where it was possible to get information about the closereason. This is not possible in WPF.
I can store the position and size in the WPF forms' closing event, but it's not possible to store the visibility state.
Is there an answer how this can be done?
Thanks
Martin
I can store the position and size in the WPF forms' closing event, but it's not possible to store the visibility state.
Poppycock! What's wrong with the Window.Visibility property?:
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
// this.Visibility is the current visibility state of the Window
}
Also, for your information, it is a better idea to handle the Window.Deactivated event to update the values each time the application goes out of focus, or the Window.Closed event to update upon closing. The Closing event is specifically for cancelling the close request.
Furthermore, it is also customary in WPF to store bool values and then data bind them to the Window.Visibility property using the BooleanToVisibilityConverter Class.

Block Events from a Form/UserControl [duplicate]

This question already has answers here:
Disable All Events on a Windows Form
(5 answers)
Closed 10 years ago.
My Application sometimes comminucate with a Server in another Thread, while the form should be locked in this time(that means, the user cant fire any Click, KeyDown .... event). In this time, a waitoverlay with a loading circle is showing which blocks any mouse events like click or mousehover.
But the problem is, that the user can fire KeyDown events by pressing keys and the user can also make mouse clicks by pressing enter after navigating to the button with the TAB Key.
I know that Form.Enabled = true; can do the trick, but then the form is looking very ugly. I know also that I could make a bool inaction; and then look at this var in the event handler, but I think theres a better way.
Short Form: How can I block GUI Events like Click or KeyDown from a Form without .Enabled = True?
Any help will be greatly appreciated.
Edit: Solved it by adding this to the overlay control:
protected override bool ProcessKeyMessage(ref Message m)
{
return true;
}
protected override bool ProcessTabKey(bool forward)
{
return true;
}
You may set the KeyPreview property of the form to false.
When the KeyPreview is false, the form will not receive key events before the event is passed to the control that has focus. And if you change your focus to some unused control which does not interact with keys, form or any controls waiting for key events will not receive any events.

What events are triggered when ShowDialog(ParentForm) is called in C#

Simple question. I have a MainForm and a settingsForm. The settings form is initialized once and then shown every time the user clicks a button. I need it to do something when this button is clicked.
m_inputSettings.ShowDialog(this); //edit settings selected, so show that form
This is the MainForm calling the settings form, which it does fine. But I need the SettingsForm to do something every time this happens. Presently, I cant figure out if this call actually triggers any events I can set handlers for. Does it trigger an event? If not, is there another way I can tell my SettingsForm to do something every time this call happens?
Note: Any code in the mainform after that line doesn't get executed until the SettingsForm returns, but that is intentional.
Thanks.
Edit: One of the things I want my form to do it select a specific control when this happens, but it seems that that is impossible until after the form is done loading everything.
You can override the OnVisibleChanged method in your settings form. Make sure to call base.OnVisibleChanged though as to not screw up any potential observers of the event (and anything else the base class may do inside of that method.)
FormShown event - raised only once when form is displayed first time.
OnPaint / OnActivate - every time form is activated, but these events raised even when you switch with other application, which probably you don't want to do.
If you are changing form visbility, then you can use OnVisibleChanged
If you are minimizing the form, you can use OnSizeChanged / OnLocationChanged event.
If none suits you, make a public property and set false when form is closed / hidded, and set true before showing it. OnActivate, use this property to do your task.
Maybe use VisibleChanged event.
Override OnShown() event in your form this will raise only once after the form is opened
The disadvantage of OnVisibleChanged is it will also get raised when the form is closed.
On Paint , On Activate and OnEnter will raise before form is shown to the user.

WPF: Is there an event that is fired when the user presses the close button [X]?

Is there an event that is fired when the user presses the close button?
This is because the Window Closing event is fired both when one closes the window manually (with the Close method) and also when the user presses the [X] button...but I somehow need to know only when the user presses the [X] button not when the window is closed manually.
I don't believe there is a way to tell those apart in WPF (though I'm not positive).
The way I always handled it in WinForms was to create a member variable "_Closing", set it to false, and a method "ReallyClose()" that would set _Closing to true, then call Close. My Closing handler would then cancel the close if _Closing was not set to true.
Yeah, it's a bit of a hack, but it worked.
I also don't think there is a way to tell them apart. You can put a handler on the Application.Exit event, but it doesn't distinguish between "red X button close" and "alt-F4 close" (or whatever other types of close you are considering).
BTW, if you check for Application.Exit, be sure to check for Application.SessionEnding too - if someone logs off while your app is running, you can't be guaranteed that Application.Exit will be called.
Try to put button with name Cancel and bool variable in your class so When you click on button set it to the true and in Closing Event check if is true use e.Cancel=false to exit window I tried everything and It doesn't work for me and I do on this way and also you can remove X button just to have Accept ot Cancel buttons if you insert some informations.

Categories

Resources