Bug 20857 - ValueConverters throw an exception if they're not used as Static Resources.
Summary: ValueConverters throw an exception if they're not used as Static Resources.
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.0.6188
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-24 16:50 UTC by Chase Florell
Modified: 2015-01-26 03:21 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Chase Florell 2014-06-24 16:50:22 UTC
Given the following ValueConverter

    public class UppercaseValueConverter : IValueConverter
    {
        public UppercaseValueConverter(){}

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = value as string;
            return string.IsNullOrWhiteSpace(str) ? string.Empty : str.ToUpper();
        }

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

If I set it as a StaticResource, it works fine.

  <ContentPage.Resources>
    <ResourceDictionary>
      <converters:UppercaseValueConverter x:Key="UppercaseValueConverter"></converters:UppercaseValueConverter>
    </ResourceDictionary>
  </ContentPage.Resources>
   <Label Text="{Binding SectionName, Converter={StaticResource UppercaseValueConverter}}" />

but if I don't use the StaticResource, it throws an exception

    <Label Text="{Binding SectionName, Converter={converters:UppercaseValueConverter}}" />

>  Missing public default constructor for MarkupExtension converters:UppercaseValueConverter

If I put a breakpoint in the default constructor, I **CAN** see it being hit, so I'm pretty sure that it's being called properly, just not sure why the exception is being thrown.

I personally feel that the ContentPage.Resoruces bit is ugly and overkill. I think it's a bug that it won't initialize the other way.
Comment 1 Eric Maupin 2014-07-17 13:30:44 UTC
Made public at the request of the reporter.
Comment 2 Ram Chandra 2014-08-25 12:13:30 UTC
I have checked this issue and I am able to reproduce this issue: 

Steps I have followed:

1. Create a Blank App (Xamarin.Forms Portable) application
2. Add the following code in .axml file.  ( Change namespace according to user proga

    <ContentPage.Resources>
    <ResourceDictionary>
      <converters:UppercaseValueConverter x:Key="UppercaseValueConverter">       </converters:UppercaseValueConverter>
    </ResourceDictionary>
   </ContentPage.Resources>
	<ContentPage.Content>
    <StackLayout>
	  <Label Text="{Binding SectionName, Converter={converters:UppercaseValueConverter}}"  />
	</StackLayout>
	</ContentPage.Content>

3. Create a new class with following code
      
    public class UppercaseValueConverter :IValueConverter
    {
        public UppercaseValueConverter()
        {
            
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = value as string;
            return string.IsNullOrWhiteSpace(str) ? string.Empty :  str.ToUpper();
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

4. Add the following property in .axml.cs page

        public string SectionName { get; set; }

5. assign the value to the "SectionName" in axml.cs page constructor.
      SectionName = "Test

I observed that when I deploy the application I am also getting the same exception message i.e. "Missing public default constructor for MarkupExtension converters:UppercaseValueConverter"

Environment Info:

Microsoft Visual Studio Professional 2012
Version 11.0.61030.00 Update 4
Microsoft .NET Framework
Version 4.5.50938
Installed Version: Professional
Xamarin   3.3.47.0 (0b2a123923812a88ed3091909478dbe9e0111f00)
Comment 3 Stephane Delcroix 2015-01-26 03:20:36 UTC
This is invalid Xaml. 

    <Label Text="{Binding SectionName, Converter={converters:UppercaseValueConverter}}"  />

this assumes there is a UppercaseValueConverter or UppercaseValueConverterExtension class existing and implementing IMarkupExtension.

Try it on WPF or SL, and you should get the same effects.

If you don't want to use StaticResource markup extension, you can use the element syntax for Binding:

    <Label>
        <Label.Text>
            <Binding Path="SectionName">
                <Binding.Converter>
                    <converters:UppercaseValueConverter/>
                </Binding.Converter>
            </Binding>
        </Label.Text>
    </Label>

This should work, although it's almost never used.

The big advantage of defining the converter in the ResourceDictionary, then referencing it through StaticResource is that the converter is only instantiated once, and reused across multiple StaticResource calls.