Skip to content

RemoveParameter Code Mapping Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Usage

<map:RemoveParameter Position="INDEX-OF-PARAMETER-TO-REMOVE"/>

Description

This code mapping action removes a parameter from a event handler method declaration.

Properties

​Property ​Usage ​Description
Position Required The position of the argument to remove

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 with CodeMaps of type EventDecl

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally