MetroEventToCommand 2.0

Finally…the V2.0 is out !

What’s new :

Added :

  • Windows Phone 8 version
  • Very simple test app
  • You can pass event args to the command

 

Corrected :

  • Memory leak

 

Here is how to use it :

Get the NuGet package (https://nuget.org/packages/MetroEventToCommand/)

Add to your XAML header :

xmlns:metroEventToCommand="using:MetroEventToCommand"

Then, to use it just do like this :

<Button Content="Click Here">
   <metroEventToCommand:EventToCommandManager.Collection>
      <metroEventToCommand:EventToCommand Command="{Binding TestCommand}" CommandParameter="{Binding SimpleParam}" Event="Click"></metroEventToCommand:EventToCommand>
   </metroEventToCommand:EventToCommandManager.Collection>
</Button>

Event args

Now you can pass the event args to the command:

To do this, replace the generic type of your command to the new EventParameter<T> type.

The engine will see your command generic parameter is an EventParameter<T>, so it will populate the Sender and Args properties accordingly. If you have a command parameter, it will be set on the Parameter property.

Example :

XAML

<Button Content="Click here">
   <metroEventToCommand:EventToCommandManager.Collection>
      <metroEventToCommand:EventToCommand Command="{Binding TestCommand}"
            CommandParameter="{Binding TestParameter}"
            Event="Click" />
   </metroEventToCommand:EventToCommandManager.Collection>
</Button>

 

C#

public Command<EventParameter<int>> TestCommand { get; set; }

public MainPage()
{
    this.InitializeComponent();

    TestCommand = new Command<EventParameter<int>>((param) =>
    {
        if ((param.Sender is Button) && (param.Parameter == 111))
            //Do something
        else
        {
            //Do something else
        }
    });

    DataContext = this;
}

 

Pro and cons

The advantages of my solution :

  • Easy
  • Can use several MetroEventToCommand on the same control for different events
  • Can use full databinding capabilities

 

The disadvantages :

  • Using a FrameworkElement put some overhead
  • Lot of useless FrameworkElement properties appearing at the Intellisense

 

Clearly it is not the cleanest solution. But it is an easy one but it works (so far) for the projects I’ve been on.

You can download source and example on Codeplex : http://metroeventtocommand.codeplex.com/

Comments powered by Disqus