演练:在 Windows 窗体中承载 Windows Presentation Foundation 复合控件 【转载】
http://msdn.microsoft.com/zh-cn/library/ms745781.aspx
更新:2007 年 11 月
本演練演示如何創建 WPF 復合控件,并通過使用 ElementHost 控件在 Windows 窗體控件和窗體中承載它。
在本演練中,將實現一個包含兩個子控件的 WPFUserControl。 UserControl 顯示一個三維 (3-D) 圓錐。使用 WPF 呈現三維對象比使用 Windows 窗體更簡單。因此,對于在 Windows 窗體中創建三維圖形,承載 WPFUserControl 類非常有意義。
本演練涉及以下任務:
-
創建 WPFUserControl。
-
創建 Windows 窗體宿主項目。
-
承載 WPFUserControl。
有關本演練中所闡釋任務的完整代碼清單,請參見在 Windows 窗體中承載 Windows Presentation Foundation 復合控件的示例。
注意 顯示的對話框和菜單命令可能與“幫助”中所述的有所不同,具體取決于當前的設置或版本。若要更改設置,請在“工具”菜單上選擇“導入和導出設置”。有關更多信息,請參見 Visual Studio 設置。
先決條件
您需要以下組件來完成本演練:
-
Visual Studio 2008.
創建 UserControl
創建 UserControl
創建一個名為 HostingWpfUserControlInWf 的 WPF 用戶控件庫項目。
在 WPF 設計器中打開 UserControl1.xaml。
用下面的代碼替換生成的代碼。
該代碼定義一個包含兩個子控件的 System.Windows.Controls.UserControl。第一個子控件是 System.Windows.Controls.Label 控件;第二個控件是顯示三維圓錐的 Viewport3D 控件。
創建 Windows 窗體宿主項目
創建宿主項目
將名為 WpfUserControlHost 的 Windows 應用程序項目添加到解決方案中。有關更多信息,請參見“添加新項目”對話框。
在解決方案資源管理器中,添加一個對名為 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程序集的引用。
添加對以下 WPF 程序集的引用:
-
PresentationCore
-
PresentationFramework
-
WindowsBase
添加對 HostingWpfUserControlInWf 項目的引用。
在解決方案資源管理器中,將 WpfUserControlHost 項目設置為啟動項目。
承載 Windows Presentation Foundation UserControl
承載 UserControl
在 Windows 窗體設計器中打開 Form1。
在“屬性”窗口中,單擊“事件”,然后雙擊 Load 事件以創建事件處理程序。
代碼編輯器打開并定位到新生成的 Form1_Load 事件處理程序。
將 Form1.cs 中的代碼替換為以下代碼。
Form1_Load 事件處理程序將創建一個 UserControl1 實例,并將其添加到ElementHost 控件的子控件集合中。ElementHost 控件將被添加到窗體的子控件集合中。
Visual Basic
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.FormsImports System.Windows.Forms.IntegrationPublic Class Form1Inherits FormPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Create the ElementHost control for hosting the' WPF UserControl.Dim host As New ElementHost()
host.Dock = DockStyle.Fill' Create the WPF UserControl.Dim uc As New HostingWpfUserControlInWf.UserControl1()' Assign the WPF UserControl to the ElementHost control's' Child property.host.Child = uc' Add the ElementHost control to the form's' collection of child controls.Me.Controls.Add(host)End SubEnd Class按 F5 生成并運行該應用程序。
轉載于:https://www.cnblogs.com/chenxizhang/archive/2009/05/07/1451281.html
總結
以上是生活随笔為你收集整理的演练:在 Windows 窗体中承载 Windows Presentation Foundation 复合控件 【转载】的全部內容,希望文章能夠幫你解決所遇到的問題。