2016-04-26 83 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。 –