Reputation: 13306
I Want to inherit this class
[DataContract]
public class Item
{
string _name;
string _path;
long _size;
byte[] _image;
public Item(string Name, string path, long Size, byte[] image)
{
_name = Name;
_path = path;
_size = Size;
_image = image;
}
[DataMember]
public string Name
{
get { return _name; }
set { }
}
[DataMember]
public string Path
{
get { return _path; }
set { }
}
[DataMember]
public long Size
{
get { return _size; }
set { }
}
[DataMember]
public string SizeToString
{
get
{
if (_size == 0) return "Folder";
return CnvrtUnit(_size);
}
set { }
}
}
I tried to inherit it:
public class ItemEx : Item
{
public ImageSource Icon
{
get
{
BitmapImage bi = new BitmapImage();
using (MemoryStream ms = new MemoryStream(_image))
{
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = ms;
bi.EndInit();
}
return bi;
}
set { }
}
}
then when I assign an instance of ItemEx
to instance of 'Item', it doesn't work
public void Assign(Item item)
{
ItemEx x = item as ItemEx; //its still null
}
Why is this happening? and what should I do to get it work?
P.S.: I need to inherit this class, because it came from WCF service which doesn't pass ImageSource
, so i passed it as byte[]
then item has to contain an ImageSource named "Icon" for WPF MVVM purpose.
Upvotes: 0
Views: 116
Reputation: 13306
found a perfect solution
public class ItemEx : Item
{
public byte[] IconData
{
get { return icon; }
set
{
icon = value;
Icon = null;
}
}
public ImageSource Icon
{
get { return ToImageSource(IconData); }
set { RaisePropertyChanged("Icon"); }
}
}
public static ImageSource ToImageSource(byte[] icon)
{
if (icon != null)
{
BitmapImage biImg = new BitmapImage();
MemoryStream ms = new MemoryStream(icon);
biImg.BeginInit();
biImg.StreamSource = ms;
biImg.EndInit();
return biImg;
}
return null;
}
Upvotes: 0
Reputation: 1010
What is the underlying type of item in the line:
public void Assign(object item)
e.g. what do you see if you run MessageBox.Show(item.GetType()).
If x is null after
ItemEx x = item as ItemEx; //its still null
then item isn't of type ItemEx. I assume you're sending an ItemEx from the Client; otherwise it won't work. Also note that _image is private in Item so you won't be able to access it in ItemEx unless you modify the base class.
Do you know why the other three properties of the 'Item' class are exposed via properties whereas 'image' is not. This seems to be the problem.
Upvotes: 1
Reputation: 3967
I think you can pass byte[] _image to ImageSource like below instead of try to override that class:
private static BitmapImage LoadImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
Image img = new Image();
img.Source = LoadImage(Item._image); // for example
Upvotes: 0