Material Design DialogHost doesn't close after First Page Load in WPF - c#

I've been working with this WPF application that integrates a UrhoSharp Window. When I click somewhere in the Urho window, there's this dialog that pops up as rendered by the main WPF Application.
The issue is that on every application startup or first page load, the dialog pops as it should but doesn't respond to any of the user actions (the dialog has buttons in it) which is supposed to close the dialog eventually. However, if I resize the window or just click the application in the Start Bar, the dialog now responds(happily ever after). This issue doesn't return until I exit the current application page and switch back to the same page. The case is also the same for every application startup.
All the buttons in the dialog executes this command:
ICommand cmd = DialogHost.CloseDialogCommand;
Execute(null);
And the CloseDialogCommand is awaited like this:
var dialog = new SomeDialog();
dialog = (SomeDialog)await Controller.dialogHandler(dialog, true);
I couldn't figure out what the issue actually is. May be some help would do good.

Related

Why does a folder dialog immediately close unless a window is displayed in WPF?

I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.

Is there a way to disallow clicking outside of winforms (c#) until user presses close button?

I'm currently building a winforms application. The idea is to force the user to press Close or Cancel button and will not remove focus from the winform or be overlapped by the (whatsoever clicked item/apps/) if the user clicks outside of its winform border/size.
* Update *
I am not trying to create a ransomware. We are trying to copy the behavior of bootstrap modal that we created in our web app that the only way to close the modal is by pressing our "x" button. We are focusing on consistency.
I hope that you are talking about a modal dialog - one that blocks interaction with the rest of your application, rather than all applications running on the PC.
Assuming this is the case, do the following when opening your dialog:
using (var frm = new MyCustomForm())
{
//This is a blocking call - the execution stops here until that
//form is closed and you can then examine the result
var result = frm.ShowDialog();
}
See https://msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx for more information on ShowDialog method.

Window shown as dialogue losts its focus - wpf

I have a main window Say home and from there i am calling another window named addItem as
var item = new addItem();
item.ShowDialog();
Its working fine.
But when we navigate to other applications like chrome, notepad by alt + tab and come back to the WPF application both the windows are separated meaning as like in winforms it wont stick together
So user got confused in that behavior.
Requirement is unless until the dialogue window is opened it should always
be on top and main window on the back and when we click the icon in task bar both window together should come.
try this
var item = new addItem();
item.Owner = this;
item.ShowDialog();

Mono Gtk Window require focus for input

I haven't been able to find an example containing this functionality, and either i missed it in the documentation or it's not there.
I have a fullscreen GUI program, and when a user is required to type in a number, a calculator window popup has to appear in the center of the screen. The user types a number and clicks on enter, or hits cancel to continue past the window.
The problem is clicking on the fullscreen window behind the calculator brings that window to the front and hides the calculator without the intended entry being completed, which could get annoying for the user.
I guess the functionality I'm trying to create is what happens in most text editors/IDE's when you press the Open File button. Let me know if you want to see code, it's just two separate Window classes at the moment.
The Present() function of the Window object brings that particular window to the front. So adding a FocusOutEvent listener on the window you wish to keep in front like:
windowObj.FocusOutEvent += (obj, args) => windowObj.Present();
will work.
The alternative to this (and the better way) is to set the Modal property of the window to true.
If you've stumbled across this and you were looking for a popup window that suspends whatever called it to wait for input, see this question:
gtk# thread for window
You basically use the Dialog class instead of Window and add your elements to the ActionArea of the Dialog.
Hope this helps.

Open a dialog in the same window and wait for user input

Is there a way to open a new window instance within the primary application window and still wait for a user response before continuing with the main program? I just have a 3 option query with a picture to be offered to the user and a whole new window opening up seems like overkill, I would much prefer it opening in a frame within the current window.
Do something like this sans the separate window:
OptionWindow optionDialog = new OptionWindow();
optionDialog.Owner = this;
optionDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
optionDialog.ShowDialog();
if (optionDialog.DialogResult == true)
{
something;
{
Seems like you could just have your "OptionWindow" be a frame that is on the WPF form, and when you want to show it, just disable all other panels and show the frame with your dialog, and after the user completes the dialog, enable all other panels and hide your dialog frame.
After I posted my answer, I saw that Mark Hall made an edit to his comment with pretty much the same suggestion. Sorry Mark.
I guess you are looking for modal window which can be achieved using Window.Showdialog.
Opens a window and returns only when the newly opened window is
closed.

Categories

Resources