![](/img/trans.png)
[英]how to store an image received from the web and display it in the windows phone 7 application
[英]Display image based on data received over the internet for windows phone
我通過 Internet 請求數據並通常將其綁定在 XAML 中。
現在我很難訪問接收到的數據,操作它並在用戶控制中顯示它。
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;
我所需要的只是在用戶控制代碼中獲取問題。
如何獲取用戶控制代碼中的數據?
當 containsImage 為 true 時,此樣式將設置 Image Source 屬性:
<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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.