Skip to content

ReplaceEventHandlerBodyWithTemplate Xaml Mapping Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

Replaces the content of an event handler with the evaluation of a C# code template.

Properties

Property ​Usage ​Description
​CodeTemplate ​Content property / Required The C# code template

Example

The following mapping will add an if statement that wraps the method body with a condition regarding its first parameter.

<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="TextBlock" ElementNamespace="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
         <xmap:XamlElementMapper.Maps>
            <xmap:XamlMap Kind="Property" PropertyName="Tap">
               <xmap:XamlMap.Action>
                  <xmap:ActionSequence>
                     <xmap:ChangeEventHandlerEventArgsType
                        NewEventArgsTypeName="Windows.UI.Xaml.Input.TappedRoutedEventArgs" />
                     <xmap:ReplaceEventHandlerBodyWithTemplate >
                        <![CDATA[ if($parameter1Name != null) $body;  ]]>
                     </xmap:ReplaceEventHandlerBodyWithTemplate>
                     <xmap:RenameProperty NewPropertyLocalName="Tapped" />
                  </xmap:ActionSequence>
               </xmap:XamlMap.Action>
            </xmap:XamlMap>
         </xmap:XamlElementMapper.Maps>
      </xmap:XamlElementMapper>
   </MapUnit.Elements>
</MapUnit>

Say that we have the following code:

--XAML (Windows Phone Silverlight)--

<TextBlock x:Name="someText" Tap="TextBlock_Tap">Some text</TextBlock>

--C#(Windows Phone Silverlight)--

private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   MessageBox.Show("Hello!");
}

After running the conversion tool with this new mapping we get the following code:

--XAML (UWP)--

<TextBlock x:Name="someText" Tapped="TextBlock_Tap">Some text</TextBlock>

--C# (UWP)--

private async void TextBlock_Tap(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
   if ( e != null )
   {
      await (new Windows.UI.Popups.MessageDialog("Hello!")).ShowAsync();
   }
}

Predefined template variables

​Variable name Description
$parameter0Name, $parameter1Name, ..., $parameterXName ​The names of the parameters of the current method. Parameter names are 0 based.
​$body ​The block with the body of the current method

Notes

TODO

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally