2015-08-09 84 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

,工作得很好 –