I'm new with XAML/WPF and I don't know how to deal with this issue, I spend too much time into this, but I can't figure it out.

I have a class called Card with a property which "should" contain a source of an image.

public class Card
{
     public ImageSource image; // this is the only one which doesn't get a value..
}

I also have a class with a method which add information to the Collection of card like this:

public class deck
{
     public card[] = new card[51];
     public void FillCards()
     {
         while(I > something) //don't mind about the loop, because it's working fine
         {
             card[i] = new card()
             {   
                 name = this.name, //works
                 number = this.number, //works
                 //works etc...
                 image = new BitmapImage(new Uri(path, UriKind.Absolute)), //except this is not working
                 //Before you ask, yes the path is correct...
             };
         }   
     }
}

So in short, my question is:

Why can't I store BitmapImage in the ImageSource property,

What I'm basically doing is this: How can I have a property in a class to store an picture/image?

But I'm getting NullReferenceException.

Sorry for the bad English...

有帮助吗?

解决方案

There are a lot of ways to store a picture. If the property will be binding to an Image Control. Then you can use: byte[], BitmapImage, string(Uri) to store a picture. This is because those types can automatic converter a BitmapImage to ImageSource.

In this case, you just need to modify the ImageSource To BitmapImage.

By the way, The reason why you can't use ImageSource is the Constructor of ImageSource is internal, so you can't even create an ImageSource object.

So, that's all, try to use some other type.

public class Card
{
     // Try some other type, they all can be bind to Image.Source.
     public BitmapImage image; 
}

An related question.

Hope it helpful.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top