Skip to content

ReplaceEventHandlerParameterMember Xaml Mapping Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

This mapping action perform changes on member accesses of event handler parameters. This mapping action is useful when creating a mapping for an event handler with different event arguments.

This is the XAML equivalent of the [ReplaceParameterMember Code Action](ReplaceParameterMember Code Action) .

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 details

The ParameterMemberReplacement 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.

<xmap: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

<xmap:ParameterMemberReplacement From="Cancel" To="Handled" FromMemberKind="Method"​/>

In this case the references of Cancel will be replaced with invocations to GetHandled​()

Example 1

This mapping operation is useful to perform changes on event handlers that changed on UWP . One 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. For example:

<MapUnit xmlns='clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers'
         xmlns:xmap='clr-namespace:Mobilize.XamlMappers;assembly=Mobilize.XamlMapper'
         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >
    <MapUnit.Elements>
        <xmap:XamlElementMapper ElementName="PhoneApplicationPage" ElementNamespace="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
            <xmap:XamlElementMapper.Maps>

              ...

              <xmap:XamlMap Kind="Property" PropertyName="BackKeyPress" >
                <xmap:XamlMap.Action>
                  <xmap:ActionSequence>
                    <xmap:AddStatementToConstructorFromTemplate>
                      <![CDATA[ Windows.Phone.UI.Input.HardwareButtons.BackPressed += $propertyValue;  ]]>
                    </xmap:AddStatementToConstructorFromTemplate>
                  <xmap:ChangeEventHandlerEventArgsType NewEventArgsTypeName="Windows.Phone.UI.Input.BackPressedEventArgs" />
                  <xmap:ReplaceEventHandlerParameterMember Position="1">
                    <xmap:ParameterMemberReplacement From="Cancel" To="Handled"/>
                  </xmap:ReplaceEventHandlerParameterMember>
                  </xmap:ActionSequence>
                </xmap:XamlMap.Action>
              </xmap:XamlMap>

            </xmap:XamlElementMapper.Maps>
            <xmap:XamlElementMapper.ReferencedPackages>
                <x:String>CommonAttributesToRemove</x:String>
                <x:String>CommonPostMappingActions</x:String>
            </xmap:XamlElementMapper.ReferencedPackages>
        </xmap:XamlElementMapper>
    </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.MapInputEventArgs, 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=Mobilize.ExtensibleMappers'
         xmlns:xmap='clr-namespace:Mobilize.XamlMappers;assembly=Mobilize.XamlMapper'
         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >
    <MapUnit.Elements>
        <xmap:XamlElementMapper ElementName="Map" ElementNamespace="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps">
            <xmap:XamlElementMapper.Maps>
...
              <xmap:XamlMap Kind="Property" PropertyName="Tap" >
                <xmap:XamlMap.Action>
                  <xmap:ActionSequence>
                    <xmap:ChangeEventHandlerEventArgsType NewEventArgsTypeName="Windows.UI.Xaml.Controls.Maps.MapInputEventArgs" />
                    <xmap:ReplaceEventHandlerParameterMember Position="1">
                      <xmap:ParameterMemberReplacement From="GetPosition" To="Position" FromMemberKind="Method"/>
                    </xmap:ReplaceEventHandlerParameterMember>
                  </xmap:ActionSequence>
                </xmap:XamlMap.Action>
              </xmap:XamlMap>
...
            </xmap:XamlElementMapper.Maps>
        </xmap:XamlElementMapper>
    </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