WindowBehavior ForceCloseAfterSettingDialogResult -> NullReferenceException

Topics: Issues / bugs
Jun 14, 2012 at 8:26 AM

Hi Geert,

i overlooked this issue. ForceCloseAfterSettingDialogResult causes a NullReferenceException because the property accesses the WindowLogic object but the logic is created first when the behavior is attached to the element.

When setting the ForceCloseAfterSettingDialogResult property through xaml the behavior is not yet attached and therefore the WindowLogic not initialized -> NullReferenceException.

Could you please fix that bug?

Many thanks!

Coordinator
Jun 14, 2012 at 6:00 PM

Interesting issue. I use this technique all over the place. Let me take a look at it and see if I can fix it easily.

Jun 15, 2012 at 7:21 AM
Edited Jun 15, 2012 at 7:23 AM

Currently i have created a simple workaround... i have derived a new WindowBehavior class which sets the ForceCloseAfterSettingDialogResult after the WindowLogic was created. Here's the code:

    public class WindowBehavior : Catel.Windows.Controls.MVVMProviders.WindowBehavior
    {
        protected override void Initialize()
        {
            base.Initialize();
            this.ForceCloseAfterSettingDialogResult = true; // base.Logic is now created, it's safe now...
        }
    }
Using the Catel WindowBehavior and using ForceCloseAfterSettingDialogResult through xaml does not work because of this code:
		public bool ForceCloseAfterSettingDialogResult
		{
			get
			{
				return base.Logic.ForceCloseAfterSettingDialogResult;
			}
			set
			{
				base.Logic.ForceCloseAfterSettingDialogResult = value;
			}
		}

base.Logic is created when the WindowBehavior is attached to the element, but it is not created when the WindowBehavior itself is created (instantiated) which results in a NullReferenceException. Therefore the catel WindowBehavior does not work properly through xaml when i touch the ForceCloseAfterSettingDialogResult property (setting it to true).

Coordinator
Jun 15, 2012 at 7:24 AM

Another thing you might want to consider is creating a new base window for all the windows in your app. You can programmatically add the behavior to this window, and the issue is solved. Another advantage is that if you want to make a generic change, you can easily do this.