Problem with Restore Layout

May 12, 2009 at 10:02 AM
Edited May 12, 2009 at 10:03 AM

I have the following code in my main window class

namespace SampleDockLayout
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        StatusBarItem DateTimeItem;
        DataSet LogDataSet;
        public Window1()
        {
            InitializeComponent();
            BuildTree();
            InitStatusBar();
            InitEventLogGrid();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            SaveLayout();
            base.OnClosing(e);
        }

        void LoadLayout()
        {
            string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                + @"\layout.xml";
            if (!File.Exists(path))
                return;
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            try{
                dockMngr.RestoreLayout(fs);
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
            fs.Close();
        }
        void SaveLayout()
        {
            string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                + @"\layout.xml";
            dockMngr.SaveLayout(path);
        }

If I try to call LoadLayout after  InitializeComponent() I catches an exception. If I try to do this in OnInitialized event handlers such as following

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            LoadLayout();
        }

I also catches an exception. What I do wrong? I want to Restore my layout each time when my application starts. Object dockMngr is an object of class Window1.

May 12, 2009 at 10:33 AM
Edited May 12, 2009 at 10:34 AM

What event I can override to Restore my Layout on startup?

May 12, 2009 at 12:44 PM

And when I tried to start timer for 1 minute and after that I restored layout I see that the controls in my layout isn't restored automatically. I think that it means that I have to write some code for serialize my controls into xml-file, isn't  it? If anybody know how to do this or have some ideas how to save the layout when application closes and restore this layout  when it starts give me please some exmaple code.

May 12, 2009 at 2:44 PM

I can't provide a code sample but maybe some explaination from my own experience.

First thing is to check the layout.xml. The save may not be correct, and then it is obvious that the reload will crash. All content must have a name.

To reload correctly the layout, windows must be created before to call RealoadLayout.

Example : You have 3 windows win1, win2, win3 and you saved. At the next run, just create win1, win2, win3 without carring about the layout. and then call reload.

May 13, 2009 at 7:10 AM

May be problem is that I use unnamed cotrols. But exception message is that RestoreLayout can't be done while dockManager unload. But InitializeComponent(); is already completed and other initial functions are already completed. If I start timer one 1 minute and RestoreLayout it completes sucessful but Layout is not like on Layout which I saved

May 13, 2009 at 11:53 AM

I named all my DockableContent elements and set delay 10 seconds after InitializeComponent() completed. After that I called RestoreLayout. It was done successful. By I want RestoreLayout immediately when my application starts. How can I do this. In what place I have to place call of RestoreLayout() or what event  must happened for call RestoreLayout successful? Now I try to call:

        void LoadLayout()
        {
            string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                + @"\layout.xml";
            if (!File.Exists(path))
            {
                Debug.Assert(false, "Отсутствует xml-файл, содержащий layout");
                return;
            }
            try{
                if (dockMngr != null)
                    dockMngr.RestoreLayout(path);
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            LoadLayout();
        }

And I catch an exception message: "Unable to deserialize a docking layout while DockingManager control is unloaded" but the object of DockingManager isn't null

May 13, 2009 at 12:11 PM

The question is canceled. DockingManager has the event Loaded and RestoreLayout must be called on this event such as:

        private void dockMngr_Loaded(object sender, RoutedEventArgs e)
        {
            LoadLayout();
        }

thoqbk thanks for your answer. You are really help me.

May 13, 2009 at 12:29 PM

I have the next problem. I have the next property

<ad:DockablePane MinWidth="150">

 

And it works correct while I run my application first time. When I shutdown my application I save layout into xml file. After that I try to restore layout from file and this property doesn't work! I tried to set the name for the DockablePane but the result is the same. And also I tried to set this property manually such as

<ad:DockablePane MinWidth="150" Name="_navigationTreePane">

        private void dockMngr_Loaded(object sender, RoutedEventArgs e)
        {
            LoadLayout();
            _navigationTreePane.MinWidth = 150;
        }

But there is no effect. What's wrong?

May 14, 2009 at 7:09 AM

For your question before, you can call ReloadLayout the Loaded event of the window holding the dockingManager.

For the last one,I had a similar problem of size.

I suppose your DockablePane is in a ResizingPanel ? In this case, i suggest you to use "ad:ResizingPanel.ResizeWidth" :

<ad:ResizingPanel Orientation="Horizontal">
     <ad:DockablePane ad:ResizingPanel.ResizeWidth="200">
     ...
     </ad:DockablePane>
...
</ad:ResizingPanel>

May 14, 2009 at 8:33 AM

Yes, of course. Thank you very much. I saw this property in layout.xml file which made by SaveLayout() call. But I couldn't understand which element has this property.