【问题标题】:How to edit a element from a list that is stored in Isolated Storage?如何编辑存储在独立存储中的列表中的元素?
【发布时间】:2013-12-20 22:41:17
【问题描述】:

我的独立存储中存储了一个 OberservableCollection。

新建一个场景包含一个列表。并且 Light 包含 Xpo​​s en Ypos 属性。

如何编辑此列表?

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

希望有人可以帮助我:)。

亲切的问候, 尼尔斯

【问题讨论】:

    标签: c# silverlight windows-phone-7 isolatedstorage


    【解决方案1】:

    ObservableCollection 有一个 colleciton changed 事件,它传递了谁发送它(基本集合)以及一系列参数。你可以监听这个CollectionChanged事件,只监听新添加的item并对item执行你的操作。

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

    示例:

    class Clean<T>
    {
        public T Value;
        public bool IsClean;
        public Clean(T value, bool clean)
        {
            Value = value;
            IsClean = clean;
        }
    }
    

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

    来源:

    ObservableCollectionMSDN Article

    NotifyCollectionChangedEventHandlerMSDN Article

    【讨论】:

      【解决方案2】:

      我会像往常一样检索集合...

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

      如果你使用FileMode.Create,它将覆盖之前的集合。

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

      您可以找到similar example on MSDN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多