【问题标题】:How to get parent of MMC SnapIn panel controls如何获取 MMC SnapIn 面板控件的父级
【发布时间】:2016-06-07 14:42:50
【问题描述】:

我正在开发我的第一个 MMC 管理单元。我想要每个 SnapIn 配置信息。我需要从 SnapIn 面板控件访问该信息。我看不到从这些控件中找到父 SnapIn 对象的任何方法。除了创建静态全局之外,还有其他方法吗?

作为 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);

谢谢

【问题讨论】:

    标签: c# mmc3


    【解决方案1】:

    在您的控件 (SelectionControl) 中,您可以实现 Microsoft.ManagementConsole.IFormViewControl 接口。然后,您会收到以 FormView 为参数的 Initialize 方法的调用。通过这个参数,您可以访问 SnapIn。

    这是一个示例:

    public class SelectionControl : UserControl, IFormViewControl
    {
        ...
    
        public void Initialize(FormView view)
        {
            var snapIn = view.ScopeNode.SnapIn;
    
            ...
        }
    }
    

    [已编辑]

    您可以使用以下类作为控件的基类,而不是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"
    

    (这也将调整其父控件的大小。)

    注意:不要在类的构造函数中访问SnapIn 属性。请改用OnInitialize 方法。

    【讨论】:

    • 该成员编译良好,但从未被调用。
    • 你的课堂上添加接口IFormViewControl了吗?
    • 我明天会仔细检查,但我很确定我做到了。我添加了一个跟踪并使用 debugview 来查看它是否在运行时被调用
    • 我已经在源代码中有一个特定的接口方法( IFormViewControl.Initialize() )。我添加了您的非特定代码的副本并且编译器允许它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多