2016-04-26 79 views
2

我通過互聯網請求數據,並通常將其綁定到XAML中。基於通過互聯網接收的Windows phone數據顯示圖像

現在我無法訪問接收到的數據,操作它並將其顯示在用戶控件中。

public partial class FullQuestionUserControl : UserControl 
    { 
     public FullQuestionUserControl() 
     { 
       InitializeComponent(); 
     } 
    } 

我的模型問題包含諸如id,authorFullName,text,containsImage等字段。

這是我如何綁定:

<TextBlock Style="{StaticResource TextSmall}" TextWrapping="Wrap" 
     Text="{Binding SelectedQuestion.authorFullName}" /> 

我需要檢查containsImage。如果爲true,則使用id格式化一個新字符串並顯示它。

我知道如何顯示圖像:

var bi = new BitmapImage(new Uri(url)); 
    this.QuestionImage.Source = bi; 

所有我需要的是讓用戶控制代碼的問題。

如何獲取用戶控制代碼中的數據?

+0

你可以使用數據綁定而不是代碼隱藏來做你所需要的,但是你的問題不是很清楚。如果containsImage屬性爲true,那麼您需要使用包含id的格式化URL來創建BitmapSource? –

回答

2

這種樣式設置圖像源屬性時containsImage是正確的:

<Image> 
    <Image.Resources> 
     <stackoverflow:IdToImageSourceConverter x:Key="IdToImageSourceConverter"/> 
    </Image.Resources> 
    <Image.Style> 
     <Style TargetType="Image"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding SelectedQuestion.containsImage}" Value="True"> 
        <Setter Property="Source" Value="{Binding SelectedQuestion.id, Converter={StaticResource IdToImageSourceConverter}}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Image.Style> 
</Image> 

該轉換器將id屬性,格式的URL,並返回一個BitmapImage的圖片來源:

class IdToImageSourceConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var idValue = value.ToString(); 

     var url = string.Format("http://myurl.com/{0}", idValue); 

     return new BitmapImage(new Uri(url)); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

感謝您的幫助。我使用轉換器,就像你建議的那樣。雖然,觸發器顯然不適用於Windows Phone。 –