Skip to content

AddPreStatementFromTemplate Code Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

This mapping action is used to add a statement before the statement where the current expression is located.

Properties

Property ​Usage ​Description
CodeTemplate ​Content property / Required The name of the new method to be called

Example

Say that we want to convert from Microsoft.Phone.Tasks.SmsComposeTask to Windows.ApplicationModel.Char.ChatMessage. When converting the To property to UWP we need to make sure that the list of Recipients is empty. We wil want todo the following conversion:

-- Windows Phone 8 Silverlight--

...
SmsComposeTask tsk = new SmsComposeTask();
...
tsk.To = user;
...

We want to convert this code to:

-- Windows UWP --

ChatMessage tsk =...;
...
tsk.Recipients.Clear();
tsk.Recipients.Add(user);

To write a conversion for this scenario we can write the following mapping:

<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="Microsoft.Phone.Tasks.SmsComposeTask">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Type">
          <map:CodeMap.Action>
            <map:ReplaceClassUsage
                 NewNamespace="Windows.ApplicationModel.Chat"
                 NewClassName="ChatMessage" />
          </map:CodeMap.Action>
        </map:CodeMap>

        <map:CodeMap Kind="Assign" MemberName="To">
          <map:Conditional>
            <map:Case>
              <map:Case.Condition>
                <map:WithAssignment>
                  <map:WithAssignmentLeftSide>
                    <map:WithLeftSideOfDottedAccess>
                      <map:AssignName>$owner</map:AssignName>
                    </map:WithLeftSideOfDottedAccess>
                  </map:WithAssignmentLeftSide>
                  <map:WithAssignmentRightSide>
                    <map:AssignName>$right</map:AssignName>
                  </map:WithAssignmentRightSide>
                </map:WithAssignment>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ActionSequence>
                  <map:AddPreStatementFromTemplate>
                    <![CDATA[
                  $owner.Recipients.Clear();
                  ]]>
                  </map:AddPreStatementFromTemplate>
                  <map:ReplaceWithTemplate> $owner.Recipients.Add($right);</map:ReplaceWithTemplate>
                </map:ActionSequence>
              </map:Case.Action>
            </map:Case>
            <map:Default>
              <map:Keep></map:Keep>
            </map:Default>
          </map:Conditional>
        </map:CodeMap>


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

Notes

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally