Skip to content

ReplaceParameterValue Code Action

ldfallas edited this page Sep 18, 2015 · 4 revisions

Usage

<map:ReplaceParameterValue Position="PARAMETER-TO-REPLACE-POSITION">
     C#-CODE-TEMPLATE
</map:ReplaceParameterValue>

Properties

Property ​Usage ​Description
Position ​Required A zero-based index of the parameter to replace the value to
CodeTemplate Optional / Content property A code template with the expression to initialize the argument

Predefined template variables

Variable name Description
$parameter0Name The name of the first parameter of the current event handler
$parameter1Name The name of the second parameter of the current event handler
... ...
$parameterNName The name of the nth parameter of the current event handler

Description

Replaces the value of the specified parameter by declaring a local variable with the same name of the given parameter.

This mapping operation is useful when a new event handler requires a parameter value that is not available on the target platform.

Example

In the following example we modify an event handler of the Contacts.SearchCompleted event to be complatible with a Task.ContinueWith call.

-- Windows Phone 8 Silverlight --

public void SearchInContacts()
{
    var contacts = new Contacts();
    contacts.SearchCompleted += contacts_SearchCompleted;
    contacts.SearchAsync(searchFilter, filterKind, null);
}

void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    foreach(var result in e.Results)
    {
        System.Diagnostics.Debug.WriteLine(result.DisplayName);
    }
}

The code that we want to get is the following:

-- Windows UWP --

public async void SearchInContacts()
{
    var contacts = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync();
    //contacts.SearchCompleted += contacts_SearchCompleted;
    ;
    (contacts.GetContactReader(
        new Windows.ApplicationModel.Contacts.ContactQueryOptions(searchFilter, filterKind))
        .ReadBatchAsync())
            .AsTask()
            .ContinueWith(contacts_SearchCompleted,
                            System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
}

void contacts_SearchCompleted(System.Threading.Tasks.Task<Windows.ApplicationModel.Contacts.ContactBatch> sender)
{
    var e = sender.Result;
    foreach ( var result in e.Contacts )
    {
    System.Diagnostics.Debug.WriteLine(result.DisplayName);
    }
}

Here's the mapping to do this change

<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.UserData.Contacts">
      <map:CodeMapPackage.Maps>
        ...
        <map:CodeMap Kind="EventDecl" MemberName="SearchCompleted">
          <map:ActionSequence>
            <map:ReplaceParameterDeclarationType Position="0">
              <![CDATA[System.Threading.Tasks.Task<Windows.ApplicationModel.Contacts.ContactBatch>]]>
            </map:ReplaceParameterDeclarationType>
            <map:ReplaceParameterValue Position="1">
              $parameter0Name.Result
            </map:ReplaceParameterValue>
            <map:RemoveParameter Position="1"/>
          </map:ActionSequence>
        </map:CodeMap>
        ...
      </map:CodeMapPackage.Maps>

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

Part of the operations we want to perform is to remove the second argument from the current event handler and reuse its variable name (to avoid changes to the event handler body).

Notes

  • This code mapping action only works when applied to event handlers. That is when used as part of a CodeMap EventDecl mapping.

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally