0

我使用 JSON.NET 反序列化为 CLR 类。将 JSON 反序列化到类中一切正常,在类构造函数中我尝试制作一个 BitmapImage 并设置为类属性 Photo。问题是属性 BitmapImage Photo 仍然为空,Uri 很好。

我尝试在没有类构造函数的情况下使用这个 uri 来创建 BitmapImage 对象并且它可以工作。

哪里可能有问题?

代码在这里:

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

我认为问题就在这里。如果我在该行中设置断点:

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

属性 ProfilePhoto 为空,我认为问题一定出在这个方法上。

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

}

解决方案:问题是,JSON.NET 调用此构造函数:

    public JsonUser()
    {
    }

我在其他地方初始化属性 ProfilePhoto,在属性 Photon 的设置器中

4

2 回答 2

0

你有没有这样尝试过:

Photo = new BitmapImage(); 
Photo = img;

如果它不起作用,我建议用 Photo 替换 img。

于 2010-11-15T10:07:39.307 回答
0

在将 Photo 设置为 img 之前放置一个断点。检查 img 是否不为空。这个问题可能与序列化没有任何关系,而与 img 初始化无关

于 2010-11-15T10:32:36.163 回答