Trigger Action in Code

Oct 25, 2010 at 2:14 PM

I've got Action Filters setup as described in the recipe.  In my app I have a datagrid that can be refreshed by clicking a button and that action uses some filters.  I also need to automatically refresh that datagrid when its Screen is activated.  What's the proper way to trigger an action in code (so that all the ActionExecutionContext stuff gets properly setup)?

 

Thanks!

Oct 25, 2010 at 3:37 PM

I'm looking into this to find a clean solution.

Oct 25, 2010 at 5:34 PM

I played around some solutions, but things started to get messy; I couldn't manage to create an hand made context to run action just like they do when driven by UI.
Since CM is based on System.Windows.Interactivity (which is strongly bound to the UI) I suspect that this approach would be harder.

So I tried this *very* hacky UI approach (which I feel a bit ashamed for...) and it worked!
(the trick was borrowed from Microsoft.Expression.Interactivity.Media.PlaySoundAction)

 

               public static void ExecuteAction(object target, string action) 
		{
			var dummy = new System.Windows.FrameworkElement {  Visibility = System.Windows.Visibility.Collapsed };
			Action.SetTarget(dummy, target);
			Message.SetAttach(dummy, "Event Loaded = " + action);

			var popup = new System.Windows.Controls.Primitives.Popup { Child = dummy};
			popup.IsOpen = true;

			popup.IsOpen = false;
		}

where:

  • target is the handler of the action (typically, the VM itself)
  • action is the part of the message after the "=" sign  (for example, in "[Event Click] = [MyAction]", the action is simply "MyAction")

I would like to investigate some more strong-typed overloads, sooner or later.

To solve your scenario, you can use it on VM initialization 

 

        public class CalculatorViewModel : Screen
	{
		protected override void OnInitialize()
		{
			base.OnInitialize();
			
			FilterManager.ExecuteAction(this, "VerySlowDivide");
		}


        ...
        }

 

 

Feedbacks are welcome.

 

Oct 25, 2010 at 6:02 PM

Thanks!  I agree it's pretty hacky, but it works well for now.