在 xaml Windows Universal 中绑定图像源

Binding Imagesource in xaml Windows Universal

我需要将我的图像源绑定到一个 uri,该 uri 由一个静态 uri 和一个我尝试在静态 uri 前面绑定的 uri 组成。 我已经包含了绑定,但我不能将静态 uri 放在它前面: 注意:我正在为 Windows 通用平台

开发
 <Image Source="{Binding InventoryItem.properties.icon_url}"> //Here needs to be the static uri in front of the binding
                                        </Image>

如何实现?

如果我理解得很好,解决方案如下:

1.- 创建转换器:

namespace Converters
{
public class UriConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string relativepath = value as String;
        BitmapImage bi = new BitmapImage();
        bi.UriSource = new Uri($"http://www.yourwebsite.com/{relativepath}.png");
        return bi;
    }

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

2.- 然后添加到你的 XAML

<Page... xmlns:c="using:Converters">
<Page.Resources>
 <c:UriConverter x:Key="UriConverter"/>
 </Page.Resources>

 <Image Source="{Binding InventoryItem.properties.icon_url, Converter={StaticResource UriConverter}}"/>
...
</Page>

如果您需要在多个地方使用,您可以在 App.xaml 中实例化资源转换器。