UserControl Unloaded Command Behavior not working..

Topics: Prism v2 - Silverlight 4
Mar 31, 2011 at 3:34 PM

Hi,
I'm using SL4 with Prism 2.2  and need to capture Unloaded event for a usercontrol. I have this Unloaded Command behavior.

 

public static class UnLoaded
    {

        #region Dependency Properties
        //Dependancy Property that should be attached with the Control

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(UnLoaded), new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
        = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(UnLoaded), new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(DependencyObject obj)
        {

            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        public static void SetCommandParameter(DependencyObject dataForm, object parameter)
        {
            dataForm.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(DependencyObject dataForm)
        {
            return dataForm.GetValue(CommandParameterProperty);
        }

        public static UnLoadedBehavior UnLoadedCommandBehavior(DependencyObject obj)
        {
            return (UnLoadedBehavior)obj.GetValue(UnLoadedCommandBehaviorProperty);
        }

        public static void SetLoadedCommandBehavior(DependencyObject obj, UnLoadedBehavior value)
        {
            obj.SetValue(UnLoadedCommandBehaviorProperty, value);
        }

        public static readonly DependencyProperty UnLoadedCommandBehaviorProperty = DependencyProperty.RegisterAttached("UnLoadedCommandBehavior", typeof(UnLoadedBehavior), typeof(UnLoaded), null);

        #endregion

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            UserControl element = dependencyObject as UserControl; if (element != null)
            {

                UnLoadedBehavior behavior = GetOrCreateBehavior(element);
                behavior.Command = e.NewValue as ICommand;
            }
        }
        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            UserControl targetObject = dependencyObject as UserControl;
            if (targetObject != null)
            {
                UnLoadedBehavior behavior = GetOrCreateBehavior(targetObject);
                behavior.CommandParameter = e.NewValue;
            }
        }
        private static UnLoadedBehavior GetOrCreateBehavior(UserControl element)
        {
            UnLoadedBehavior behavior = element.GetValue(UnLoadedCommandBehaviorProperty) as UnLoadedBehavior; if (behavior == null)
            {
                behavior = new UnLoadedBehavior(element);
                element.SetValue(UnLoadedCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }

    public class UnLoadedBehavior : CommandBehaviorBase<UserControl>
    {
        public UnLoadedBehavior(UserControl element)
            : base(element)
        {
            element.Unloaded += element_UnLoaded;
        }

        private void element_UnLoaded(object sender, RoutedEventArgs e)
        {
            //base.ExecuteCommand();
            ExecuteCommand();
        }
    }

Also,

 

<UserControl x:Class="IST.Modules.LoanApplication.Views.ApplicationWorkflowEditView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    xmlns:ControlExtension="clr-namespace:IST.Client.Infrastructure.Controls;assembly=IST.Client.Infrastructure"
    xmlns:controls="clr-namespace:IST.Client.Infrastructure;assembly=IST.Client.Infrastructure"
    xmlns:XAMLConverters="clr-namespace:IST.Client.Infrastructure.Converters;assembly=IST.Client.Infrastructure"             
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"    
    xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
    xmlns:CommandExtension="clr-namespace:IST.Client.Infrastructure.Commands;assembly=IST.Client.Infrastructure"
    xmlns:cmd="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"    
    xmlns:View="clr-namespace:IST.Modules.LoanApplication.Views"    
    xmlns:telerikDocking="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
   
    CommandExtension:UnLoaded.Command="{Binding UnloadedCommand,Mode=TwoWay}"        
    mc:Ignorable="d"                
    >

 

 

My Command behavior does fire the Unloaded event whenever i unload my view. But the UnloadedCommand in my viewModel isn't get invoked. anyone having any idea what am I missing here? 

 

br,

 

 

Mar 31, 2011 at 6:45 PM

Hi,

One possible cause for the issue you are experiencing could be that your view model is garbage collected when the view is unloaded.

Could you provide us with a repro sample of your solution, in order to reproduce your issue?

On the other hand, you can find the following links from Damian Schenkelman´s blog handy in order to check the code:

I hope you find this information useful.

Thanks,

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

 

Apr 1, 2011 at 9:42 AM

Actually I'm using these command behaviors for quite sometime now. I don't think there is any issue with that code. As I can see my debugger hit this line of code when my view is unloaded

ExecuteCommand();

 

It's just that it doesn't send the control to the command that I've bounded this command behavior with, in my ViewModel. Don't know why...

Apr 1, 2011 at 11:40 AM

hi, my bad it was problem at my end. We had this base class where for every view datacontext is being set to null before disposing it.