Skip to content

ElementContainsAttribute Xaml Mapping Condition

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

Verifies that the current element contains the specified element and that its value matches with the specified regular expression.

Properties

Property ​Usage ​Description
​PropertyLocalName Required The local name of the property to be verified
​​PropertyValueRegex ​Required ​A regular expression to check the value of the property

Example

We want to comment out Setter instances that have references to a Windows Phone static resource.

For example:

--Windows Phone 8 Silverlight--

...
<Style TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
</Style>
...

The mapping definition looks like this:

<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="Setter" ElementNamespace="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
            <xmap:XamlElementMapper.Maps>
                <xmap:XamlMap Kind="Element">
                    <xmap:XamlMap.Action>
                        <xmap:CommentOutElement>
                            <xmap:CommentOutElement.Condition>
                                <xmap:ElementContainsAttribute PropertyLocalName="Value" PropertyValueRegex="^.StaticResource Phone.*" />
                            </xmap:CommentOutElement.Condition>
                        </xmap:CommentOutElement>
                    </xmap:XamlMap.Action>
                </xmap:XamlMap>
            </xmap:XamlElementMapper.Maps>
        </xmap:XamlElementMapper>
    </MapUnit.Elements>
</MapUnit>

The converted code looks like this:

<Style TargetType="TextBlock">
  <!--<Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />-->
</Style>

Here we used the ElementContainsAttribute condition to determine if we will comment out the current XAML element.

Notes

  • The regular expression is a common .NET regular expression.

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally