示例#1
0
        internal void OnLayoutLoaded_Event(object sender, LayoutLoadedEventArgs layoutLoadedEvent)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                try
                {
                    // Process the finally block since we have nothing to do here
                    if (layoutLoadedEvent == null)
                    {
                        return;
                    }

                    // Process the finally block since we have nothing to do here
                    var result = layoutLoadedEvent.Result;
                    if (result == null)
                    {
                        return;
                    }

                    if (result.LoadwasSuccesful == true)
                    {
                        XmlLayoutSerializer stringLayoutSerializer;

                        // Make sure AvalonDock control is visible at the end of restoring layout
                        stringLayoutSerializer = new XmlLayoutSerializer(dockManager);

                        //Here I've implemented the LayoutSerializationCallback just to show
                        // a way to feed layout desarialization with content loaded at runtime
                        //Actually I could in this case let AvalonDock to attach the contents
                        //from current layout using the content ids
                        //LayoutSerializationCallback should anyway be handled to attach contents
                        //not currently loaded

                        stringLayoutSerializer.LayoutSerializationCallback += (s, e) =>
                        {
                            try
                            {
                                var workSpace = (DataContext as AppViewModel).AD_WorkSpace;

                                if (workSpace == null || string.IsNullOrEmpty(e.Model.ContentId))
                                {
                                    e.Cancel = true;
                                    return;
                                }

                                // Is this a tool window layout ? Then, get its viewmodel and connect it to the view
                                var tool = workSpace.Tools.FirstOrDefault(i => i.ContentId == e.Model.ContentId);
                                if (tool != null)
                                {
                                    e.Content = tool;
                                    return;
                                }

                                // Its not a tool window -> So, this could rever to a document then
                                if (!string.IsNullOrWhiteSpace(e.Model.ContentId))
                                {
                                    DocumentViewModel vm = new DocumentViewModel(workSpace, e.Model.ContentId);

                                    if (vm != null)
                                    {
                                        workSpace.AddFile(vm);
                                        e.Content = vm;
                                        e.Cancel  = false;
                                        return;
                                    }

                                    e.Cancel = true;
                                    return;
                                }

                                // Not something we could recognize -> So, we won't handle it beyond this point
                                e.Cancel = true;
                            }
                            catch (System.Exception exc)
                            {
                                Debug.WriteLine(exc.StackTrace);
                            }
                        };

                        using (var reader = new StringReader(result.XmlContent))                           // Read Xml Data from string
                        {
                            stringLayoutSerializer.Deserialize(reader);
                        }
                    }
                }
                catch (System.Exception exception)
                {
                    Debug.WriteLine(exception);
                }
                finally
                {
                    // Make sure AvalonDock control is visible at the end of restoring layout
                    dockManager.Visibility  = Visibility.Visible;
                    loadProgress.Visibility = Visibility.Collapsed;
                }
            }, System.Windows.Threading.DispatcherPriority.Background);
        }
 private void OnLayoutLoaded_Event(object sender, LayoutLoadedEventArgs layoutLoadedEvent) => Application.Current.Dispatcher.Invoke(() =>
 {
     try
     {