如何继承一个类?

时间:2013-12-21 01:24:57

标签: c# wpf

我想继承这门课程

[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 { }
    }
}

我试图继承它:

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 { }
    } 
}

然后当我将ItemEx的实例分配给'Item'的实例时,它不起作用

public void Assign(Item item)
{
    ItemEx x = item as ItemEx; //its still null
}

为什么会这样?我该怎么做才能让它发挥作用?

PS: 我需要继承这个类,因为它来自WCF服务,它没有通过ImageSource,所以我把它传递给了byte[]然后item必须包含一个名为“Icon”的ImageSource,用于WPF MVVM目的。

3 个答案:

答案 0 :(得分:1)

行中的基础类型是什么:

public void Assign(object item)

e.g。如果你运行MessageBox.Show(item.GetType()),你会看到什么。

如果 x

之后为空
ItemEx x = item as ItemEx; //its still null

然后 item 不是ItemEx类型。我假设您从客户端发送了一个ItemEx;否则它将无法工作。另请注意,_image在Item中是私有的,因此除非您修改基类,否则您将无法在ItemEx中访问它。

你知道为什么'Item'类的其他三个属性是通过属性公开的,而' image '则不是。这似乎是个问题。

答案 1 :(得分:0)

我认为您可以将byte [] _image传递给ImageSource,而不是尝试覆盖该类:

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

答案 2 :(得分:0)

找到了一个完美的解决方案

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;
    }
相关问题