2016-05-06 6 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_UIクラスでは、CMiX_Dataクラスの2つのインスタンスを作成しています。初期化中に1つ、「CMiX_Data data = new CMiX_Data();」です。そして1つは "this.DataContext = new CMiX_Data();"である。私は、あなたは、以前に作成されたオブジェクトを次のように使用するべきであると考えています:this.DataContext = this.data ;.

関連する問題