Enrique posted on January 13, 2010 13:07

I’m going to quickly post the solution to this issue since its late ..or too early 5:41 a.m EST  basically when working with WPF using  CAL-PRISM- or whatever you want to call the Composite Application Guidance for WPF and Silverlight and you want to show a popup window you may want to use the WindowRegionAdapter from the WPF Contrib project   the problem is when you close the windows you may have the exception {"Cannot change ObservableCollection during a CollectionChanged event."}

 

private void window_Closed(object sender, EventArgs e)
{
Window window = sender as Window;
IRegion region = _regionWeakReference.Target as IRegion;
if (window != null && region != null)
if (region.Views.Contains(window.Content))
region.Remove(window.Content);//exception
                                             //Cannot change ObservableCollection 
                                             //during a CollectionChanged event."} 
}

 

 

 

I have made some changes to WindowRegionAdapter.cs in order to fix it and seems to be working, here are the changes:

add:

using System.Threading;
replace the  window_Closed  code for the code below:
 
class CloseWindwowCallbackParameter
{
    public IRegion theRegion { get; set; }
    public Window windowToClose { get; set; }
}
private void closeWindowinThread(object parameters)
{
    CloseWindwowCallbackParameter arguments = parameters as CloseWindwowCallbackParameter;
    IRegion region = arguments.theRegion;
    Window window = arguments.windowToClose;
    window.Dispatcher.Invoke(
                       System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
  {
      region.Remove(window.Content);
  }));
 
}
 
private void window_Closed(object sender, EventArgs e)
{
 
    Window window = sender as Window;
    IRegion region = _regionWeakReference.Target as IRegion;
    if (window != null && region != null)
        if (region.Views.Contains(window.Content))
        {
            WaitCallback callback = new WaitCallback(closeWindowinThread);
            ThreadPool.QueueUserWorkItem(callback, new CloseWindwowCallbackParameter 
                                                 {theRegion = region, windowToClose = window });
        }
}


Posted in: WPF  Tags: , ,

Comments

Comments are closed

Calendar

«  March 2010  »
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 BizTalkAnd .Net