Cimbalino.Phone.Toolkit.Converters.ResolutionAwareUriConverter.Convert C# (CSharp) Method

Convert() public method

Converts a Uri value to a resolution aware Uri value.
public Convert ( object value, Type targetType, object parameter, System culture ) : object
value object The value produced by the binding source.
targetType System.Type The type of the binding target property.
parameter object The converter parameter to use.
culture System The culture to use in the converter.
return object
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            #if WP8
            if (value == null || string.IsNullOrEmpty(_resolutionSuffix))
            {
                return value;
            }

            var iconUrl = value.ToString().TrimStart('/');

            if (iconUrl.Length > 0)
            {
                var resolutionAwareIconUrl = Path.ChangeExtension(iconUrl, _resolutionSuffix + Path.GetExtension(iconUrl));
                var resolutionAwareIconUri = new Uri(resolutionAwareIconUrl, UriKind.Relative);

                if (CheckIfUriExists)
                {
                    var resouceStreamResult = Application.GetResourceStream(resolutionAwareIconUri);

                    if (resouceStreamResult != null && resouceStreamResult.Stream != null)
                    {
                        resouceStreamResult.Stream.Dispose();

                        return resolutionAwareIconUri;
                    }
                }
                else
                {
                    return resolutionAwareIconUri;
                }
            }

            return value;
            #else
            return value;
            #endif
        }

Usage Example

        public void ConvertFromUriToResolutionAwareUri(int scaleFactor, string expectedValue)
        {
            var converter = new ResolutionAwareUriConverter(scaleFactor);

            var value = converter.Convert(TestValue, typeof(Uri), null, CultureInfo.CurrentCulture);

            Assert.IsNotNull(value);
            Assert.AreEqual(value.ToString(), expectedValue);
        }