Skip to content

ReplaceParameterMember Code Action

ldfallas edited this page Aug 17, 2015 · 3 revisions

Description

Replaces properties or method names from parameter values of method declarations.

This mapping action is useful when creating a mapping for an event handler where the class of the event arguments parameter changed.

Properties

​Property ​Usage ​Description
​Position ​Required The position of the parameter to change the property accesses to.
​​Replacements Content property The member name replacement pairs. These pairs are specified using the ParameterMemberReplacement element. See below for more details

The ParameterMemberReplacement inner element

The member name replacement pairs. These pairs are specified using the ParameterMemberReplacement element. It has the following properties:

Attribute name Description
From The name of the original member.
To The name of the member in the target class
ToMemberKind ToMemberKind Can have the values Property or Method. When the value is Method the Action treats the value of To as the name of a method. Causes the action to change the expressions that reference the original property to a method invocation with no parameters. When the value of FromMemberKind is Property. The default value is Property
FromMemberKind FromMemberKind Can have the values Property or Method. When the value is Method the Action treats the value of From as the name of a method. Causes the action to change the invocations of the method From to a Property reference when ToMemberKind has the value Property.

Note: When ToMemberKind and FromMemberKind both have the same value. The Action will proceed to replace the references of the From property to the To Property.

For example:

Examples:

<map:ParameterMemberReplacement From="Cancel" To="Handled"/>​​

In this case by default ToMemberKind and FromMemberKind​ have the value property. Thus all the references of Cancel will be replaced with Handled

<map:ParameterMemberReplacement From="Cancel" To="GetHandled" FromMemberKind="Method"/>​

In this case the references of Cancel will be replaced with invocations to GetHandled.

Examples

This mapping operation is useful to perform changes on event handlers that changed on UWP .

Example 1

In this example is the conversion from BackKeyPress (https://msdn.microsoft.com/en-US/library/windows/apps/xaml/microsoft.phone.controls.phoneapplicationpage.backkeypress%28v=vs.105%29.aspx) to the UWP equivalent BackPressed event (https://msdn.microsoft.com/en-us/library/windows.phone.ui.input.hardwarebuttons.backpressed.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 .) When moving to this event handler we need to change the event handlers from the CancelEventArgs (https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs%28v=vs.110%29.aspx) to the Windows.Phone.UI.Input.BackPressedEventArgs class (https://msdn.microsoft.com/en-us/library/windows.phone.ui.input.backpressedeventargs.aspx). Because the CancelEventArgs class also exist in UWP we cannot simply convert from CancelEventArgs to BackPressedEventArgs. In this case we need to apply a local conversion which is performed using the ReplaceParameterMember and ReplaceParameterDeclarationType mapping operations.

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=ExtensibleMappers">
  <MapUnit.Elements>
    <map:CodeMapPackage Type="Microsoft.Phone.Controls.PhoneApplicationPage">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Type">
          <map:CodeMap.Action>
            <map:ReplaceClassUsage NewNamespace="Windows.UI.Xaml.Controls"
                                   NewClassName="Page" />
          </map:CodeMap.Action>
        </map:CodeMap>

        ...

        <map:CodeMap Kind="MemberAccess" MemberName="BackKeyPress">
          <map:ReplaceWithTemplate>Windows.Phone.UI.Input.HardwareButtons.BackPressed</map:ReplaceWithTemplate>
        </map:CodeMap>
        <map:CodeMap Kind="EventDecl" MemberName="BackKeyPress">
          <map:CodeMap.Action>
            <map:ActionSequence>
              <map:ReplaceParameterMember Position="1">
                <map:ParameterMemberReplacement From="Cancel" To="Handled"/>
              </map:ReplaceParameterMember>
​
              <map:ReplaceParameterDeclarationType Position="1">
                Windows.Phone.UI.Input.BackPressedEventArgs
              </map:ReplaceParameterDeclarationType>
            </map:ActionSequence>
          </map:CodeMap.Action>
        </map:CodeMap>
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

Using this mapping we can convert from the following event handler:

--- Windows Phone 8---

void MyPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (this.NeedToCancel)
    {
       e.Cancel = true;
    }
}

--- UWP ---

void MyPage_BackKeyPress(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (this.NeedToCancel)
    {
       e.Handled = true;
    }
}

Example 2

In this example ​the EventHandler method will replace the type of the of the first parameter from GestureEventArgs to Windows.UI.Xaml.Controls.Maps.MapInputEventArgs​. The method GetPosition is not present in the class Windows.UI.Xaml.Controls.Maps.MapInputEventArgss, but, it has the corresponding property named Position. In order to change the invocations to GetPosition to references to the property Position we need to indicate that GetPosition is in fact a method.

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=ExtensibleMappers">
	<MapUnit.Elements>

		<map:CodeMapPackage Type="Microsoft.Phone.Maps.Controls.Map">
			<map:CodeMapPackage.Maps>

...

				<map:CodeMap Kind="EventDecl" MemberName="Tap">
					<map:CodeMap.Action>
						<map:ActionSequence>
							<map:ReplaceParameterMember Position="1">
								<map:ParameterMemberReplacement From="GetPosition" To="Position" FromMemberKind="Method" />
							</map:ReplaceParameterMember>


							<map:ReplaceParameterDeclarationType Position="1">
								Windows.UI.Xaml.Controls.Maps.MapInputEventArgs
							</map:ReplaceParameterDeclarationType>
						</map:ActionSequence>
					</map:CodeMap.Action>
				</map:CodeMap>

...

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

Using this mapping we can convert from the following event handler:

--- Windows Phone 8---

       private void Map_OnTap(object sender, GestureEventArgs e)
       {
            Point clickLocation = e.GetPosition(Map);
            GeoCoordinate coordinate = Map.ConvertViewportPointToGeoCoordinate(clickLocation);
            SetPushpin(coordinate);((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
        }

--- UWP ---

      private void Map_OnTap(object sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs e)
      {
         Windows.Foundation.Point clickLocation = e.Position;
         Windows.Devices.Geolocation.Geopoint geoPoint;
         Map.GetLocationFromOffset(clickLocation, out geoPoint);
         Windows.Devices.Geolocation.Geopoint coordinate = geoPoint;
         SetPushpin(coordinate);
        ((Windows.UI.Xaml.Controls.AppBarButton)((Windows.UI.Xaml.Controls.CommandBar)BottomAppBar).PrimaryCommands[0]).IsEnabled = true;
      }

Notes

TODO

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally