Skip to content

ReplaceMethodBodyWithTemplate Code Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

Replaces the body of a method declaration where this action is applied.

This action is commonly used when creating a mapping for an event handler. See [Writing a mapping for event handlers](Writing a mapping for event handlers).

Properties

Property ​Usage ​Description
​CodeTemplate ​Content property / Required The C# code template. This template must specify a valid code block or statement.

Example

The following mapping will add an if statement that wraps the method body with a condition regarding its first parameter.

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
   xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">
   <MapUnit.Elements>
      <map:CodeMapPackage Type="System.Windows.UIElement" Name="System.Windows.UIElement">
         <map:CodeMapPackage.Maps>

            ...

            <map:CodeMap Kind="EventDecl" MemberName="Tap">
               <map:ActionSequence>
                  <map:ReplaceParameterDeclarationType Position="1">
                     <![CDATA[Windows.UI.Xaml.RoutedEventArgs]]>
                  </map:ReplaceParameterDeclarationType>
                  <map:ReplaceMethodBodyWithTemplate>
                     <![CDATA[if ($parameter0Name != null) $body;]]>
                  </map:ReplaceMethodBodyWithTemplate>
               </map:ActionSequence>
            </map:CodeMap>

            ...
         </map:CodeMapPackage.Maps>
      </map:CodeMapPackage>
   </MapUnit.Elements>
</MapUnit>

Say that we have the following code:

public int Subscribe()
{

   someText.Tap += someText_Tap;
}

void someText_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   System.Diagnostics.Debug.WriteLine("Tap!");
}

After running the conversion tool with this new mapping we get the following code:

public int Subscribe()
{
   someText.Tapped += someText_Tap;
}

void someText_Tap(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
   if ( sender != null )
   {
      System.Diagnostics.Debug.WriteLine("Tap!");
   }
}

Predefined template variables

​Variable name Description​
$parameter0Name, $parameter1Name, ..., $parameterXName ​The names of the parameters of the current method. Parameter names are 0 based.
​$body ​The block with the body of the current method

Notes

TODO

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally