2016-06-07 23 views
0

我正在開發我的第一個MMC管理單元。我想每個SnapIn配置信息。我需要從SnapIn面板控件訪問這些信息。我沒有看到從這些控件中找到父SnapIn對象的方法。除了創建靜態全局之外,還有其他方法嗎?如何獲取MMC 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); 

感謝

回答

1

在你的控制(SelectionControl),您可以實現Microsoft.ManagementConsole.IFormViewControl接口。然後您會收到一個電話號碼Initialize,其中FormView作爲參數。從這個參數你可以訪問管理單元。

這裏有一個例子:

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方法。

+0

該會員編譯得很好,但從未被調用。 – Jay

+0

您是否已將接口IFormViewControl添加到您的類中? – Udo

+0

明天我會仔細檢查,但我很確定我做了。我添加了一個跟蹤並使用debugview來查看它是否在運行時被調用 – Jay

相關問題