Skip to content

AddParentElementResource Xaml Mapping Action

ldfallas edited this page Aug 31, 2015 · 1 revision

Usage

<map:AddParentElementResource>
   <AddParentElementResource.ReplacementTemplateContents>
         ...XAML TEMPLATE...
   </AddParentElementResource.ReplacementTemplateContents>
</map:AddParentElementResource>

Description

This XAML mapping action allows adding a resource to the top level element (a Page or UserControl) by using a XAML template.

Properties

​Property ​Usage ​Description
ReplacementTemplateContents Content property A XAML template with the value of the new resource being generated
NewResourceNameTemplateVariable Optional The name of a template variable that can be used to refer to the generated resource key

Example

The following example shows how to use this action to extract the value of the Text property from TextBlocks and put them in a resource.

<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='Element'>
          <xmap:ActionSequence>
            <xmap:BindPropertyValueElement ContextKeyName='$txt' PropertyName='Text'/>
            <xmap:AddParentElementResource NewResourceNameTemplateVariable='$resKey'>
              <![CDATA[  <x:String xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> The text: $txt </x:String> ]]>
            </xmap:AddParentElementResource>
            <xmap:SetPropertyValueToSimpleValue AttributeToChangeName='Text' NewPropertyValue='{}{StaticResource $resKey}'/>
          </xmap:ActionSequence>
        </xmap:XamlMap>
      </xmap:XamlElementMapper.Maps>
    </xmap:XamlElementMapper>
  </MapUnit.Elements>
</MapUnit>

By applying this mapping to this source code:

--Windows Phone 8 Silverlight --

<phone:PhoneApplicationPage ... >
    ...
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
    ...
    </Grid>
</phone:PhoneApplicationPage>

We get the following code:

--Windows UWP--

<Page ...>
  <Page.Resources>
    <x:String xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="newResourceResource"> The text: MY APPLICATION </x:String>
    <x:String xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="newResourceResource0"> The text: page name </x:String>
  </Page.Resources>
...
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
      <TextBlock Margin="12,0" Text="{StaticResource newResourceResource}" />
      <TextBlock Margin="9,-7,0,0" Text="{StaticResource newResourceResource0}" />
    </StackPanel>
...
</Page>

Notes

  • The value of the ReplacementTemplateContents must be a valid XAML string

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally