MMC 管理单元中的 WPF

发布于 2024-08-30 12:26:27 字数 100 浏览 6 评论 0原文

有人可以提供一些在自定义 MMC 管理单元中使用 WPF 的示例代码吗?我是 WPF 新手,我已经了解编写 MMC 管理单元的示例,但我不明白如何选择 WPF 而不是 Winforms。

Can someone provide some sample code for using WPF in a custom MMC snapin? I'm new to WPF, and I've understood the samples for writing MMC snapins, but I do not understand how to choose WPF instead of Winforms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-09-06 12:26:27

我刚刚花了一上午的时间为我正在从事的项目构建这个确切的场景。这是代码的上下文无关版本:

/// <summary>
/// Defines a scope node with a default Forms view that will host a
/// WPF control.
/// </summary>
public class WPFScopeNode: ScopeNode
{
    /// <summary>
    /// Instantiates a new specialized scope node.
    /// </summary>
    public WPFScopeNode()
        : base()
    {
        this.DisplayName = "Name in tree view";

        // TODO: Create any child scope nodes, adding them to 'this.Children'.

        // Attach a forms view to this node that hosts a WPF control.
        var wpfFormView = new FormViewDescription
        {
            DisplayName = "Name of view",
            ControlType = typeof(WPFHostControl),
            ViewType = typeof(WPFHostFormView)
        };
        this.ViewDescriptions.Add(wpfFormView );
    }
}


/// <summary>
/// This class provides a model-interface for the hosted WPF control.
/// </summary>
public class WPFHostFormView : FormView
{
    public WPFHostFormView ()
    {
        this.DescriptionBarText = "WPF Host View";
    }

    /// <summary>
    /// Defines the structure of the form view.
    /// </summary>
    /// <param name="status"></param>
    protected override void OnInitialize(AsyncStatus status)
    {
        base.OnInitialize(status);

        // TODO: This may be a good place to pass 'this.Control' the
        // data context that will be used for databinding by the WPF
        // control.  Note that 'this.Control' is the Windows Forms
        // UserControl that contains an ElementHost which, in turn,
        // hosts the WPF UserControl of interest.
    }
}


/// <summary>
/// Defines a Windows Forms control that hosts a specific WPF UserControl.
/// Note the logic for automatic resizing to the parent surface in the
/// MMC MDI interface.
/// Also, 'MyWPFUserControl' is just a standard WPF
/// UserControl; nothing special needs to be done there for MMC integration.
/// </summary>
public partial class WPFHostControl : UserControl
{
    // TODO: Expose a method for setting the DataContext of the hosted element!

    public WPFHostControl ()
    {
        InitializeComponent();

        ParentChanged += new EventHandler(WPFHostControl_ParentChanged);
    }

    void WPFHostControl_ParentChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
            Parent.ClientSizeChanged +=
                new EventHandler(Parent_ClientSizeChanged);
        }
    }

    void Parent_ClientSizeChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
        }
    }

    private MyWPFUserControl hostedControl;
    private System.Windows.Forms.Integration.ElementHost elementHost;

    /// <remarks>
    /// To make the Visual Studio designer work smoothly, you could
    /// split this function into a *.designer.cs file using a partial
    /// class definition.
    /// </remarks>
    private void InitializeComponent()
    {
        this.elementHost = new System.Windows.Forms.Integration.ElementHost();
        this.hostedControl = new MyWPFUserControl();
        this.SuspendLayout();
        // 
        // elementHost
        // 
        this.elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
        this.elementHost.Location = new System.Drawing.Point(0, 0);
        this.elementHost.Name = "elementHost";
        this.elementHost.Size = new System.Drawing.Size(150, 150);
        this.elementHost.TabIndex = 0;
        this.elementHost.Child = this.hostedControl;
        // 
        // EnvironmentDashboard
        // 
        this.Controls.Add(this.elementHost);
        this.Name = "WPF Host View";
        this.ResumeLayout(false);

    }
}

I've just spent the morning building this exact scenario for a project I'm working on. Here's the context-free version of the code:

/// <summary>
/// Defines a scope node with a default Forms view that will host a
/// WPF control.
/// </summary>
public class WPFScopeNode: ScopeNode
{
    /// <summary>
    /// Instantiates a new specialized scope node.
    /// </summary>
    public WPFScopeNode()
        : base()
    {
        this.DisplayName = "Name in tree view";

        // TODO: Create any child scope nodes, adding them to 'this.Children'.

        // Attach a forms view to this node that hosts a WPF control.
        var wpfFormView = new FormViewDescription
        {
            DisplayName = "Name of view",
            ControlType = typeof(WPFHostControl),
            ViewType = typeof(WPFHostFormView)
        };
        this.ViewDescriptions.Add(wpfFormView );
    }
}


/// <summary>
/// This class provides a model-interface for the hosted WPF control.
/// </summary>
public class WPFHostFormView : FormView
{
    public WPFHostFormView ()
    {
        this.DescriptionBarText = "WPF Host View";
    }

    /// <summary>
    /// Defines the structure of the form view.
    /// </summary>
    /// <param name="status"></param>
    protected override void OnInitialize(AsyncStatus status)
    {
        base.OnInitialize(status);

        // TODO: This may be a good place to pass 'this.Control' the
        // data context that will be used for databinding by the WPF
        // control.  Note that 'this.Control' is the Windows Forms
        // UserControl that contains an ElementHost which, in turn,
        // hosts the WPF UserControl of interest.
    }
}


/// <summary>
/// Defines a Windows Forms control that hosts a specific WPF UserControl.
/// Note the logic for automatic resizing to the parent surface in the
/// MMC MDI interface.
/// Also, 'MyWPFUserControl' is just a standard WPF
/// UserControl; nothing special needs to be done there for MMC integration.
/// </summary>
public partial class WPFHostControl : UserControl
{
    // TODO: Expose a method for setting the DataContext of the hosted element!

    public WPFHostControl ()
    {
        InitializeComponent();

        ParentChanged += new EventHandler(WPFHostControl_ParentChanged);
    }

    void WPFHostControl_ParentChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
            Parent.ClientSizeChanged +=
                new EventHandler(Parent_ClientSizeChanged);
        }
    }

    void Parent_ClientSizeChanged(object sender, EventArgs e)
    {
        if (Parent != null)
        {
            Size = Parent.ClientSize;
        }
    }

    private MyWPFUserControl hostedControl;
    private System.Windows.Forms.Integration.ElementHost elementHost;

    /// <remarks>
    /// To make the Visual Studio designer work smoothly, you could
    /// split this function into a *.designer.cs file using a partial
    /// class definition.
    /// </remarks>
    private void InitializeComponent()
    {
        this.elementHost = new System.Windows.Forms.Integration.ElementHost();
        this.hostedControl = new MyWPFUserControl();
        this.SuspendLayout();
        // 
        // elementHost
        // 
        this.elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
        this.elementHost.Location = new System.Drawing.Point(0, 0);
        this.elementHost.Name = "elementHost";
        this.elementHost.Size = new System.Drawing.Size(150, 150);
        this.elementHost.TabIndex = 0;
        this.elementHost.Child = this.hostedControl;
        // 
        // EnvironmentDashboard
        // 
        this.Controls.Add(this.elementHost);
        this.Name = "WPF Host View";
        this.ResumeLayout(false);

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文