WPF Bug List (5)--Hidden modal dialog box becomes modeless

Source: Internet
Author: User
Tags visibility

Found this problem, vaguely remember someone had already sent this problem, want to put the link here, but for a long time, it is not found. If you find it in the future, you must add.

Problem Description: Use the ShowDialog method to pop up a modal dialog box, and then set the Visibility property of this dialog box to hidden, then set back to visible, found that this dialog box is not modal.

Some people will think that it is closed, and will not have this problem, why do you want to cancel the close and then show it? Because it has an application environment.

Application environment: There are dialog boxes that logically are examples, such as the Find dialog boxes in office and Visual Studio, and there is obviously no need to display two at the same time. And there is no need to instantiate and display each time, so that when the user closes the form, it's better to hide the form so that the last lookup keyword still exists. You can save some code from saving this history keyword.

Of course, there will be bad places in this way, and we are welcome to criticize them.

A program was written to simulate the bug, as shown in the three images below.

Figure 1. Main form, click the first button

Figure 2. Pop-up modal dialog box, click on the button to hide itself

Figure 3. Click on the last button on the main form to show that it's modal dialog box

The previous hair bug, generally did not go to see. NET source code, this feeling this bug is a bit too wrong, looked at the source code, found that WPF also deliberately for the dialog (modal) of the hidden do a separate processing, feel more should not have a problem, we look at the source code.

Dodialoghide

[SecurityCritical, SecurityTreatAsSafe]
private void Dodialoghide ()
{
Securityhelper.demandunmanagedcode ();
bool Isactivewindow = false;     
if (this._dispatcherframe!= null)
{
This._dispatcherframe.continue = false;
This._dispatcherframe = null;   
}
if (!this._dialogresult.hasvalue)
{
This._dialogresult = false;
   
This._showingasdialog = false; Cause this Bug
Isactivewindow = this._swh. Isactivewindow;
this. Enablethreadwindows (TRUE);
if (Isactivewindow && (this._dialogpreviousactivehandle!= intptr.zero)) && UnsafeNativeMethods. IsWindow (New HandleRef (this, this._dialogpreviousactivehandle)))
{
Unsafenativemethods.setactivewindow (new HandleRef (this, this._dialogpreviousactivehandle));
}
}

Where the _showingasdialog set to False, when the form of the visibility set to visible, the window class will be based on the value of the variable to determine whether the form is displayed in modal mode. Ms's comment on this line of code is only "//Clears _showingasdialog".

From the source code, the WPF window appears to be using the following code to change a form from modeless to modal.

Setasmodal

try
{
   //telluserswe'regoingmodal
   ComponentDispatcher.PushModal();
   _dispatcherFrame=newDispatcherFrame();
   Dispatcher.PushFrame(_dispatcherFrame);
}
finally
{
   //telluserswe'regoingnon-modal
   ComponentDispatcher.PopModal();
}

But when I use this method in my own use, I find it impossible to achieve the goal. Then suddenly think of a method, try it, you can. The workaround is to make the form appear again without using visibility = Visible. And then call the ShowDialog method again to display the form. This approach may only be possible for people who are unfamiliar with WPF or very familiar with it (I am a dead horse as a live horse), because normally the second call to the ShowDialog method throws an exception. A similar bizarre window exception is used in [WPF] to confirm with a message box when the modeless subform is closed--as described in the exception thrown when the minimized form is resolved.

In addition, MessageBox that are ejected from a non-UI thread are also modeless. The solution is simple, just pop the MessageBox in the dispatcher.

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.