windows phone 8创建自定义静态资源

时间:2013-09-23 10:59:59

标签: windows-phone-7 windows-phone-8 windows-phone

我有一个网格,在那个网格中有几个图像和其他一些元素。我想创建一个背景图像作为每个图像的静态资源。我明白这不是不可能所以请帮助我。 例如(这不是正确的代码,这只是我想要实现的例子

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

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

1 个答案:

答案 0 :(得分: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" />

你必须实现BinaryToImageSourceConverter类: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(); 
        } 
    }
}
相关问题