开发者

Problem with create BitmaImage In constructor of JSON object

I use JSON.NET on deserialization into CLR class. Everything is ok with deserialize JSON into class, in class constructor I try make a BitmapImage and set to the class property Photo. Problem is properties BitmapImage Photo is still null, Uri is good.

I try use this uri without class constructorto create BitmapImage object and it works.

Where can be problem?

Code is here:

    [JsonObject]
    public class JsonUser
    {
        [JsonProperty("idUser")]
        public string IdUser { get; set; }
        [JsonProperty("nick")]
        public string Nick { get; set; }
        [JsonProperty("sefNick")]
        public string SefNick { get; set; }
        [JsonProperty("sex")]
        public string Sex { get; set; }
        [JsonProperty("photon")]
        public string Photon { get; set; }
        [JsonProperty("photos")]
        public string Photos { get; set; }
        [JsonProperty("logged")]
        public bool Logged { get; set; }
        [JsonProperty("idChat")]
        public int IdChat { get; set; }
        [JsonProperty("roomName")]
        public string RoomName { get; set; }
        [JsonProperty("updated")]
        public string Updated { get; set; }

        public BitmapImage Photo { get; set; }

        public JsonUser()
        {
        }

        public JsonUser(string idUser, string nick, string sefNick, string sex, string photon, 
            string photos, bool logged, int idChat, string roomName, string updated )
        {
            IdUser = idUser;
            Nick = nick;
            SefNick = sefNick;
            Sex = sex;
            Photon = photon;
            Photos = photos;
            Logged = logged;
            IdChat = idChat;
            RoomName = roomName;
            Updated = updated;

            var img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(photon, UriKind.Absolute);
            img.EndInit();

            //it is still n开发者_如何学运维ull
            Photo = img;
        }
    }


I use this object of type JsonUser in other class

public class MyClass
{
public JsonUser user;


public JsonUser CreateUser()
{
   //this method parse JSON string and return object type of JsonUser
}


//in this method I create instance on user
public void SomeMethod()
{
 user=CreateUser();
}

}



//and in other part of code i try this

var obj = new MyClass();
obj.SomeMethod();

//nad here is Photo null
obj.user.ProfilePhoto;

I Think problem is here. If I set breakpoint in the line:

var users = JsonConvert.DeserializeObject>>(htmlStringResult.Replace(@"\",""));

Properties ProfilePhoto is null, I think problem must be in this method.

public JsonUser CreateJsonUser(string nick)
{
    const string parameter = @"&nickOponent=";

try
{
    string htmlStringResult = HttpGetReq(new Uri(string.Format
                                     (CultureInfo.InvariantCulture, "{0}{1}{2}{3}",
                                      PokecUrl.DoplnData, Account.SessionId, parameter, nick)));

    var users = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, JsonUser>>>(htmlStringResult.Replace(@"\",""));
    return users["newNickInfo"].First().Value;

}
catch (Exception exception)
{

    throw exception;
}

}

SOLUTION: Problem was, that JSON.NET call this constructor:

    public JsonUser()
    {
    }

I initialize property ProfilePhoto on other place, in setter in property Photon


Have you tried it like this:

Photo = new BitmapImage(); 
Photo = img;

If it doesn't work, I would suggest replacing img with Photo.


Place a breakpoint just before you set Photo to img. Check that img is not null. This issue might not have anything to do with the serialization so much as the img initialization

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜