Skip to content

Writing a simple namespace mapping for a control

ldfallas edited this page Aug 31, 2015 · 3 revisions

Windows Phone Silverlight applications use XAML to define Phone pages. This files contain elements that must be changed when converting the application to UWP.

For example the Microsoft.Phone.Controls.WebBrowser element :

-- Windows Phone Silverlight XAML --

<phone:PhoneApplicationPage
    x:Class="TesPhoneWebBrowsertApp.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:d="http://schemas.microsoft.com/expression/blend/2008">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
      <phone:WebBrowser x:Name="wb"  Grid.Row="0"/>
    </Grid>

</phone:PhoneApplicationPage>

The equivalent element on UWP is the Windows.UI.Xaml.Controls.WebView control. So a desired conversion for this code could be:

-- Windows UWP --

<Page x:Class="TesPhoneWebBrowsertApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
  <Grid x:Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>



      <WebView x:Name="wb" Grid.Row="0">
      <!--IsScriptEnabled="True"-->
    </WebView>
  </Grid>
</Page>

The changes required for this scenario are the following:

  1. Change the name of the element from WebBrowser to WebView
  2. Change the xml namespace of the element from clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone to http://schemas.microsoft.com/winfx/2006/xaml/presentation

Conversion

Now to convert this code using the conversion tool we need to define a Xaml mapping . These mappings act on XAML elements the same way code mappings act of API usages.

-- UWP conversion tool Xaml mapping --

<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="WebBrowser"
         ElementNamespace="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
         <xmap:XamlElementMapper.Maps>
            <xmap:XamlMap Kind="Element">
               <xmap:XamlMap.Action>
                  <xmap:RenameElementMappingAction
                                NewName="WebView"
                                NewNamespace="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
               </xmap:XamlMap.Action>
            </xmap:XamlMap>
         </xmap:XamlElementMapper.Maps>
      </xmap:XamlElementMapper>
   </MapUnit.Elements>
</MapUnit>

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally