简体   繁体   English

如何获取MMC SnapIn面板控件的父级

[英]How to get parent of MMC SnapIn panel controls

I'm developing my first MMC SnapIn. 我正在开发我的第一个MMC SnapIn。 I'd like to have per SnapIn configuration information. 我想要每个SnapIn配置信息。 I need to access that information from the SnapIn panel controls. 我需要从“管理单元”面板控件访问该信息。 I don't see any way to find the parent SnapIn object from those controls. 我看不到从这些控件中找到父SnapIn对象的任何方法。 Is there a method other than creating a static global? 除了创建静态全局以外,还有其他方法吗?

The FormViewDescription that's part of the SnapIn seems to create the controls using a default constructor: 作为SnapIn一部分的FormViewDescription似乎使用默认的构造函数创建控件:

// Create a form view for the root node.
FormViewDescription fvd = new FormViewDescription();
fvd.DisplayName = "Status";
fvd.ViewType = typeof(SelectionFormView); 
fvd.ControlType = typeof(SelectionControl);

Thanks 谢谢

In your control ( SelectionControl ) you can implement the Microsoft.ManagementConsole.IFormViewControl interface. 在控件( SelectionControl )中,可以实现Microsoft.ManagementConsole.IFormViewControl接口。 You then receive a call to your Initialize method with the FormView as argument. 然后,您会收到一个以FormView作为参数的Initialize方法的调用。 From this argument you can access the SnapIn. 通过此参数,您可以访问管理单元。

Here's a sample: 这是一个示例:

public class SelectionControl : UserControl, IFormViewControl
{
    ...

    public void Initialize(FormView view)
    {
        var snapIn = view.ScopeNode.SnapIn;

        ...
    }
}

[EDITED] [编辑]

You can use the following class as the base class of your control instead of UserControl : 您可以使用以下类作为控件的基类,而不是UserControl

//
//  @(#) FormViewControl.cs
//
//  Project:    usis.ManagementConsole
//  System:     Microsoft Visual Studio 2015
//  Author:     Udo Schäfer

using System;
using System.Windows.Forms;
using Microsoft.ManagementConsole;

namespace usis.ManagementConsole
{
    //  ---------------------
    //  FormViewControl class
    //  ---------------------

    /// <summary>
    /// Provides an empty control that can be used to create the content of a Windows Forms view.
    /// </summary>
    /// <seealso cref="UserControl" />
    /// <seealso cref="IFormViewControl" />

    public class FormViewControl : UserControl, IFormViewControl
    {
        #region fields

        private Control oldParent;

        #endregion fields

        #region properties

        //  -----------------
        //  FormView property
        //  -----------------

        /// <summary>
        /// Gets the associated Windows Forms view.
        /// </summary>
        /// <value>
        /// The form view.
        /// </value>

        protected FormView FormView { get; private set; }

        //  ---------------
        //  SnapIn property
        //  ---------------

        /// <summary>
        /// Gets the scope node's snap-in.
        /// </summary>
        /// <value>
        /// The scope node's snap-in.
        /// </value>

        protected NamespaceSnapInBase SnapIn
        {
            get { return this.FormView.ScopeNode.SnapIn; }
        }

        #endregion properties

        #region overrides

        //  ----------------------
        //  OnParentChanged method
        //  ----------------------

        /// <summary>
        /// Raises the <see cref="Control.ParentChanged"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>

        protected override void OnParentChanged(EventArgs e)
        {
            if (Parent != null)
            {
                if (!DesignMode) Size = Parent.ClientSize;
                Parent.SizeChanged += Parent_SizeChanged;
            }
            if (oldParent != null)
            {
                oldParent.SizeChanged -= Parent_SizeChanged;
            }
            oldParent = Parent;
            base.OnParentChanged(e);
        }

        #endregion overrides

        #region IFormViewControl implementation

        //  -----------------
        //  Initialize method
        //  -----------------

        /// <summary>
        /// Uses the associated Windows Forms view to initialize the control.
        /// </summary>
        /// <param name="view">The associated <c>FormView</c> value.</param>

        public void Initialize(FormView view)
        {
            FormView = view;
            OnInitialize();
        }

        //  -------------------
        //  OnInitialize method
        //  -------------------

        /// <summary>
        /// Called when the control is initialized.
        /// </summary>

        protected virtual void OnInitialize() { }

        #endregion IFormViewControl implementation

        #region private methods

        //  -------------------------
        //  Parent_SizeChanged method
        //  -------------------------

        private void Parent_SizeChanged(object sender, EventArgs e)
        {
            if (!DesignMode) Size = Parent.ClientSize;
        }

        #endregion private methods
    }
}

// eof "FormViewControl.cs"

(This will also resize the control inside it's parent.) (这还将在其父控件中调整控件的大小。)

Be careful: Do not access the SnapIn property in the constructor of your class. 注意:不要访问类的构造函数中的SnapIn属性。 Use the OnInitialize method instead. 请改用OnInitialize方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM