2015-08-09 83 views
0

我需要将我的图像源绑定到由一个静态uri和一个uri组成的uri,我试图在静态uri前绑定这个uri。 我已经包含了结合,但我不能把静态URI在它的面前: 注意:我开发的Windows通用平台在xaml中绑定Imagesource Windows通用

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

如何实现这一目标?

+0

你能写一个什么值需要icon_url的例子吗?是完整的uri还是需要转换它? –

+0

该icon_url是一个特定的部分,例如,静态URL是:“http://stackoverflow.com/questions/”,并且变化的url(icon_url)是例如“3190499”。我已经通过将每个列表项目的这些URL组合起来解决了这个问题,但是获得更简单的xaml解决方案会很有趣。谢谢, –

回答

0

如果我的理解以及解决方案如下:

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中的资源转换器。

+0

,工作得很好 –