Silverlight - ImageSource obligatorio para Fill del rectángulo
-
02-10-2019 - |
Pregunta
Blend 4 me está diciendo que esto es válido y su margen de beneficio no me dice por qué:
<ImageBrush Stretch="Fill" ImageSource="{Binding Avatar, Mode=OneWay}"/>
Me estoy tirando de los datos de una cuenta de Twitter, el ahorro a un ImageSource, y luego enlazarlo a un ImageBrush (como se verá más adelante) para ser utilizado como el Relleno de un rectángulo. Aquí es más contexto:
<Rectangle x:Name="Avatar" RadiusY="9" RadiusX="9" Width="45" Height="45" VerticalAlignment="Center" HorizontalAlignment="Center" >
<Rectangle.Fill>
<ImageBrush Stretch="Fill" ImageSource="{Binding Avatar, Mode=OneWay}"/>
</Rectangle.Fill>
</Rectangle>
Estoy usando esto dentro de un control de usuario de Silverlight, que se utiliza dentro de una aplicación de Silverlight. ¿Alguna idea sobre lo que podría ser el problema?
Solución
La unión no se puede aplicar a ImageSource de un ImageBrush, o eso parece. Estoy teniendo un problema similar y buscar alternativas.
Otros consejos
No se puede obligar a la ImageSource de un ImageBrush, sin embargo se puede unir a la propiedad relleno de una forma. Por lo tanto, las siguientes obras:
<Rectangle Name="myRect" Fill="{Binding Avatar}"/>
Con una clase algo como esto:
public class AvatarClass
{
public ImageBrush Avatar { get; set; }
}
y el código detrás de la siguiente manera:
myRect.DataContext = new AvatarClass{
Avatar = new ImageBrush {
ImageSource = new BitmapImage(avatarUri)}};
Aquí van: WPF / Silverlight
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convertor="clr-namespace:WpfApplication1.Converters"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<convertor:RectangleImageFillConvertor x:Key="RectangleImageFillConvertor" />
</Window.Resources>
<Grid>
<Rectangle HorizontalAlignment="Center"
RadiusX="10"
RadiusY="10"
Width="200"
Height="200"
Fill="{Binding ImageUrl, Converter={StaticResource RectangleImageFillConvertor}}"/>
</Grid>
</Window>
Blockquote
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string ImageUrl { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ImageUrl = "http://www.deargrumpycat.com/wp-content/uploads/2013/02/Grumpy-Cat1.jpg";
}
}
}
cita en bloque Blockquote
namespace WpfApplication1.Converters
{
public class RectangleImageFillConvertor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return new ImageBrush(new BitmapImage(new Uri(value as string, UriKind.RelativeOrAbsolute)));
//if silverlight
// return new ImageBrush{ ImageSource = new BitmapImage(new Uri(value as string, UriKind.RelativeOrAbsolute))};
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}