Skip to content

ReplaceWithTemplate Code Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

Replaces the current expression with the result of evaluating a C# code template.

This mapping action is commonly used in conjunction with the [Conditional Code Action](Conditional Code Action) and the [AssignName Code Mappings condition](AssignName Code Mappings condition).

Properties

Property ​Usage ​Description
​CodeTemplate ​Content property / Required The C# code template

Example

Say that we want to replace usages of the System.MessagBox.Show method to usages of the MessageDialog class (https://msdn.microsoft.com/library/windows/apps/br208674)

For example we may have the following Windows Phone 8.0 code:

MessageBox.Show("Hi!");

The mapping definition for this scenario looks like this:

<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.MessageBox">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Call" MemberName="Show">
          <map:Conditional>
            <!-- Case for one argument -->
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:ArgumentCount>1</map:ArgumentCount>
                  <map:WithArgument Position="0">
                    <map:AssignName>$message</map:AssignName>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>

                <map:ReplaceWithTemplate>
                  await (new Windows.UI.Popups.MessageDialog($message)).ShowAsync()
                </map:ReplaceWithTemplate>

              </map:Case.Action>
            </map:Case>
            ...
            <map:Default>
              <map:Keep/>
            </map:Default>
          </map:Conditional>
        </map:CodeMap>
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

The converted code looks like this:

await (new Windows.UI.Popups.MessageDialog("Hi!")).ShowAsync()

Notes

  • The template specified for this code mapping action must be a valid C# expression. If not the mapping action will not be applied.
  • Variables are specified using the dollar sign ($) followed by a sequence of letters
  • Since C# code may contain angle brackets it is recommented to specify the code template in a CDATA block (https://en.wikipedia.org/wiki/CDATA) .

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally