I recently ported our Windows Phone application FlightInfo to a Windows 8 version.
This was a good learning exercise for learning how to port Windows Phone code to Windows 8 Store code. Much of it went smoothly, but I did have problems, converting convertors. Convertors in Windows Phone are great, and FlightInfo uses them to colour code your flight status, giving you a much easier 'glance and go' experience.
I converted quite a lot of the code, and then came to the convertors. I copied the relevant XAML to my W8 project, and then added the C# code. I was expecting it to work straight away, but I wasn't really surprised when it didn't. So, I did some searching, and found out the differences. What I discovered may help someone else.
The XAML for the Windows Phone code was...
xmlns:coding4fun="clr-namespace:Coding4Fun.Phone.Controls;assembly=Coding4Fun.Phone.Controls"
xmlns:APPAFlightInfo_Converters="clr-namespace:APPA_FlightInfo.Converters"
xmlns:local="clr-namespace:APPA_FlightInfo"
Windows 8
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:APPA_Flight_Info_W8.Converters"
x:Class="APPA_Flight_Info_W8.MainPage"
and then Windows Phone declared in the PhoneApplicationPage resources...
<phone:PhoneApplicationPage.Resources>
<APPAFlightInfo_Converters:FlightStatusToBrushConverter x:Key="BrushConverter"/>
Similarly for Windows 8...
<Page.Resources>
<converters:FlightStatusToBrushConverter x:Key="BrushConverter"/>
and then for Windows Phone the convertor is called on a bound textblock...
<TextBlock Height="70" HorizontalAlignment="Left" Margin="12,467,0,0" Name="txtStatus" Text="{Binding FlightStatus}" VerticalAlignment="Top" Width="434" FontSize="26" TextWrapping="Wrap" FontWeight="Bold" Foreground="{Binding FlightStatus, Converter={StaticResource BrushConverter}}" />
Similarly, for Windows 8, we are bound to a textblock
<TextBlock Text="{Binding FlightStatus}" TextWrapping="Wrap" Margin="12,0,0,0" FontSize="32" FontWeight="Bold" x:Name="tbArrStatus" Foreground="{Binding FlightStatus, Converter={StaticResource BrushConverter}}" />
Finally, the C# class for Windows Phone was...
namespace APPA_FlightInfo.Converters
{
public class FlightStatusToBrushConverter : IValueConverter
{
public object Convert(object status, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
modMain.tilecolour = 0;
And for Windows 8
namespace APPA_Flight_Info_W8.Converters
{
public class FlightStatusToBrushConverter : IValueConverter
{
public object Convert(object status, Type targetType, object parameter, string language)
{
Notice that the last parameter for Windows Phone is a 'culture', whereas for Windows 8 we just declare a string for language.
For the C# code, we could use an #if on the declaration to share code.
#if WINDOWS_PHONE
public object Convert(object status, Type targetType, object parameter, System.Globalization.CultureInfo culture)
#else
public object Convert(object status, Type targetType, object parameter, string language)
#endif
So, the main differences are in the parameter type in the .cs file, and the declaration of the namespace in the XAML form.
Posted
Jan 20 2013, 06:55 PM
by
Pete Vickers