public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { byte[] content = value as byte[]; if (content != null) { BitmapImage source = new BitmapImage(); source.SetSource(new MemoryStream(content)); return source; } Uri uri = new UriConverter().Convert(value, targetType, parameter, culture) as Uri; if (uri != null) { string extensions = uri.ToString(); if (StringComparer.OrdinalIgnoreCase.Compare(System.IO.Path.GetExtension(extensions), ".xaml") == 0) { var brush = new BitmapImage(); var stream = Application.GetResourceStream(uri); if (stream != null) { // Load from application resources content = new byte[stream.Stream.Length]; stream.Stream.Read(content, 0, content.Length); SetSourceFromXaml(brush, System.Text.Encoding.UTF8.GetString(content, 0, content.Length)); } else { // Load from web WebClient wc = new WebClient(); wc.DownloadStringCompleted += (s, e) => { if (e.Error == null && !e.Cancelled) { SetSourceFromXaml(brush, e.Result); } }; wc.DownloadStringAsync(uri); } return brush; } else { return new BitmapImage(uri); } } return value; }
public void TestConvertBackRelative() { string relative = "default.aspx"; Uri relativeUri = new Uri(relative, UriKind.RelativeOrAbsolute); UriConverter converter = new UriConverter(); object result = converter.ConvertBack(relativeUri, typeof(string), null, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual(relativeUri.ToString(), (string)result); result = converter.ConvertBack(relativeUri, typeof(Uri), null, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(Uri)); Assert.AreEqual(relativeUri.ToString(), ((Uri)result).ToString()); }
public void TestConvertBackAbsolute() { string absolute = "http://www.SLExtensions.Controls.com"; Uri absoluteUri = new Uri(absolute); UriConverter converter = new UriConverter(); object result = converter.ConvertBack(absoluteUri, typeof(string), null, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual(absoluteUri.ToString(), (string)result); result = converter.ConvertBack(absoluteUri, typeof(Uri), null, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(Uri)); Assert.AreEqual(absoluteUri.ToString(), ((Uri)result).ToString()); }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string strValue = value as string; Uri uri; if (!string.IsNullOrEmpty(strValue)) { uri = new Uri(strValue, UriKind.RelativeOrAbsolute); } else if ((uri = value as Uri) != null) { uri = new UriConverter().Convert(value, targetType, parameter, culture) as Uri; } else return value; return new DeepZoomImageTileSource(uri); }
public void TestConvertBackRelativeWithParamUri() { Uri resultUri = new Uri("default.aspx", UriKind.RelativeOrAbsolute); Uri sourceUri = new Uri("http://www.SLExtensions.Controls.com/default.aspx"); UriConverter converter = new UriConverter(); Uri param = new Uri("http://www.SLExtensions.Controls.com"); object result = converter.ConvertBack(sourceUri, typeof(string), param, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual(resultUri.ToString(), (string)result); result = converter.ConvertBack(sourceUri, typeof(Uri), param, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(Uri)); Assert.AreEqual(resultUri.ToString(), ((Uri)result).ToString()); }
public void TestConvertDirectRelativeWithParamString() { string relative = "default.aspx"; Uri relativeUri = new Uri(relative, UriKind.RelativeOrAbsolute); Uri resultUri = new Uri("http://www.SLExtensions.Controls.com/default.aspx"); UriConverter converter = new UriConverter(); string paramString = "http://www.SLExtensions.Controls.com"; object result = converter.Convert(relative, typeof(Uri), paramString, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(Uri)); Assert.AreEqual(resultUri.ToString(), ((Uri)result).ToString()); result = converter.Convert(relativeUri, typeof(Uri), paramString, CultureInfo.CurrentCulture); Assert.IsInstanceOfType(result, typeof(Uri)); Assert.AreEqual(resultUri.ToString(), ((Uri)result).ToString()); }