2010-11-15 6 views
0

J'utilise JSON.NET sur la désérialisation dans la classe CLR. Tout est ok avec deserialize JSON dans la classe, dans le constructeur de classe j'essaye de faire un BitmapImage et mettre à la propriété de la classe Photo. Le problème est les propriétés BitmapImage Photo est toujours nulle, Uri est bon.Problème avec create BitmaImage Dans le constructeur de l'objet JSON

J'essaie d'utiliser cet URI sans classe constructort pour créer un objet BitmapImage et cela fonctionne.

Où peut-être un problème?

code est ici:

[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 null 
      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; 

Je pense que problème est ici. Si j'ai défini le point d'arrêt dans la ligne:

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

Propriétés ProfilePhoto est null, je pense que le problème doit être dans cette méthode.

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: Le problème était, cet appel JSON.NET ce constructeur:

public JsonUser() 
    { 
    } 

J'initialiser ProfilePhoto de propriété sur un autre lieu, en setter dans la propriété Photon

Répondre

0

Have vous l'avez essayé comme ceci:

Photo = new BitmapImage(); 
Photo = img; 

Si cela ne fonctionne pas, je suggère de remplacer img avec Photo.

+0

cela ne fonctionne pas –

0

Placez un point d'arrêt juste avant de définir la photo sur img. Vérifiez que img n'est pas nul. Ce problème n'a peut-être rien à voir avec la sérialisation autant que l'initialisation img