Skip to content

WithArgument Code Mappings condition

ldfallas edited this page Aug 31, 2015 · 4 revisions

Description

Applies a condition to the specified argument expression of a method call.

This code mapping condition is useful when trying to test conditions on method call arguments or to capture arguments in variables(using the [AssignName mapping condition]).

Properties

Property ​Usage ​Description
​ SubConditions ​Required / Content property The conditions checked against the specified argument
​Position Required A zero based index of the argument to chosen argument

Example

The following example captures the first (Position = 0) and second (Position = 1) arguments of calls to Microsoft.Phone.Maps.Controls.Map.SetView in order to transform them to calls to MapControl.TrySetView .

<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.Maps.Controls.Map">
      <map:CodeMapPackage.Maps>

        ...
        <map:CodeMap Kind="Call" MemberName="SetView">
          <map:Conditional>
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:WithCalledMemberOwner>
                    <map:AssignName>$mapInstance</map:AssignName>
                  </map:WithCalledMemberOwner>
                  <map:WithArgument Position="0">
                    <map:AssignName>$arg0</map:AssignName>
                  </map:WithArgument>
                  <map:WithArgument Position="1">
                    <map:AssignName>$arg1</map:AssignName>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  await $mapInstance.TrySetViewAsync($arg0, $arg1)
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
            ...
          </map:Conditional>
        </map:CodeMap>
        ...
    </map:CodeMapPackage>
</MapUnit.Elements>
</MapUnit>

Given the following code:

--Windows Phone 8 Silverlight--

void SetMapView(Map aMap)
{
    aMap.SetView(coordinate, 0.5);
}

We can apply this mapping and get the following code:

async void SetMapView(Windows.UI.Xaml.Controls.Maps.MapControl aMap)
{
    await aMap.TrySetViewAsync(coordinate, 0.5);
}

The WithArgument code action is used in this case to capture parts of a expression. The following picture shows how the different parts of the expression are captured.

capturing parts of a call

Notes

  • This condition fails when one of its inner conditions fail
  • This condition can also be used to test conditions of constructor calls.
  • The index with the position of the argument is zero based.

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally