I've developed app on windows phone 8.1. Several of my pages have the custom converter; for example:
using System;
using System.Globalization;
using Windows.UI.Xaml.Data;
namespace XXXXX.Model.Converters
{
public sealed class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var date = (DateTime)value;
return date.ToString("g", new CultureInfo("it-IT"));
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
In the XAML i have:
<Page
x:Class="XXXXX.Views.VehiclesView"
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"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:XXXXX.Model.Converters"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
Foreground="{StaticResource PhoneForegroundBrush}">
<Page.Resources>
<converters:DateTimeToStringConverter x:Key="DateTimeToStringConverter" />
<converters:StringFormatConverter x:Key="StringFormatConverter" />
</Page.Resources>
And use it in this way:
<TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding Distance, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0} Km'}"></TextBlock>
1) Why does the compiler gives me this error?
The name "DateTimeToStringConverter" does not exist in the namespace using:XXXXX.Model.Converters
2) Why does it work if I change the target platform to x86?
3) If I wanted to work in XAML and not in Code Behind, are there alternatives to the Converter?
Try to move converters in dedicate class library with target platform "AnyCPU", and reference it in your application project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With