2013-09-23 47 views
0

我有一个网格,在那个网格中有几个图像与其他一些元素。我想创建一个背景图像作为每个图像的静态资源。我明白,它不是不可能的,所以请帮助我。 例如(这是不正确的代码,这是我想要达到windows phone 8创建自定义静态资源

<style x:key="myimage"> 
<Setter property="Image" value="images/loading.png"/> 
</style> 

<image style={staticresource myimage" source={binding someotherimage"/> 

回答

1

我不明白的问题,只是例子,但也许你可以尝试这样的事情:(这是一个例子)

XAML

把这个的PhoneApplicationPage:

xmlns:my="clr-namespace:YOURNAMESPACE" 
    <phone:PhoneApplicationPage.Resources> 
     <my:BinaryToImageSourceConverter x:Key="BinaryToImageSourceConverter1" /> 
    </phone:PhoneApplicationPage.Resources> 

在电网将这个:

<Image Source="{Binding Path=Image, Converter={StaticResource BinaryToImageSourceConverter1}, ConverterParameter=Image, TargetNullValue='/Image/no-foto-60.png'}" Stretch="None" /> 

必须实现类Bin​​aryToImageSourceConverter:的IValueConverter

namespace YOURNAMESPACE 
{ 
    public class BinaryToImageSourceConverter: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value != null && value is byte[]) 
      { 
       try 
       { 
        var bytes = value as byte[]; 
        var stream = new MemoryStream(bytes); 
        var image = new BitmapImage(); 
        image.SetSource(stream); 
        stream.Close(); 
        return image; 
       } 
       catch (Exception) 
       { } 
      } 
      return null; 
     }  
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
}