-
Notifications
You must be signed in to change notification settings - Fork 5
AddStatementToConstructorFromTemplate Xaml Mapping Action
Adds an statement to the constructor of the current XAML page. The statement is specified using a code template as with the ReplaceWithTemplate Code Action.
| Property | Usage | Description |
|---|---|---|
| CodeTemplate | Required / Content property | A C# code template for the statement to be added . |
| Variable name | Description |
|---|---|
| $propertyValue | the value of the Xaml property being mapped as a C# identifier |
| $propertyValueAsString | the value of the Xaml property being mapped as a C# string literal |
| $elementName | the name of the current element as a C# identifier |
Say that we want to create a mapping that moves the PhoneApplicationPage.BackKeyPress event (https://msdn.microsoft.com/en-US/library/windows/apps/xaml/microsoft.phone.controls.phoneapplicationpage.backkeypress(v=vs.105).aspx) to the Windows.Phone.UI.Input.HardwareButtons.BackPressed event which can only be set in code. We can write the following code:
<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 will take the value of the BackKeyPress property and used to apply the code template to generate an statement.
For example for :
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
BackKeyPress="PhoneApplicationPage_BackKeyPress"
>
...
</phone:PhoneApplicationPage>This will generate the following change:
public MainPage()
{
InitializeComponent();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += PhoneApplicationPage_BackKeyPress;
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}The element name can be used in the code template. For example, say that we want set the value of a property in the constructor instead of using a Xaml element.
For example:
-- Windows Phone Silverlight --
<cntrls:DatePicker x:Name="datePicker" Value="1/1/2001" />We can write a mapping for this property as follows:
<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="DatePicker"
ElementNamespace="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
<xmap:XamlElementMapper.Maps>
...
<xmap:XamlMap Kind="Property" PropertyName="Value" >
<xmap:XamlMap.Action>
<xmap:ActionSequence>
<xmap:AddStatementToConstructorFromTemplate>
<![CDATA[ $elementName.Date = System.DateTimeOffset.Parse($propertyValueAsString); ]]>
</xmap:AddStatementToConstructorFromTemplate>
<xmap:RemoveAttribute/>
</xmap:ActionSequence>
</xmap:XamlMap.Action>
</xmap:XamlMap>
</xmap:XamlElementMapper.Maps>
</xmap:XamlElementMapper>
</MapUnit.Elements>
</MapUnit>Now the constructor will have the following code:
--- UWP ---
...
public Form1()
{
datePicker.Date = System.DateTimeOffset.Parse("1/1/2001");
}- The statement must be a valid C# statement (see )
- The statement is added at the end of the constructor
- The '$elementName' template variable has a valid value if the current element has the x:Name property.
- The '$propertyValue' template variable has a valid value if the value of the current property can be represented as a C# indentifier (see https://msdn.microsoft.com/en-us/library/aa664670.aspx)
Contact us for more information
Overview
Writing mappings
Code Mapping Actions
- ActionSequence
- AddHelper
- AddNamespaceImport
- AddPreStatementFromTemplate
- CommentOut
- Conditional
- Keep Code Mapping Action
- MarkAsNotMapped
- RedirectCall
- RedirectCallToInnerMember
- RedirectIndexer
- RedirectProperty
- RemoveCurrentStatement
- RemoveParameter
- ReplaceClassUsage
- ReplaceMethodBodyWithTemplate
- ReplaceParameterDeclarationType
- ReplaceParameterMember
- ReplaceParameterValue
- ReplaceWithMethodCall
- ReplaceWithProperty
- ReplaceWithTemplate
Code Mapping Conditions
- AllConditionsApply
- ArgumentCount
- AssignName
- AssignNameToArgumentRange
- IsExpressionOfType
- IsStringLiteralMatchingRegex
- WithArgument
- WithAssignment
- WithAssignmentLeftSide
- WithAssignmentRightSide
- WithCalledMemberOwner
- WithCalledMethodExpression
- WithConstructorCall
- WithLambdaExpressionBody
- WithLambdaExpressionParameter
- WithLeftSideOfDottedAccess
- WithMemberInitValue
- WithMethodCall
XAML mapping actions
- ActionSequence
- AddStatementToConstructorFromTemplate
- BindPropertyValueElement Xaml mapping action
- ChangeEventHandlerEventArgsType
- CommentOutElement
- CommentOutProperty
- MarkAsNotMapped
- MoveValueToContentProperty
- RemoveNamespaceDeclaration
- RenameElement
- RenameProperty
- ReplaceAttributeValue
- ReplaceEventHandlerBodyWithTemplate
- ReplaceEventHandlerParameterMember
- ReplaceNamespaceDeclaration
- ReplacePropertyValueWithParentResource
- ReplaceStaticResourceWithThemeResource
- SetPropertyValueToComplexElement
- SetPropertyValueToSimpleValue
- WrapContent
XAML mapping conditions
Misc