我正在为Windows Phone 8中的应用创建个人资料页面
我需要store data
每个配置文件
所以我正在使用IsolatedStorage
IsolatedStorageSettings Profile = IsolatedStorageSettings.ApplicationSettings;
和Ofcourse List
用于存储所有
List<IsolatedStorageSettings> Profiles = new List<IsolatedStorageSettings>();
现在问题是当我想添加个人资料到列表时 每个配置文件必须具有不同的密钥
Profile.Add("profile1", player); // "profile1" is the key of the first profile
Profile.Save();
Profiles.Add(Profile);
所以,如果用户想要添加我需要做的个人资料 Profile.Add("profile(CurrentIndex)",player);
就像:
“player1”“player2”“player3”。 。 “playerN”
我如何编写完成所有这些功能的代码?
答案 0 :(得分:2)
您不必使用List<IsolatedStorageSettings>
,因为IsolatedStorageSettings
提供了Dictionary<TKey, TValue>
,可以将密钥值对存储在隔离存储中。
这是我的代码,我试一试,效果很好。
public void SavePlayer()
{
IsolatedStorageSettings profile = IsolatedStorageSettings.ApplicationSettings;
string key = string.Format("player{0}", GetCurrentIndex());
profile.Add(key, player);
profile.Save();
}
public object GetPlayer(string Key)
{
object obj = null;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains(Key))
{
obj = settings[Key];
}
return obj;
}
public int GetCurrentIndex()
{
int index = 1;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("CurrentIndex"))
{
index = (int)settings["CurrentIndex"];
index++;
settings["CurrentIndex"] = index;
}
else
{
settings.Add("CurrentIndex", (int)1);
}
settings.Save();
return index;
}
答案 1 :(得分:1)
这可能会对你有所帮助。想一想如何实现你所尝试的。
public class Profile
{
public string Datakey{get; set;}
public ProfileData Data{get; set;}
}
public class ProfileData
{
public string Name{get;set;}
//define all your profile properties
...
...
}
//Now in code
IsolatedStorageSettings ProfileSettings = IsolatedStorageSettings.ApplicationSettings;
List<Profile> ProfileList = new List<Profile>();
//Add data in ProfileList instead of IsolatedStorageSettings
Profile profileData = new Profile();
ProfileData data = new ProfileData ();
//Add properties
data.Name ="PlayerOne";
.....
...
profileData.Datakey="PlayerOneKey";
profileData.Data = data;
ProfileList.Add(profileData);
ProfileSettings.Add("AllProfile",ProfileList);
ProfileSettings.Save();
//If you want save another profile in IsolatedStorageSettings
if(ProfileSettings.Contains("AllProfile"))
{
List<Profile> ProfileList = ProfileSettings["AllProfile"] as List<Profile> ();
// Add new item in profileList
}