saulgreenberg / TimelapseDeprecatedPre2.3

Timelapse Image Analysis Tool
Other
1 stars 0 forks source link

AvalonDock test to save window placement settings #62

Closed saulgreenberg closed 6 years ago

saulgreenberg commented 6 years ago

While AvalonDock supposedly lets one save and restore settings, I can't get it to work. Pursue this.

saulgreenberg commented 6 years ago

Finally figured it out. See TestPrograms/AvalonLoadSave for an example solution.

Generally, the solution is simple although documentation is incredibly poor on this. Avalon panes (e.g., LayoutDocument, LayoutAnchorable) must have a unique ContentId specified in order for them to be saved/restored, e.g.,

using System.IO; using System.Windows; using System.Windows.Controls; using Xceed.Wpf.AvalonDock.Layout.Serialization; namespace AvalonLoadSave { public partial class MainWindow : Window { private string LastUsedLayoutName = "LastUsedLayout"; public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { if (File.Exists (CreateConfigFileName(LastUsedLayoutName))) { LoadLayout(LastUsedLayoutName); } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { SaveLayout("LastUsedLayout"); } private void OnSaveLayout(object sender, RoutedEventArgs e) { SaveLayout((sender as MenuItem).Header.ToString()); } private void SaveLayout(string fileName) { XmlLayoutSerializer serializer = new XmlLayoutSerializer(dockManager); using (StreamWriter stream = new StreamWriter(CreateConfigFileName(fileName))) serializer.Serialize(stream); } private void OnLoadLayout(object sender, RoutedEventArgs e) { // var currentContentsList = dockManager.Layout.Descendents().OfType().Where(c => c.ContentId != null).ToArray(); LoadLayout((sender as MenuItem).Header.ToString()); } private void LoadLayout(string fileName) { XmlLayoutSerializer serializer = new XmlLayoutSerializer(dockManager); try { // Note. If there is a bug in the config file, this won't deserialize properly. Unfortunately, no // warning is produced. This should not happen unless for some reason we have changed the names of the ContentID and / or windows // included in the system. using (StreamReader stream = new StreamReader(CreateConfigFileName(fileName))) serializer.Deserialize(stream); } catch { System.Diagnostics.Debug.Print("Could not deserialize"); } } private string CreateConfigFileName (string fileName) { return string.Format(@".\AvalonDock_{0}.config", fileName); } } }