Prism 4 + MEF + IDialogService - state changes ignored when dialog re-opened

Topics: Prism v4 - WPF 4
Dec 11, 2011 at 9:47 PM
Edited Dec 11, 2011 at 9:49 PM

I try to make Modal Window. I use the approach described in this article, but unfortunately, I have a problem. When I first called Modal Window everything is working correctly.

http://imageshack.us/photo/my-images/707/16199488.jpg/

http://imageshack.us/photo/my-images/803/89135642.jpg/

But if you close the window and open it again Modal Window will not be react to state changes.

http://imageshack.us/photo/my-images/214/21686102.jpg/

I can not understand how to fix it.

Source code

Developer
Dec 12, 2011 at 8:52 PM

Hi,

In my opinion, the recommended approach to create dialog or modal window in Prism is using the WindowDialogActivationBehavior provided in the Stock Trader RI (or the PopupDialogActivationBehavior for Silverlight). Those behaviors will allow you to define a region that, when a view is added to it, will be shown in a new window/popup.

You might find the following threads useful where similar concerns are discussed:

As a possible workaround using your approach, you could add the following code in the code-behind of the Window1 window to handle the Closed event of the window:

private void Window_Closed(object sender, EventArgs e)
{
    IRegionManager myRegionManager = RegionManager.GetRegionManager(MainRegion);
    IRegion myRegion = myRegionManager.Regions["MainRegion"];

    while( myRegion.Views.Count() > 0)
    {
        myRegion.Remove(myRegion.Views.FirstOrDefault());
    }

    myRegionManager.Regions.Remove("MainRegion");
}

Like this, we remove all the views from the region and remove the region itself from the RegionManager, so that the next time the Window1 window is created it should work like the first time. Note that in the previous code snippet, we get the RegionManager of the element MainRegion. This element is the ContentControl that acts as the region and has the name "MainRegion."

I hope you find it useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini