Error when closing the window and reopening when the window instance is in singleton mode in MVVM mode

http://www.codes51.com/itwd/4505000.html


Problem:  When the window instance is in singleton mode in MVVM mode, an error occurs when closing the window and reopening it.
Description:

Singleton pattern MVVMWPFc#

Error message:
System.InvalidOperationException: "After closing Window, Visibility cannot be set, and Show, ShowDialogor or WindowInteropHelper.EnsureHandle cannot be called."
Core code:
public partial class RailVibrateDetect : Window

{

        //Singleton mode

        private static RailVibrateDetect frm = null;

        //Singleton mode

        private RailVibrateDetect()

        {

            InitializeComponent();

        }

         //Singleton mode

        public static RailVibrateDetect CreateInstrance()

        {

            if (frm == null || frm.IsActive  )

            {

                //frm.Hide();

                frm=new RailVibrateDetect();

            }

            return frm;

        }

}

/*The main window calls this window*/

 private void RailVibrateDetectExecute(Window window)

        {

            //Jump to the vibration browsing interface

            RailVibrateDetect p = RailVibrateDetect.CreateInstrance();

            //Display in the middle of the parent window,

            p.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            p.Owner = window;

       

            p.Show(); //Error here

        }


Solution 1:

Quoting the reply of baidu_38131614 on the 4th floor:
Also useless. . . . I'll try it in Hungry Man mode

I don't know your logic, I can tell you the reason for this error. The error is caused by calling the show of the same form again after you close the same form. So the solution is to close the window, you need to create a new form, and then call the show of the new form. Refer to http://blog.csdn.net/yapingxin/article/details/12209205
or do not close the form but choose to hide the form


The above introduces the answer to the question of "When the window instance is in singleton mode in MVVM mode, an error occurs when closing the window and reopening it", I hope it will be helpful to netizens in need.
URL link of this article: http://www.codes51.com/itwd/4505000.html




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446641&siteId=291194637