Question

I getting XamlParseException when try to use converter. I suspect that I made a mistake in converter but can't catch it.

Full error text:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll

Additional information: Cannot create instance of type 'app.Converters.DimensionToText' [Line: 21 Position: 42]

namespace app.Converters
{
    class DimensionToText : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            Dimensions dim = (Dimensions) value;
            //bool param = (bool) parameter;
            return dim.width.ToString().Trim() + "\"x " + dim.length.ToString().Trim() + "\"x " + dim.height.ToString().Trim() + "\"";
        }

        public object ConvertBack(object value, Type targetType,
                object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}

XAML parts:

xmlns:converter="clr-namespace:app.Converters"
...
<phone:PhoneApplicationPage.Resources>
    <converter:DimensionToText x:Key="DimensionToText"/>
</phone:PhoneApplicationPage.Resources>
...
<TextBlock Style="{StaticResource PhoneTextNormalStyle}"> 
    <Run Text="Dimensions:"/>
    <Run Text="{Binding information.dimensions, Converter={StaticResource DimensionToText}}"/>
</TextBlock>

Strangely in design time converter works just fine. Any suggestions appreciated

Was it helpful?

Solution

Make your converter public

namespace app.Converters
{
    public class DimensionToText : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            Dimensions dim = (Dimensions) value;
            //bool param = (bool) parameter;
            return dim.width.ToString().Trim() + "\"x " + dim.length.ToString().Trim() + "\"x " + dim.height.ToString().Trim() + "\"";
        }

        public object ConvertBack(object value, Type targetType,
                object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top