Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从存储在独立存储中的列表中编辑元素?_C#_Silverlight_Windows Phone 7_Isolatedstorage - Fatal编程技术网

C# 如何从存储在独立存储中的列表中编辑元素?

C# 如何从存储在独立存储中的列表中编辑元素?,c#,silverlight,windows-phone-7,isolatedstorage,C#,Silverlight,Windows Phone 7,Isolatedstorage,我有一个OberservableCollection存储在我的独立存储中 新场景包含一个列表。灯光包含Xpos en Ypos属性 如何编辑此列表 所以我想从IsoStorage中检索它,更改2个属性,然后再次保存它 希望有人能帮助我 亲切问候,, NielsObservableCollection有一个Collecton changed事件,该事件传递给向其发送基本集合的人以及一系列参数。您可以侦听此CollectionChanged事件,只侦听新添加的项,并对该项执行操作 如果集合是引用对象

我有一个OberservableCollection存储在我的独立存储中

新场景包含一个列表。灯光包含Xpos en Ypos属性

如何编辑此列表

所以我想从IsoStorage中检索它,更改2个属性,然后再次保存它

希望有人能帮助我

亲切问候,, Niels

ObservableCollection有一个Collecton changed事件,该事件传递给向其发送基本集合的人以及一系列参数。您可以侦听此CollectionChanged事件,只侦听新添加的项,并对该项执行操作

如果集合是引用对象的列表,则可以直接对对象执行编辑,更改将镜像到列表中。但是,如果您使用的是值类型的列表,那么您需要一种标记该项的方法,这样当您将其从列表中删除并重新添加时,添加不会触发无限循环

例如:

这个Clean类用于存储一个值以及一个标志标记(如果它是否已被处理)

class Program
{
    static void Main(string[] args)
    {
        ObservableCollection<Clean<int>> myCollection = new    ObservableCollection<Clean<int>>();
        myCollection.CollectionChanged += x_CollectionChanged;

        myCollection.Add(new Clean<int>(2,false));

    }

    static void x_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //Grab the collection being modified
        ObservableCollection<Clean<int>> collection = sender as ObservableCollection<Clean<int>>;
        if (collection == null)
        {
            // do some error checking action
        }
        //Only look at items being added
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            //New item has been added
            foreach (Clean<int> newItem in e.NewItems)
            {
                ///Only perform the operation on new "dirty" objects
                if (!newItem.IsClean)
                {
                    collection.Remove(newItem);
                    //Add the new modified value and mark it as clean so that 
                    //    this process isn't run again
                    collection.Add(new Clean<int>(newItem.Value * 2,true));
                }
            }
        }

    }
}
资料来源:

可观测收集


NotifyCollectionChangedEventHandler

我将按照正常方式检索集合

public IEnumerable<Scene> GetScenes()
{
    using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var fs = new IsolatedStorageFileStream("Scenes", FileMode.Open, filesystem))
        {
            var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(IEnumerable<Scene>));
            return serializer.ReadObject(fs) as IEnumerable<Scene>;
        }
    }
}
如果使用FileMode.Create,它将覆盖上一个集合

你可以找到一份工作

public IEnumerable<Scene> GetScenes()
{
    using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var fs = new IsolatedStorageFileStream("Scenes", FileMode.Open, filesystem))
        {
            var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(IEnumerable<Scene>));
            return serializer.ReadObject(fs) as IEnumerable<Scene>;
        }
    }
}
var myScenes = new ObservableCollection<Scene>(GetScenes());
var itemToUpdate = myScenes.Where(i => i.PropertyToCheck == "value to check");
itemToUpdate.PropertyToSet = "new value";
SaveScenes(myScenes);
public void SaveScenes(IEnumerable<Scene> scenes)
{
    using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var fs = new IsolatedStorageFileStream("Scenes", FileMode.Create, filesystem))
        {
            var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(IEnumerable<Scene>));
            serializer.WriteObject(fs, Scenes);
        }
    }
}