Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

How To Embed An Application Into a Docking Library

, 3 Sep 2011
Step by Step conversion of an application into a Docking application component
SofaAdditionalExamples_-_Step_by_Step.zip
Examples
Step by step SofaComponization
0. Original CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.suo
Data
Properties
Settings.settings
1. From Application To Component
1. From Application To Component.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
Properties
Settings.settings
SofaBasicContainer
Properties
app.manifest
Settings.settings
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
2. From Simple Component to sub-Components
2. From Simple Component to sub-Components.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
MainComponent.xaml.cs.bak
Properties
Settings.settings
SofaBasicContainer
BaseWindow.xaml.cs.bak
Properties
app.manifest
Settings.settings
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
3. About Perspectives
3. About Perspectives.suo
CSWPFMasterDetailBinding
CSWPFMasterDetailBinding.csproj.user
Data
MainComponent.xaml.cs.bak
Properties
Settings.settings
Service References
SofaBasicContainer
BaseWindow.xaml.cs.bak
Properties
app.manifest
Settings.settings
Resources
Sofa.bmp
Sofa
SofaBasicContainer.csproj - Copie.user
Perspectives
_Dev_SofaBasicContainer.csproj.user
_SofaBasicContainer.csproj.user
External dependencies
log4net.dll
Mef.Codeplex
System.ComponentModel.Composition.Codeplex.dll
System.ComponentModel.Composition.Registration.dll
System.Reflection.Context.dll
Prism.Mef_Codeplex
Microsoft.Practices.Prism.dll
Microsoft.Practices.Prism.Interactivity.dll
Microsoft.Practices.Prism.MefExtensions.dll
Microsoft.Practices.Prism.UnityExtensions.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll
WPFToolkit.dll
Libraries
ExternalFrameworks.dll
Sofa.Commons.dll
Sofa.Component.dll
Sofa.Container.dll
Sofa.Themes.dll
��using System;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using Sofa.Commons;

using Sofa.Container;

using AvalonDock;

using System.Reflection;

using System.ComponentModel;

using System.ComponentModel.Composition;

using System.ComponentModel.Composition.Hosting;





namespace SofaBasicContainer

{



    public partial class BaseWindow : Window, IBaseContainer, IPartImportsSatisfiedNotification

    {

        public string PerspectiveTitle

        {

            get { return (string)GetValue(PerspectiveTitleProperty); }

            set { SetValue(PerspectiveTitleProperty, "SofaBasicContainer - " + value); }

        }



        public static readonly DependencyProperty PerspectiveTitleProperty =

            DependencyProperty.Register("PerspectiveTitle", typeof(string), typeof(BaseWindow), new UIPropertyMetadata(string.Empty));



        //Import all SofaComponents

        [ImportMany("Sofa.Examples.SofaComponization.SofaBasicContainer_ViewContract", AllowRecomposition = true)]

        private ExportFactory<UserControl, ISofaComponent_Metadata>[] _views = null;



        PerspectiveHelper perspectiveHelper;

        private CompositionContainer _container;



        //IBaseContainer implementation

        public SofaContainer SofaContainer

        {

            get { return sofaContainer; }

            set { sofaContainer = value; }

        }



        //Not used, required by IBaseContainer

        public SofaMenu SofaMenu

        {

            get { return null; }

            set {  }

        }



        public BaseWindow()

        {

            InitializeComponent();



            Compose();



            PerspectiveTitle = "...";

            perspectiveHelper = new PerspectiveHelper(sofaContainer, this);



            sofaContainer.CmpModelList.Sort(delegate(CmpModel cmp1, CmpModel cmp2) { return cmp1.Label.CompareTo(cmp2.Label); });

            ViewComponents.DataContext = sofaContainer.CmpModelList;

            ViewPerspectives.DataContext = perspectiveHelper.perspectivesList;            

        }



        public void OnImportsSatisfied()

        {

            foreach (var view in _views)

            {

                sofaContainer.RegisterComponent(view);

            }

        }



        private bool Compose()

        {

            var catalog = new AggregateCatalog();

            //Components are built in the SofaContainer\bin\Debug\AddIns\ directory

            catalog.Catalogs.Add(new DirectoryCatalog("AddIns"));



            _container = new CompositionContainer(catalog);

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(this);

            batch.AddExportedValue<CompositionContainer>(_container);



            try { _container.Compose(batch); }

            catch (CompositionException compositionException)

            {

                MessageBox.Show(compositionException.ToString());

            }

            return true;

        }



        private void Window_Loaded(object sender, RoutedEventArgs e)

        {



            if (Parameters.Default.Componization_CurrentPerspective != "")

            {

                Perspective p = sofaContainer.LoadPerspective(Parameters.Default.Componization_CurrentPerspective);

                PerspectiveTitle = Parameters.Default.Componization_CurrentPerspective;

                perspectiveHelper.UpdatePerspectiveStatus();

            }



        }



        //Closing event handle declared in xaml

        private void Window_Closing(object sender, CancelEventArgs e)

        {

            perspectiveHelper.SaveCurrentPerspective();

            e.Cancel = !sofaContainer.CloseAllComponents();

        }



        #region Sofacomponents

        //Sofacomponents management - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 



        private void LoadComponent_Click(object sender, RoutedEventArgs e)

        {

            String componentName = (((System.Windows.Controls.MenuItem)sender).Tag.ToString());

            sofaContainer.OpenComponent(componentName);

        }



        private void CloseComponent_Click(object sender, RoutedEventArgs e)

        {

            SofaComponent sofaComponent = null;

            

            //Search for the clicked Sofacomponent

            //First try IsActiveContent (Component is in a DockablePane)

            try

            {

                sofaComponent = sofaContainer.SofaComponentList.Single(c => ((ManagedContent)c.DockableContent).IsActiveContent == true);

            } catch {}

            //If not found

            if (sofaComponent == null)

            {

                //Then try IsActiveDocument (Component is in a DocumentPane)

                try

                {

                    sofaComponent = sofaContainer.SofaComponentList.Single(c => ((ManagedContent)c.DockableContent).IsActiveDocument == true);

                }

                catch { }

            }



            if (sofaComponent != null) sofaContainer.CloseComponent(sofaComponent.GUID);

        }



        private void CloseAllComponent_Click(object sender, RoutedEventArgs e)

        {

            sofaContainer.CloseAllComponents();

        }

        #endregion



        #region Perspectives

        //Perspectives management - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 



        private void LoadPerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.LoadPerspective(((System.Windows.Controls.MenuItem)sender).Header.ToString());

        }



        private void SavePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.SaveCurrentPerspective();

        }



        private void NewPerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.NewPerspective();

        }



        private void DeletePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.DeletePerspective();

        }



        private void RenamePerspective_Click(object sender, RoutedEventArgs e)

        {

            perspectiveHelper.RenamePerspective();

        }

        #endregion

    }

}



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sofa Team

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 3 Sep 2011
Article Copyright 2011 by Sofa Team
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid