Skip to content

Writing a simple namespace mapping

ldfallas edited this page Aug 31, 2015 · 3 revisions

The Problem

Some Windows Phone Silverlight APIs have the same class names as their equivalents in UWP . A common difference is the namespace .

For example the following code:

-- Windows Phone Silverlight --

System.Windows.Controls.Button myButton;
myButton = new System.Windows.Controls.Button();

This code must be changed to the following UWP equivalent:

-- Windows UWP --

Windows.UI.Xaml.Controls.Button myButton;
myButton = new Windows.UI.Xaml.Controls.Button();

Conversion

In this case we need to create a 'Type' mapping that allows us to convert type references from System.Windows.Controls.Button to Windows.UI.Xaml.Controls.Button. The following code shows the definition of a MapUnit for the System.Windows.Controls.Button class.

** -- Converter mapping -- ***

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
   xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">

   <map:CodeMapPackage Type="System.Windows.Controls.Button">
      <map:CodeMap Kind="Type">
         <map:ReplaceClassUsage NewNamespace="Windows.UI.Xaml.Controls"
                                NewClassName="Button" />
      </map:CodeMap>
   </map:CodeMapPackage>
</MapUnit>

You can register this mapping by following the instructions in the [Registering new code mappings](Registering new code mappings) .

"Using" namespace reference

When a namespace has completely disappeared , for example the System.Windows.Controls namespace doesn't exist anymore then we need to remove or replace the namespace import declarations at the top of source code files.

For example:

-- Windows Phone Silverlight --

using System.Windows.Controls;

public class MyClass
{
   ...
}

This code could be converted to:

-- Windows UWP --

using Windows.Xaml.UI;

public class MyClass
{
   ...
}

Conversion for using declarations

-- Converter namespace mapping --

<NamespaceMappings xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Namespaces;assembly=Mobilize.ExtensibleMappers">
	<Replace Old="System.Windows.Controls" New="Windows.UI.Xaml.Controls"/>
</NamespaceMappings>

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally