public MainWindow()
        {
            // show splash screen
            splashscreen = new Splash();
            splashscreen.Show();

            InitializeComponent();

            Messenger.Default.Register <NotificationMessage <string> >(this, PopNotificationMessage);

            // create DocumentPanel per the logical motion components defined in the config file
            var service = SimpleIoc.Default.GetInstance <SystemService>();

            #region Create logical motioin components panels
            foreach (var aligner in service.LogicalMotionComponentCollection)
            {
                // create a motion component panel control
                // which is the content of the document panel
                MotionComponentPanel uc = new MotionComponentPanel()
                {
                    // set the datacontext to the LogicalMotionComponent
                    DataContext = aligner
                };

                // create a document panel in the window
                DocumentPanel panel = new DocumentPanel()
                {
                    Name          = string.Format("dp{0}", aligner.Caption.Replace(" ", "")),
                    Caption       = aligner.Caption,
                    AllowMaximize = false,
                    AllowSizing   = false,
                    AllowFloat    = false,
                    AllowDock     = false,
                    //AllowClose = false,
                    ClosingBehavior = ClosingBehavior.HideToClosedPanelsCollection,

                    // put the user control into the panel
                    Content = uc
                };

                // add the documentpanel to the documentgroup
                MotionComponentPanelHost.Items.Add(panel);

                // find the icon shown in the button
                var image = (BitmapFrame)TryFindResource(aligner.Icon);

                // add view buttons to Ribbon toolbar
                BarCheckItem chk = new BarCheckItem()
                {
                    Content    = aligner.Caption,
                    LargeGlyph = image
                };

                // bind the IsCheck property to the document panel's Closed property
                Binding b = new Binding()
                {
                    Source    = panel,
                    Path      = new PropertyPath("Visibility"),
                    Mode      = BindingMode.TwoWay,
                    Converter = new VisibilityToBoolean()
                };
                chk.SetBinding(BarCheckItem.IsCheckedProperty, b);

                rpgView_MotionComponent.Items.Add(chk);

                // add buttons to show the preset position window
                BarButtonItem btn = new BarButtonItem()
                {
                    Content     = aligner.Caption,
                    LargeGlyph  = image,
                    DataContext = aligner
                };

                // raise the click event
                btn.ItemClick += (s, e) =>
                {
                    var view = new ViewMassMove(service, aligner);
                    var win  = new MassMoveWindow
                    {
                        DataContext = view
                    };
                    win.ShowDialog();
                };

                rpgPresetPositionButtonsHost.Items.Add(btn);
            }
            #endregion

            #region Create control panels for instruments

            ViewModelBase viewInstr;
            foreach (var instr in service.MeasurementInstrumentCollection)
            {
                UserControl uctrl = null;

                //TODO The following codes is not elegant, the code must be expanded if new type of instrument added into the system
                if (instr is Keithley2400)
                {
                    // create the user control for k2400
                    viewInstr = new ViewKeithley2400(instr as Keithley2400);
                    uctrl     = new Keithley2400ControlPanel()
                    {
                        DataContext = viewInstr
                    };
                }
                else if (instr is Newport2832C)
                {
                    // create the user control for k2400
                    viewInstr = new ViewNewport2832C(instr as Newport2832C);
                    uctrl     = new Newport2832cControlPanel()
                    {
                        DataContext = viewInstr
                    };
                }

                // create document panel in the window
                DocumentPanel panel = new DocumentPanel()
                {
                    Name            = string.Format("dp{0}", instr.DeviceClass.ToString("N")),
                    Caption         = instr.Config.Caption,
                    AllowMaximize   = false,
                    AllowSizing     = false,
                    AllowDock       = false,
                    AllowFloat      = false,
                    ClosingBehavior = ClosingBehavior.HideToClosedPanelsCollection,

                    // put the user control into the panel
                    Content = uctrl
                };

                // add the documentpanel to the documentgroup
                MotionComponentPanelHost.Items.Add(panel);

                // find the icon shown in the button
                var image = (BitmapFrame)TryFindResource(instr.Config.Icon);

                // add view buttons to Ribbon toolbar
                BarCheckItem chk = new BarCheckItem()
                {
                    Content    = instr.Config.Caption,
                    LargeGlyph = image
                };

                // bind the IsCheck property to the document panel's Closed property
                Binding b = new Binding()
                {
                    Source    = panel,
                    Path      = new PropertyPath("Visibility"),
                    Mode      = BindingMode.TwoWay,
                    Converter = new VisibilityToBoolean()
                };
                chk.SetBinding(BarCheckItem.IsCheckedProperty, b);

                rpgView_Equipments.Items.Add(chk);
            }

            #endregion

            #region Restore workspace layout
            var config = SimpleIoc.Default.GetInstance <ConfigManager>();
            for (int i = 0; i < MotionComponentPanelHost.Items.Count; i++)
            {
                var panel = MotionComponentPanelHost.Items[i];

                if (panel is DocumentPanel)
                {
                    //var layout =
                    //    (from items
                    //    in config.WorkspaceLayoutHelper.WorkspaceLayout
                    //     where items.PanelName == panel.Name
                    //     select items).First();

                    try
                    {
                        var setting = ((IEnumerable)config.ConfWSLayout.WorkspaceLayout).Cast <dynamic>().Where(item => item.PanelName == panel.Name).First();
                        panel.Visibility = setting.IsClosed ? Visibility.Hidden : Visibility.Visible;
                        ((DocumentPanel)panel).MDILocation = setting.MDILocation;
                    }
                    catch
                    {
                        ; // do nothing if the panel was not found in layout setting file
                    }
                }
            }
            #endregion
        }
示例#2
0
        public MainWindow()
        {
            #region Show Splash Screen
            // show splash screen
            ResetSplashCreated = new ManualResetEvent(false);

            // Create a new thread for the splash screen to run on
            SplashThread = new Thread(ShowSplash);
            SplashThread.SetApartmentState(ApartmentState.STA);
            SplashThread.IsBackground = true;
            SplashThread.Name         = "Splash Screen";
            SplashThread.Start();

            ResetSplashCreated.WaitOne();

            #endregion

            splashscreen.ShowMessage("Initializing main window ...");
            InitializeComponent();

            DevExpress.Xpf.Core.DXGridDataController.DisableThreadingProblemsDetection = true;


            Messenger.Default.Register <NotificationMessage <string> >(this, PopNotificationMessage);

            // create DocumentPanel per the logical motion components defined in the config file
            var service = SimpleIoc.Default.GetInstance <ViewSystemService>().Service;

            #region Create logical motioin components panels
            foreach (LogicalMotionComponent aligner in service.LogicalMotionComponentCollection)
            {
                splashscreen.ShowMessage(string.Format("Initializing {0} panel ...", aligner));

                // create a motion component panel control
                // which is the content of the document panel
                MotionComponentPanel mcPanel = new MotionComponentPanel()
                {
                    // set the datacontext to the LogicalMotionComponent
                    DataContext = aligner
                };

                // create a document panel
                DocumentPanel panel = new DocumentPanel()
                {
                    Name             = string.Format("dp{0}", aligner.Caption.Replace(" ", "")),
                    Caption          = aligner.Caption,
                    AllowContextMenu = false,
                    AllowMaximize    = false,
                    AllowSizing      = false,
                    AllowFloat       = false,
                    AllowDock        = false,
                    //AllowClose = false,
                    ClosingBehavior = ClosingBehavior.HideToClosedPanelsCollection,

                    // put the user control into the panel
                    Content = mcPanel
                };

                // add the documentpanel to the documentgroup
                MotionComponentPanelHost.Items.Add(panel);

                // find the icon shown in the button
                var image = (BitmapFrame)TryFindResource(aligner.Icon);

                // add view buttons to Ribbon toolbar
                BarCheckItem chk = new BarCheckItem()
                {
                    Content    = aligner.Caption,
                    LargeGlyph = image
                };

                // bind the IsCheck property to the document panel's Closed property
                Binding b = new Binding()
                {
                    Source    = panel,
                    Path      = new PropertyPath("Visibility"),
                    Mode      = BindingMode.TwoWay,
                    Converter = new VisibilityToBoolean()
                };
                chk.SetBinding(BarCheckItem.IsCheckedProperty, b);

                rpgView_MotionComponent.Items.Add(chk);
            }
            #endregion

            #region Create control panels for instruments

            ViewModelBase viewInstr;
            foreach (IInstrument instr in service.CollectionViewDefinedInstruments)
            {
                // if not icon specified, do not create the control panel.
                if (instr.Config.Icon == null)
                {
                    continue;
                }

                UserControl uctrl = null;

                //TODO The following codes is not elegant, the code must be expanded if new type of instrument added into the system
                if (instr is Keithley2400)
                {
                    // create the user control for k2400
                    viewInstr = new ViewKeithley2400(instr as Keithley2400);
                    uctrl     = new Keithley2400ControlPanel()
                    {
                        DataContext = viewInstr
                    };
                }
                else if (instr is Newport2832C)
                {
                    // create the user control for k2400
                    viewInstr = new ViewNewport2832C(instr as Newport2832C);
                    uctrl     = new Newport2832cControlPanel()
                    {
                        DataContext = viewInstr
                    };
                }

                splashscreen.ShowMessage(string.Format("Initializing {0} panel ...", instr));

                // create document panel in the window
                DocumentPanel panel = new DocumentPanel()
                {
                    Name            = string.Format("dp{0}", instr.DeviceClass.ToString("N")),
                    Caption         = instr.Config.Caption,
                    AllowMaximize   = false,
                    AllowSizing     = false,
                    AllowDock       = false,
                    AllowFloat      = false,
                    ClosingBehavior = ClosingBehavior.HideToClosedPanelsCollection,

                    // put the user control into the panel
                    Content = uctrl
                };

                // add the documentpanel to the documentgroup
                MotionComponentPanelHost.Items.Add(panel);

                // find the icon shown in the button
                var image = (BitmapFrame)TryFindResource(instr.Config.Icon);

                // add view buttons to Ribbon toolbar
                BarCheckItem chk = new BarCheckItem()
                {
                    Content    = instr.Config.Caption,
                    LargeGlyph = image
                };

                // bind the IsCheck property to the document panel's Closed property
                Binding b = new Binding()
                {
                    Source    = panel,
                    Path      = new PropertyPath("Visibility"),
                    Mode      = BindingMode.TwoWay,
                    Converter = new VisibilityToBoolean()
                };
                chk.SetBinding(BarCheckItem.IsCheckedProperty, b);

                rpgView_Equipments.Items.Add(chk);
            }

            #endregion

            splashscreen.ShowMessage(string.Format("Restoring workspace ..."));

            #region Restore workspace layout

            var config = service.SystemSettings;
            for (int i = 0; i < MotionComponentPanelHost.Items.Count; i++)
            {
                var panel = MotionComponentPanelHost.Items[i];

                if (panel is DocumentPanel)
                {
                    try
                    {
                        var setting = ((IEnumerable)config.ConfWSLayout.WorkspaceLayout).Cast <dynamic>().Where(item => item.PanelName == panel.Name).First();

                        // set visibility
                        panel.Visibility = setting.IsClosed ? Visibility.Hidden : Visibility.Visible;

                        // set location
                        ((DocumentPanel)panel).MDILocation = setting.MDILocation;
                    }
                    catch
                    {
                        ; // do nothing if the panel was not found in layout setting file
                    }
                }
            }
            #endregion
        }