Model Window ... Need Help

Topics: Prism v4 - Silverlight 4
May 12, 2011 at 3:17 PM
Edited May 18, 2011 at 1:20 AM

Dear Guys,

I have implemented a popup window using Childwindow, but have strange issues.

The dialoge open first time with no problem, but the second time it open without any message or icon (empty) and then when close (from X) it leaves the application in-accessable as if it still there , but invisible.

Here is the code I use to implement the Popupwindow:

	public enum MessageType { Info, Error, Confirm, TextInput, ComboInput };

	public interface IWindowController
	{
		int CloseResult { get; set; }
		void CloseWindow();
		void ShowWindow(string message, string headerText, MessageType type = MessageType.Info,
								 String[] inputOptions = null);
		event EventHandler MainWindowClosed;
	}

	[Export(typeof(IWindowController))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class WindowController : IWindowController
	{
		CWPopup _Window;

		private void setMessageIcon(string imagePath)
		{
			_Window.MessageIcon.Source = new BitmapImage(new Uri(ICONS_PATH + imagePath, UriKind.RelativeOrAbsolute));
		}
		public void ShowWindow(string message, string headerText, MessageType type = MessageType.Info, String[] inputOptions = null)
		{
		//Set the message.
			_Window.TextBlock.Text = message;
			_Window.Title = headerText;
			_Window.Show();
		}
		// Used so we can alert any consumer when the MainRadWindow has closed.
		public event EventHandler MainWindowClosed;


		public WindowController()
		{
			_Window = new CWPopup();
			_Window.OKButton.Click += OKButton_Click;
			_Window.CancelButton.Click += CancelButton_Click;
			_Window.Closed += new EventHandler(Window_Closed);
		}

		void OKButton_Click(object sender, RoutedEventArgs e)
		{
			CloseResult = 1;
			//CloseWindow();
		}
		void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			CloseResult = 0;
			//CloseWindow();
		}
		// Call if you need to add your own command to close a popup window
		public int CloseResult { get; set; }

		public void CloseWindow()
		{
			_Window.Close();
			//_Window.DialogResult = true;
		}

		#region Private Methods

		void Window_Closed(object sender, EventArgs e)
		{

			// Remove whatever is assigned to the content when the window is closed.
			//_Window.Content = null;

			if (MainWindowClosed != null) MainWindowClosed(this, null);

		}

		#endregion

	}


As it appears,I have the IWindow interface and the implementer class and I use MEF to import this in my moduels using Importconstructor attribute.

I got this stange behaviour in my page when I try to navigate away as implemented here:

WindowController.ShowWindow("Sure to close current window?/n/rsure to close current window","Caution", MessageType.Confirm);

 

Can anyone help me with this issue

Regards

Waleed

 

May 12, 2011 at 4:07 PM
Edited May 18, 2011 at 1:19 AM

Hello EveryOne,

I went a little bit far from this... I fixed the code as follows:

	public enum MessageType { Info, Error, Confirm, TextInput, ComboInput };

	public interface IWindowController
	{
		int CloseResult { get; set; }
		void CloseWindow();
		void ShowWindow(string message, string headerText, MessageType type = MessageType.Info,
								 String[] inputOptions = null);
		event EventHandler MainWindowClosed;
	}

	[Export(typeof(IWindowController))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class WindowController : IWindowController
	{
		CWPopup _Window;

		private void setMessageIcon(string imagePath)
		{
			_Window.MessageIcon.Source = new BitmapImage(new Uri(ICONS_PATH + imagePath, UriKind.RelativeOrAbsolute));
		}
		public void ShowWindow(string message, string headerText, MessageType type = MessageType.Info, String[] inputOptions = null)
		{
			//Set the message.
			_Window.TextBlock.Text = message;
			_Window.Title = headerText;
			_Window.Show();
		}
		// Used so we can alert any consumer when the MainRadWindow has closed.
		public event EventHandler MainWindowClosed;


		public WindowController()
		{
			_Window = new CWPopup();
			_Window.OKButton.Click += OKButton_Click;
			_Window.CancelButton.Click += CancelButton_Click;
			_Window.Closed += new EventHandler(Window_Closed);
		}

		void OKButton_Click(object sender, RoutedEventArgs e)
		{
			CloseResult = 1;
			//CloseWindow();
		}
		void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			CloseResult = 0;
			//CloseWindow();
		}
		// Call if you need to add your own command to close a popup window
		public int CloseResult { get; set; }

		public void CloseWindow()
		{
			_Window.Close();
			//_Window.DialogResult = true;
		}

		#region Private Methods

		void Window_Closed(object sender, EventArgs e)
		{

			// Remove whatever is assigned to the content when the window is closed.
			//_Window.Content = null;

			if (MainWindowClosed != null) MainWindowClosed(this, null);

		}

		#endregion

	}


But found a small issue with the eventhandler subscribtion (mainWindowClosed)

It fires as many as the window open if I hock the Event in the module as follows:

WController.ShowWindow("Sure to close current window?","Caution", MessageType.Confirm);
WController.MainWindowClosed += (s, e) =>
{
	//continuationCallback(WController.CloseResult == 1);
	MessageBox.Show(WController.CloseResult.ToString());
};

The messageBox is shows as many as the window is open.

What is wrong here ...

Best

Waleed

May 12, 2011 at 7:20 PM

Hi Waleed,

One possible approach that could help you avoid this issue from affecting your scenario would be to unsubscribe from the MainWindowClosed event in the event handler, like this:

void Window_Closed(object sender, EventArgs e)
{
    // Remove whatever is assigned to the content when the window is closed.
    //_Window.Content = null;
    if (MainWindowClosed != null) MainWindowClosed(this, null);
    _WindowClose.Closed -= this.Window_Closed;
}

In your current scenario, it’s possible that the controller that subscribes to the MainWindowClosed event keeps references to the views that are closed, because of the event subscription.

Please let me know if this information helps you.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

May 13, 2011 at 12:06 AM
Edited May 14, 2011 at 2:39 PM

Hello Miguel,

If I do that .. the event will fire in the first time only, never after ... I need it to firs always & once.

It is not giving the right behaviour.

Best

Waleed