示例#1
0
        /// <summary>
        /// ConvertTo - Attempt to convert an instance of ImageSource to the given type
        /// </summary>
        /// <returns>
        /// The object which was constructoed.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if "value" is null or not an instance of ImageSource,
        /// or if the destinationType isn't one of the valid destination types.
        /// </exception>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="culture"> The CultureInfo which is respected when converting. </param>
        /// <param name="value"> The object to convert to an instance of "destinationType". </param>
        /// <param name="destinationType"> The type to which this will convert the ImageSource instance. </param>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType != null && value is ImageSource)
            {
                ImageSource instance = (ImageSource)value;

                if (destinationType == typeof(string))
                {
                    // When invoked by the serialization engine we can convert to string only for some instances
                    if (context != null && context.Instance != null)
                    {
                        #pragma warning disable 6506
                        if (!instance.CanSerializeToString())
                        {
                            throw new NotSupportedException(SR.Get(SRID.Converter_ConvertToNotSupported));
                        }
                        #pragma warning restore 6506
                    }

                    // Delegate to the formatting/culture-aware ConvertToString method.
                    return(instance.ConvertToString(null, culture));
                }
            }

            // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
            return(base.ConvertTo(context, culture, value, destinationType));
        }
        /// <summary>
        /// Returns true if the given transform can be converted into a string
        /// </summary>
        public override bool CanConvertToString(object value, IValueSerializerContext context)
        {
            ImageSource imageSource = value as ImageSource;

            #pragma warning disable 6506
            return(imageSource != null && imageSource.CanSerializeToString());

            #pragma warning restore 6506
        }
示例#3
0
        /// <summary>
        /// Returns true if this type converter can convert to the given type.
        /// </summary>
        /// <returns>
        /// bool - True if this converter can convert to the provided type, false if not.
        /// </returns>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="destinationType"> The Type being queried for support. </param>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                // When invoked by the serialization engine we can convert to string only for some instances
                if (context != null && context.Instance != null)
                {
                    if (!(context.Instance is ImageSource))
                    {
                        throw new ArgumentException(SR.Get(SRID.General_Expected_Type, "ImageSource"), "context");
                    }

                    #pragma warning suppress 6506 // context is obviously not null
                    ImageSource value = (ImageSource)context.Instance;

                    #pragma warning suppress 6506 // value is obviously not null
                    return(value.CanSerializeToString());
                }

                return(true);
            }

            return(base.CanConvertTo(context, destinationType));
        }