2016-05-06 77 views
0

裝入一個新的DataContext我有一個類作爲我的datacontext,它會在以後擴展:從反序列化JSON

public class CMiX_Data : INotifyPropertyChanged 
{ 
    public class VidFlip : INotifyPropertyChanged 
    { 
     private bool _VidFlipX; 
     public bool VidFlipX 
     { 
      get { return _VidFlipX; } 
      set{ if (value != _VidFlipX) 
       { 
        { _VidFlipX = value; OnPropertyChanged("IsEnabled"); } 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      MessageBox.Show("property"+propertyName+"changed"); 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private ObservableCollection<VidFlip> _Collection = new ObservableCollection<VidFlip>(new[] { new VidFlip { VidFlipX = true }, new VidFlip { VidFlipX = false }, new VidFlip { VidFlipX = false }, new VidFlip { VidFlipX = false }, new VidFlip { VidFlipX = false }, new VidFlip { VidFlipX = false } } ); 
    public ObservableCollection<VidFlip> Collection 
    { 
     get { return _Collection; } 
     set { _Collection = value; OnPropertyChanged("Collection"); } 
    } 
} 

我保存和加載JSON文件是這樣的:

public partial class CMiX_UI : UserControl, INotifyPropertyChanged, ISupportsUndo 
{ 
    private event EventHandler OnSelect; 
    public event PropertyChangedEventHandler PropertyChanged; 
    CMiX_UserControl.Properties.Settings(); 

    CMiX_Data data = new CMiX_Data(); 

    public CMiX_UI() 
    { 
     InitializeComponent(); 
     this.DataContext = new CMiX_Data(); 
    } 

    private void MenuSave_Click(object sender, RoutedEventArgs e) 
    { 
     JsonSerializer serializer = new JsonSerializer(); 
     serializer.Converters.Add(new JavaScriptDateTimeConverter()); 
     serializer.NullValueHandling = NullValueHandling.Ignore; 

     using(StreamWriter sw = new StreamWriter(@"D:\Google Drive\PROJECT\_C-MiX\_C-MiX-2.0\pouet.txt")) 
     using(JsonWriter writer = new JsonTextWriter(sw)) 
     { 
       serializer.Serialize(writer, data); 
     } 
     MessageBox.Show("saved"); 
    } 

    private void MenuLoad_Click(object sender, RoutedEventArgs e) 
    { 
     var serializer = new JsonSerializer(); 
     using (var re = File.OpenText(@"D:\Google Drive\PROJECT\_C-MiX\_C-MiX-2.0\pouet.txt")) 
     using (var reader = new JsonTextReader(re)) 
     { 
      CMiX_Data entries = serializer.Deserialize<CMiX_Data>(reader); 
      DataContext = entries; 
      MessageBox.Show("Loaded"); 
     } 
    } 

而JSON中保存的數據(這裏的ObservableCollection)是這樣的:

{"Collection":[{"VidFlipX":true},{"VidFlipX":false},{"VidFlipX":false},{"VidFlipX":false},{"VidFlipX":false},{"VidFlipX":false}]} 

什麼保存就是我上了UI。但是,當加載JSON文件,在DataContext或總的ObservableCollection返回到原來的狀態(在一個上面顯示),而不是由所加載被修改......

任何想法?謝謝

回答

0

正在創建CMiX_Data類的兩個單獨的實例,一個初始化過程中您的公共CMiX_UI類「CMiX_Data數據=新CMiX_Data();」和一個結構「this.DataContext = new CMiX_Data();」。我認爲在結構中你應該使用以前創建的對象,如下所示:this.DataContext = this.data ;.