C# 使用不同的键声明IsolatedStorage对象

C# 使用不同的键声明IsolatedStorage对象,c#,windows-phone-8,C#,Windows Phone 8,我正在windows phone 8中为我的应用程序创建配置文件页面 我需要为每个配置文件存储数据 所以我使用的是IsolatedStorage IsolatedStorageSettings Profile = IsolatedStorageSettings.ApplicationSettings; 当然还有一个列表,用于存储它们 List<IsolatedStorageSettings> Profiles = new List<IsolatedStorageSetting

我正在windows phone 8中为我的应用程序创建配置文件页面 我需要为每个配置文件存储数据 所以我使用的是
IsolatedStorage

IsolatedStorageSettings Profile = IsolatedStorageSettings.ApplicationSettings;
当然还有一个
列表
,用于存储它们

List<IsolatedStorageSettings> Profiles = new List<IsolatedStorageSettings>();
因此,如果用户想要添加配置文件,我需要执行
profile.add(“profile(CurrentIndex)”,player)

就像:

“player1”“player2”“player3”。“游乐场”


如何编写一个代码来完成所有这些工作?

您不必使用
列表
,因为
隔离存储设置
提供了一个
字典
,它将键值对存储在隔离存储中

这是我的代码,我尝试了一下,效果很好

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

您不必使用
列表
,因为
隔离存储设置
提供了一个
字典
,它将键值对存储在隔离存储中

这是我的代码,我尝试了一下,效果很好

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

您不必使用
列表
,因为
隔离存储设置
提供了一个
字典
,它将键值对存储在隔离存储中

这是我的代码,我尝试了一下,效果很好

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

您不必使用
列表
,因为
隔离存储设置
提供了一个
字典
,它将键值对存储在隔离存储中

这是我的代码,我尝试了一下,效果很好

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

这可能对你有帮助。给出一个如何实现你所尝试的目标的想法

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
    }
公共类配置文件
{
公共字符串数据键{get;set;}
公共配置文件数据{get;set;}
}
公共类概要数据
{
公共字符串名称{get;set;}
//定义所有配置文件属性
...
...
}
//现在编码
IsolatedStorageSettings ProfileSettings=IsolatedStorageSettings.ApplicationSettings;
List ProfileList=新列表();
//在ProfileList中添加数据,而不是在IsolatedStorageSettings中添加数据
Profile profileData=新的Profile();
ProfileData=新的ProfileData();
//添加属性
data.Name=“PlayerOne”;
.....
...
profileData.Datakey=“playernekey”;
profileData.Data=Data;
ProfileList.Add(profileData);
ProfileSettings.Add(“AllProfile”,ProfileList);
ProfileSettings.Save();
//如果要在IsolatedStorage设置中保存另一个配置文件
if(ProfileSettings.Contains(“AllProfile”))
{
将ProfileList=ProfileSettings[“AllProfile”]列为列表();
//在配置文件列表中添加新项
}

这可能会对您有所帮助。给出一个如何实现你所尝试的目标的想法

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
    }
公共类配置文件
{
公共字符串数据键{get;set;}
公共配置文件数据{get;set;}
}
公共类概要数据
{
公共字符串名称{get;set;}
//定义所有配置文件属性
...
...
}
//现在编码
IsolatedStorageSettings ProfileSettings=IsolatedStorageSettings.ApplicationSettings;
List ProfileList=新列表();
//在ProfileList中添加数据,而不是在IsolatedStorageSettings中添加数据
Profile profileData=新的Profile();
ProfileData=新的ProfileData();
//添加属性
data.Name=“PlayerOne”;
.....
...
profileData.Datakey=“playernekey”;
profileData.Data=Data;
ProfileList.Add(profileData);
ProfileSettings.Add(“AllProfile”,ProfileList);
ProfileSettings.Save();
//如果要在IsolatedStorage设置中保存另一个配置文件
if(ProfileSettings.Contains(“AllProfile”))
{
将ProfileList=ProfileSettings[“AllProfile”]列为列表();
//在配置文件列表中添加新项
}

这可能会对您有所帮助。给出一个如何实现你所尝试的目标的想法

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
    }
公共类配置文件
{
公共字符串数据键{get;set;}
公共配置文件数据{get;set;}
}
公共类概要数据
{
公共字符串名称{get;set;}
//定义所有配置文件属性
...
...
}
//现在编码
IsolatedStorageSettings ProfileSettings=IsolatedStorageSettings.ApplicationSettings;
List ProfileList=新列表();
//在ProfileList中添加数据,而不是在IsolatedStorageSettings中添加数据
Profile profileData=新的Profile();
ProfileData=新的ProfileData();
//添加属性
data.Name=“PlayerOne”;
.....
...
profileData.Datakey=“playernekey”;
profileData.Data=Data;
ProfileList.Add(profileData);
ProfileSettings.Add(“AllProfile”,ProfileList);
ProfileSettings.Save();
//如果要在IsolatedStorage设置中保存另一个配置文件
if(ProfileSettings.Contains(“AllProfile”))
{
将ProfileList=ProfileSettings[“AllProfile”]列为列表();
//在配置文件列表中添加新项
}

这可能会对您有所帮助。给出一个如何实现你所尝试的目标的想法

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
    }
公共类配置文件
{
公共字符串数据键{get;set;}
公共配置文件数据{get;set;}
}
公共类概要数据
{
公共字符串名称{get;set;}
//定义所有配置文件属性
...
...
}
//现在编码
IsolatedStorageSettings ProfileSettings=IsolatedStorageSettings.ApplicationSettings;
List ProfileList=新列表();
//在ProfileList中添加数据,而不是在IsolatedStorageSettings中添加数据
Profile profileData=新的Profile();
ProfileData=新的ProfileData();
//添加属性
data.Name=“PlayerOne”;
.....
...
profileData.Datakey=“playernekey”;
profileData.Data=Data;
ProfileList.Add(profileData);
ProfileSettings.Add(“AllProfile”,ProfileList);
ProfileSettings.Save();
//如果要在IsolatedStorage设置中保存另一个配置文件
if(ProfileSettings.Contains(“AllProfile”))
{
将ProfileList=ProfileSettings[“AllProfile”]列为列表();
//在配置文件列表中添加新项
}

还有一件事,为什么每次都要用“1”初始化索引?还有一件事,为什么每次都要用“1”初始化索引?还有一件事,为什么每次都要用“1”初始化索引?还有一件事,为什么每次都要用“1”初始化索引?