简体   繁体   中英

serialising an image using JsonConvert

I have a viewmodel which have an image. When i serialize the view model it doesn't convert the object into anything. So when i deserialize I couldn't get the correct view model.

viewmodel:

public class ViewModel
{
    public ViewModel(string imageUrl)
    {
        if (!string.IsNullOrEmpty(imageUrl))
        {
            IsImageLoading = true;
            _img = new BitmapImage();
            _img.ImageOpened += (s, e) => { IsImageLoading = false; };
            _img.DownloadProgress += (s, e) => { DownloadProgress = e.Progress; };
            _img.ImageFailed += (s, e) =>
                {
                    IsImageLoading = false;
                    _img = new BitmapImage(new Uri("/Assets/Images/fwakes.png", UriKind.Relative));
                    NotifyPropertyChanged("Image");
                };
            _img.UriSource = new Uri(imageUrl, UriKind.RelativeOrAbsolute);
        }
    }
    .....

    BitmapImage _img;

    public BitmapImage Image 
    { 
        get
        {
            return _img;
        }
    }
}

Serialising the objects into storage:

public bool SaveItems(IEnumerable<ItemViewModel> source)
    {
        string jsonContents = JsonConvert.SerializeObject(source, Formatting.Indented);

So how can i serialize correctly so that data binding for image doesn't trouble.

The aim of my project is that when I have internet I would download all the viewmodel with images and when there isn't internet I would deserialize the viewmodel to get images and bind them. What is the effective way to do it?

You don't serialize the image, you save it in the isolated storage and then you serialize the path to the image.

See http://www.geekchamp.com/tips/all-about-wp7-isolated-storage---read-and-save-images for saving/loading images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM