Forum Xamarin.Forms

Add ValueConversionAttribute to Xamarin.Forms.Core

Matthew-RobbinsMatthew-Robbins AUInsider ✭✭
edited January 2017 in Xamarin.Forms

Summary

Include the ValueConversionAttribute into Xamarin.Forms to enable developers to declare the type-conversion of their value converters.

This attribute is in the PresentationFramework assembly, which we can't reference in a Xamarin.Forms PCL or shared project.

API Changes

Include the following attribute:

public namespace Xamarin.Forms
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class ValueConversionAttribute : Attribute
    {
      public ValueConversionAttribute(Type input, Type output)
      {
      }

      public Type ParameterType { get; set; }
    }
}

Intended Use Case

Developers can annotate their IValueConverter implementations with ValueConversion(typeof(input), typeof(output)) to declare the value conversion input, output and parameter types for other developers.

The ValueConversionAttribute becomes informal usage documentation and allows third party tools (such as MFractor) to understand the type flow of a value converter.

Example Usage:

[ValueConversion(typeof(object), typeof(bool))]
public class IsNullToBooleanConverter : IValueConverter
{
    public IsNullToBooleanConverter()
    {
    }

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

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This attribute is already in use by FormsCommunityToolkit.Converters.

The following MFractor tutorial outlines the benefits given by value converter type-safety:

For example:

Tagged:

Open · Last Updated

Posts

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    Having type information on converters is nice, and would be quite useful in compile time check and casting using XamlC.

    there's 2 approaches for this:

    Type enforcing

    public interface IValueConverter<TFrom,TTo> : IValueConverter
    {
        TTo Convert(TFrom value, Type targetType, object parameter, CultureInfo culture);
        TFrom ConvertBack(TTo value, Type targetType, object parameter, CultureInfo culture);
    }
    

    Pros:
    - The type is enforced at compile time
    - it matches the approach of `IMarkupExtension

    Cons:
    - It diverges from other xaml implementation

    Type Information

    --> Your proposal here

    Pros:
    - it's in line with what other xaml parser expects
    Cons:
    - type is only informal, and not enforced
    - in case of value type, there's still a need to box/unbox
    - it's less discoverable

    I'd be glad with any, but if we go for option 2, we probably need to [Obsolete] IMarkupExtension<T> and introduce MarkupExtensionReturnTypeAttribute (https://msdn.microsoft.com/en-us/library/system.windows.markup.markupextensionreturntypeattribute(v=vs.110).aspx)

  • Matthew-RobbinsMatthew-Robbins AUInsider ✭✭

    @StephaneDelcroix both approaches work for me but I'm personally in favour of option 2 as:

    • It's inline with other Xaml implementations.
    • It allows value converters to have multiple valid inputs and outputs. The IValueConverter<TFrom, ,TTo> strictly ties a converter to an exact type.

    That said, a typed value converter interface is great because it enforces compile time safety

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @Matthew-Robbins I do not like the AllowMultiple = true as it will not allow XamlC to make any type-based decision. What's the benefit (for MFractor, and users) of having multiple attributes ? What scenarios do you have in mind ?

  • Matthew-RobbinsMatthew-Robbins AUInsider ✭✭

    Frankly, the AllowMultiple = true is only there because it aligns it with WPFs ValueConversionAttribute.

    I'm perfectly happy for it to not exist; it would remove edge cases I'd need to account for in MFractor... which moves us back to just having typed value converters.

Sign In or Register to comment.