Skip to content

Reusing mappings

ldfallas edited this page Aug 31, 2015 · 3 revisions

​A mapping can reuse the actions of other mappings. Avoiding the need of copy/pasting existing actions.​​

In order to make a mapping reusable it must have a name. The CodeMapPackage class has a property named Name which will be used by other mappings to import it. As you can see in the example below. A mapping named System.Windows.UIElement is used to map the event Tap and the declaration of the event handler for the class System.Windows.UIElement .

<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="System.Windows.UIElement" Name="System.Windows.UIElement">
      <map:CodeMapPackage.Maps>
			<map:CodeMap Kind="MemberAccess" MemberName="Tap">
				<map:Rename NewName="Tapped" />
			</map:CodeMap>
			<map:CodeMap Kind="EventDecl" MemberName="Tap">
					<map:ReplaceParameterDeclarationType Position="1">
				<![CDATA[Windows.UI.Xaml.RoutedEventArgs]]>
			</map:ReplaceParameterDeclarationType>
			</map:CodeMap>
		</map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>​​

The example above works when an instance of an UIElement is referenced as an UIElement but when the members are used by Inheriting classes and the referenced as the Child class the members are not mapped. In order to solve this issue we need to import the mappings into the Child classes.

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers"
         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
	<MapUnit.Elements>
		<map:CodeMapPackage Type="Microsoft.Phone.Controls.Pivot">
			<map:CodeMapPackage.Maps>
				<map:CodeMap Kind="MemberAccess" MemberName="MouseMove">
					<map:Rename NewName="PointerMoved" />
				</map:CodeMap>
			</map:CodeMapPackage.Maps>
			<map:CodeMapPackage.ReferencedPackages>
				<x:String>System.Windows.UIElement</x:String>
			</map:CodeMapPackage.ReferencedPackages>
		</map:CodeMapPackage>
	</MapUnit.Elements>
</MapUnit>

Reusing Xaml mappings

Xaml mappings can also be reused to avoid duplication. For example the following mapping brings all the mappings from the CommonEventsToChange .

<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="WebBrowser"
                                ElementNamespace="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
            <xmap:XamlElementMapper.Maps>
               ...
            </xmap:XamlElementMapper.Maps>

   <xmap:XamlElementMapper.ReferencedPackages>
            <x:String>CommonEventsToChange</x:String>
          </xmap:XamlElementMapper.ReferencedPackages>
        </xmap:XamlElementMapper>
    </MapUnit.Elements>
</MapUnit>

CommonEventsToChange is defined as follows:

<xmap:XamlElementMappingActions Name="CommonEventsToChange" >
  <xmap:XamlElementMappingActions.Maps>
     <xmap:XamlMap Kind="Property" PropertyName="Tap">
        <xmap:XamlMap.Action>
           <xmap:ActionSequence>
              <xmap:ChangeEventHandlerEventArgsType NewEventArgsTypeName="Windows.UI.Xaml.Input.TappedRoutedEventArgs" />
              <xmap:RenameProperty NewPropertyLocalName="Tapped" />
           </xmap:ActionSequence>
        </xmap:XamlMap.Action>
     </xmap:XamlMap>
     ...
  </xmap:XamlElementMappingActions.Maps>
</xmap:XamlElementMappingActions>

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally