逐步解說:在 Windows Form 中裝載 3D WPF 複合控制項
本逐步解說示範如何使用 ElementHost 控制項建立 WPF 複合控制項,並將其裝載於 Windows Forms 控制項和表單中。
在本逐步解說中,您將實作包含兩個子控制項的 WPF UserControl 。 UserControl 顯示了一個立體(3D)圓錐。 呈現 3D 物件比使用 Windows Forms 更容易使用 WPF。 因此,在 Windows Forms 中裝載 WPF UserControl 類別以建立 3D 圖形是合理的。
這個逐步解說中所述的工作包括:
建立 WPF UserControl。
建立 Windows Forms 主專案
Hosting WPF UserControl。
必要條件
您需要下列元件才能完成這個逐步解說:
- Visual Studio 2017
建立使用者控制項
建立一個名為
HostingWpfUserControlInWf
的 WPF 使用者控制連結庫 專案。在 WPF 設計工具中開啟 UserControl1.xaml 檔案。
使用下列程式碼取代所產生的程式碼:
<UserControl x:Class="HostingWpfUserControlInWf.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Grid> <!-- Place a Label control at the top of the view. --> <Label HorizontalAlignment="Center" TextBlock.TextAlignment="Center" FontSize="20" Foreground="Red" Content="Model: Cone"/> <!-- Viewport3D is the rendering surface. --> <Viewport3D Name="myViewport" > <!-- Add a camera. --> <Viewport3D.Camera> <PerspectiveCamera FarPlaneDistance="20" LookDirection="0,0,1" UpDirection="0,1,0" NearPlaneDistance="1" Position="0,0,-3" FieldOfView="45" /> </Viewport3D.Camera> <!-- Add models. --> <Viewport3D.Children> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup > <Model3DGroup.Children> <!-- Lights, MeshGeometry3D and DiffuseMaterial objects are added to the ModelVisual3D. --> <DirectionalLight Color="#FFFFFFFF" Direction="3,-4,5" /> <!-- Define a red cone. --> <GeometryModel3D> <GeometryModel3D.Geometry> <MeshGeometry3D Positions="0.293893 -0.5 0.404509 0.475528 -0.5 0.154509 0 0.5 0 0.475528 -0.5 0.154509 0 0.5 0 0 0.5 0 0.475528 -0.5 0.154509 0.475528 -0.5 -0.154509 0 0.5 0 0.475528 -0.5 -0.154509 0 0.5 0 0 0.5 0 0.475528 -0.5 -0.154509 0.293893 -0.5 -0.404509 0 0.5 0 0.293893 -0.5 -0.404509 0 0.5 0 0 0.5 0 0.293893 -0.5 -0.404509 0 -0.5 -0.5 0 0.5 0 0 -0.5 -0.5 0 0.5 0 0 0.5 0 0 -0.5 -0.5 -0.293893 -0.5 -0.404509 0 0.5 0 -0.293893 -0.5 -0.404509 0 0.5 0 0 0.5 0 -0.293893 -0.5 -0.404509 -0.475528 -0.5 -0.154509 0 0.5 0 -0.475528 -0.5 -0.154509 0 0.5 0 0 0.5 0 -0.475528 -0.5 -0.154509 -0.475528 -0.5 0.154509 0 0.5 0 -0.475528 -0.5 0.154509 0 0.5 0 0 0.5 0 -0.475528 -0.5 0.154509 -0.293892 -0.5 0.404509 0 0.5 0 -0.293892 -0.5 0.404509 0 0.5 0 0 0.5 0 -0.293892 -0.5 0.404509 0 -0.5 0.5 0 0.5 0 0 -0.5 0.5 0 0.5 0 0 0.5 0 0 -0.5 0.5 0.293893 -0.5 0.404509 0 0.5 0 0.293893 -0.5 0.404509 0 0.5 0 0 0.5 0 " Normals="0.7236065,0.4472139,0.5257313 0.2763934,0.4472138,0.8506507 0.5308242,0.4294462,0.7306172 0.2763934,0.4472138,0.8506507 0,0.4294458,0.9030925 0.5308242,0.4294462,0.7306172 0.2763934,0.4472138,0.8506507 -0.2763934,0.4472138,0.8506507 0,0.4294458,0.9030925 -0.2763934,0.4472138,0.8506507 -0.5308242,0.4294462,0.7306172 0,0.4294458,0.9030925 -0.2763934,0.4472138,0.8506507 -0.7236065,0.4472139,0.5257313 -0.5308242,0.4294462,0.7306172 -0.7236065,0.4472139,0.5257313 -0.858892,0.429446,0.279071 -0.5308242,0.4294462,0.7306172 -0.7236065,0.4472139,0.5257313 -0.8944269,0.4472139,0 -0.858892,0.429446,0.279071 -0.8944269,0.4472139,0 -0.858892,0.429446,-0.279071 -0.858892,0.429446,0.279071 -0.8944269,0.4472139,0 -0.7236065,0.4472139,-0.5257313 -0.858892,0.429446,-0.279071 -0.7236065,0.4472139,-0.5257313 -0.5308242,0.4294462,-0.7306172 -0.858892,0.429446,-0.279071 -0.7236065,0.4472139,-0.5257313 -0.2763934,0.4472138,-0.8506507 -0.5308242,0.4294462,-0.7306172 -0.2763934,0.4472138,-0.8506507 0,0.4294458,-0.9030925 -0.5308242,0.4294462,-0.7306172 -0.2763934,0.4472138,-0.8506507 0.2763934,0.4472138,-0.8506507 0,0.4294458,-0.9030925 0.2763934,0.4472138,-0.8506507 0.5308249,0.4294459,-0.7306169 0,0.4294458,-0.9030925 0.2763934,0.4472138,-0.8506507 0.7236068,0.4472141,-0.5257306 0.5308249,0.4294459,-0.7306169 0.7236068,0.4472141,-0.5257306 0.8588922,0.4294461,-0.27907 0.5308249,0.4294459,-0.7306169 0.7236068,0.4472141,-0.5257306 0.8944269,0.4472139,0 0.8588922,0.4294461,-0.27907 0.8944269,0.4472139,0 0.858892,0.429446,0.279071 0.8588922,0.4294461,-0.27907 0.8944269,0.4472139,0 0.7236065,0.4472139,0.5257313 0.858892,0.429446,0.279071 0.7236065,0.4472139,0.5257313 0.5308242,0.4294462,0.7306172 0.858892,0.429446,0.279071 " TriangleIndices="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 " /> </GeometryModel3D.Geometry> <GeometryModel3D.Material> <DiffuseMaterial> <DiffuseMaterial.Brush> <SolidColorBrush Color="Red" Opacity="1.0"/> </DiffuseMaterial.Brush> </DiffuseMaterial> </GeometryModel3D.Material> </GeometryModel3D> </Model3DGroup.Children> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D.Children> </Viewport3D> </Grid> </UserControl>
該程式碼定義了一個包含兩個子控制項的 System.Windows.Controls.UserControl 。 第一個子控制項是 System.Windows.Controls.Label 控制項;第二個是顯示 3D 圓錐的 Viewport3D 控制項。
建立主專案
將名為
WpfUserControlHost
Windows Forms 應用程式 (.NET Framework) 專案新增至方案。在方案總管中,新增名為 WindowsFormsIntegration.dll 之 WindowsFormsIntegration 組件的參考。
加入下列 WPF 組件的參考:
PresentationCore
PresentationFramework
WindowsBase
加入
HostingWpfUserControlInWf
專案的參考。在方案總管中,將
WpfUserControlHost
專案設為啟始專案。
裝載使用者控制項
在 Windows Forms 設計工具中,開啟 Form1。
在屬性視窗中,按一下 事件] 按鈕,然後按兩下 Load 事件來建立事件處理常式。
程式碼編輯器將會開啟並顯示由設計工具所產生的
Form1_Load
事件處理常式。將 Form1.cs 中的程式碼取代為下列程式碼。
該
Form1_Load
事件處理程式會建立UserControl1
的實例,並將其加入至 ElementHost 控制項的子控制項集合。 該 ElementHost 控制項會加入至表單的子控制項集合中。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Windows.Forms.Integration; namespace WpfUserControlHost { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Create the ElementHost control for hosting the // WPF UserControl. ElementHost host = new ElementHost(); host.Dock = DockStyle.Fill; // Create the WPF UserControl. HostingWpfUserControlInWf.UserControl1 uc = 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. this.Controls.Add(host); } } }
Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports System.Windows.Forms.Integration Public Class Form1 Inherits Form Private 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 Sub End Class
按 F5 以建置並執行應用程式。