使用转换器时XamlParseException

时间:2014-04-23 19:31:01

标签: c# windows-phone-8 ivalueconverter

尝试使用转换器时出现XamlParseException。我怀疑我在转换器中犯了一个错误,但无法抓住它。

完整错误文字:

  

类型的第一次机会异常   'System.Windows.Markup.XamlParseException'发生在   System.Windows.ni.dll

     

其他信息:无法创建类型实例   'app.Converters.DimensionToText'[行:21位置: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部分:

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>

奇怪的是设计时间转换器工作得很好。任何建议赞赏

1 个答案:

答案 0 :(得分:2)

制作转换器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;
        }
    }
}