Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
In dit voorbeeld ziet u hoe u een invoegtoepassing maakt die een Windows Presentation Foundation (WPF) retourneert naar een zelfstandige WPF-hosttoepassing.
De invoegtoepassing retourneert een gebruikersinterface die een WPF-gebruikersbesturingselement is. De inhoud van het gebruikersbeheer is één knop die, wanneer erop wordt geklikt, een berichtvak weergeeft. De zelfstandige WPF-toepassing fungeert als host voor de invoegtoepassing en geeft het gebruikersbesturingselement weer (geretourneerd door de invoegtoepassing) als de inhoud van het hoofdtoepassingsvenster.
Voorwaarden
Dit voorbeeld belicht de WPF-extensies voor het invoegtoepassingsmodel van het .NET Framework die dit scenario mogelijk maken en gaat ervan uit dat het volgende geldt:
Kennis van het .NET Framework-invoegtoepassingsmodel, waaronder pijplijn, invoegtoepassing en hostontwikkeling. Als u niet bekend bent met deze concepten, raadpleegt u invoegtoepassingen en uitbreidbaarheid. Zie Walkthrough: Een uitbreidbare toepassing makenvoor een zelfstudie waarin de implementatie van een pijplijn, een invoegtoepassing en een hosttoepassing wordt gedemonstreerd.
Kennis van de WPF-extensies voor het .NET Framework-invoegtoepassingsmodel, dat u hier kunt vinden: WPF Add-Ins Overzicht.
Voorbeeld
Voor het maken van een invoegtoepassing die een WPF-gebruikersinterface retourneert, is specifieke code vereist voor elk pijplijnsegment, de invoegtoepassing en de hosttoepassing.
Het contractpijplijnsegment implementeren
Een methode moet worden gedefinieerd door het contract voor het retourneren van een gebruikersinterface en de retourwaarde moet van het type INativeHandleContractzijn. Dit wordt gedemonstreerd door de GetAddInUI
methode van het IWPFAddInContract
contract in de volgende code.
using System.AddIn.Contract;
using System.AddIn.Pipeline;
namespace Contracts
{
/// <summary>
/// Defines the services that an add-in will provide to a host application
/// </summary>
[AddInContract]
public interface IWPFAddInContract : IContract
{
// Return a UI to the host application
INativeHandleContract GetAddInUI();
}
}
Imports System.AddIn.Contract
Imports System.AddIn.Pipeline
Namespace Contracts
''' <summary>
''' Defines the services that an add-in will provide to a host application
''' </summary>
<AddInContract>
Public Interface IWPFAddInContract
Inherits IContract
' Return a UI to the host application
Function GetAddInUI() As INativeHandleContract
End Interface
End Namespace
Het Add-In-kijkpijplijnsegment implementeren
Omdat de invoegtoepassing de UIs implementeert als subklassen van FrameworkElement, moet de methode in de invoegtoepassingsweergave die overeenkomt met IWPFAddInView.GetAddInUI
een waarde van het type FrameworkElementretourneren. De volgende code toont de add-in weergave van het contract, geïmplementeerd als een interface.
using System.AddIn.Pipeline;
using System.Windows;
namespace AddInViews
{
/// <summary>
/// Defines the add-in's view of the contract
/// </summary>
[AddInBase]
public interface IWPFAddInView
{
// The add-in's implementation of this method will return
// a UI type that directly or indirectly derives from
// FrameworkElement.
FrameworkElement GetAddInUI();
}
}
Imports System.AddIn.Pipeline
Imports System.Windows
Namespace AddInViews
''' <summary>
''' Defines the add-in's view of the contract
''' </summary>
<AddInBase>
Public Interface IWPFAddInView
' The add-in's implementation of this method will return
' a UI type that directly or indirectly derives from
' FrameworkElement.
Function GetAddInUI() As FrameworkElement
End Interface
End Namespace
Het implementeren van het Add-In-Side Adapter-pijplijnsegment
De contractmethode retourneert een INativeHandleContract, maar de invoegtoepassing retourneert een FrameworkElement (zoals opgegeven in de invoegtoepassingsweergave). Daarom moet de FrameworkElement worden omgezet in een INativeHandleContract voordat de isolatiegrens wordt overschreden. Dit werk wordt uitgevoerd door de invoegtoepassingsadapter door ViewToContractAdapteraan te roepen, zoals wordt weergegeven in de volgende code.
using System.AddIn.Contract;
using System.AddIn.Pipeline;
using System.Windows;
using AddInViews;
using Contracts;
namespace AddInSideAdapters
{
/// <summary>
/// Adapts the add-in's view of the contract to the add-in contract
/// </summary>
[AddInAdapter]
public class WPFAddIn_ViewToContractAddInSideAdapter : ContractBase, IWPFAddInContract
{
IWPFAddInView wpfAddInView;
public WPFAddIn_ViewToContractAddInSideAdapter(IWPFAddInView wpfAddInView)
{
// Adapt the add-in view of the contract (IWPFAddInView)
// to the contract (IWPFAddInContract)
this.wpfAddInView = wpfAddInView;
}
public INativeHandleContract GetAddInUI()
{
// Convert the FrameworkElement from the add-in to an INativeHandleContract
// that will be passed across the isolation boundary to the host application.
FrameworkElement fe = this.wpfAddInView.GetAddInUI();
INativeHandleContract inhc = FrameworkElementAdapters.ViewToContractAdapter(fe);
return inhc;
}
}
}
Imports System.AddIn.Contract
Imports System.AddIn.Pipeline
Imports System.Windows
Imports AddInViews
Imports Contracts
Namespace AddInSideAdapters
''' <summary>
''' Adapts the add-in's view of the contract to the add-in contract
''' </summary>
<AddInAdapter>
Public Class WPFAddIn_ViewToContractAddInSideAdapter
Inherits ContractBase
Implements IWPFAddInContract
Private wpfAddInView As IWPFAddInView
Public Sub New(ByVal wpfAddInView As IWPFAddInView)
' Adapt the add-in view of the contract (IWPFAddInView)
' to the contract (IWPFAddInContract)
Me.wpfAddInView = wpfAddInView
End Sub
Public Function GetAddInUI() As INativeHandleContract Implements IWPFAddInContract.GetAddInUI
' Convert the FrameworkElement from the add-in to an INativeHandleContract
' that will be passed across the isolation boundary to the host application.
Dim fe As FrameworkElement = Me.wpfAddInView.GetAddInUI()
Dim inhc As INativeHandleContract = FrameworkElementAdapters.ViewToContractAdapter(fe)
Return inhc
End Function
End Class
End Namespace
Het hostweergave pijplijnsegment implementeren
Omdat in de hosttoepassing een FrameworkElementwordt weergegeven, moet de methode in de hostweergave die overeenkomt met IWPFAddInHostView.GetAddInUI
een waarde van het type FrameworkElementretourneren. De volgende code toont de hostweergave van het contract, geïmplementeerd als interface.
using System.Windows;
namespace HostViews
{
/// <summary>
/// Defines the host's view of the add-in
/// </summary>
public interface IWPFAddInHostView
{
// The view returns as a class that directly or indirectly derives from
// FrameworkElement and can subsequently be displayed by the host
// application by embedding it as content or sub-content of a UI that is
// implemented by the host application.
FrameworkElement GetAddInUI();
}
}
Imports System.Windows
Namespace HostViews
''' <summary>
''' Defines the host's view of the add-in
''' </summary>
Public Interface IWPFAddInHostView
' The view returns as a class that directly or indirectly derives from
' FrameworkElement and can subsequently be displayed by the host
' application by embedding it as content or sub-content of a UI that is
' implemented by the host application.
Function GetAddInUI() As FrameworkElement
End Interface
End Namespace
Het Host-Side-adapterpijplijnsegment implementeren
De contractmethode retourneert een INativeHandleContract, maar de hosttoepassing verwacht een FrameworkElement (zoals opgegeven door de hostweergave). Daarom moet de INativeHandleContract worden omgezet in een FrameworkElement na het overschrijden van de isolatiegrens. Dit werk wordt uitgevoerd door de adapter aan de hostzijde door ContractToViewAdapteraan te roepen, zoals wordt weergegeven in de volgende code.
using System.AddIn.Contract;
using System.AddIn.Pipeline;
using System.Windows;
using Contracts;
using HostViews;
namespace HostSideAdapters
{
/// <summary>
/// Adapts the add-in contract to the host's view of the add-in
/// </summary>
[HostAdapter]
public class WPFAddIn_ContractToViewHostSideAdapter : IWPFAddInHostView
{
IWPFAddInContract wpfAddInContract;
ContractHandle wpfAddInContractHandle;
public WPFAddIn_ContractToViewHostSideAdapter(IWPFAddInContract wpfAddInContract)
{
// Adapt the contract (IWPFAddInContract) to the host application's
// view of the contract (IWPFAddInHostView)
this.wpfAddInContract = wpfAddInContract;
// Prevent the reference to the contract from being released while the
// host application uses the add-in
this.wpfAddInContractHandle = new ContractHandle(wpfAddInContract);
}
public FrameworkElement GetAddInUI()
{
// Convert the INativeHandleContract that was passed from the add-in side
// of the isolation boundary to a FrameworkElement
INativeHandleContract inhc = this.wpfAddInContract.GetAddInUI();
FrameworkElement fe = FrameworkElementAdapters.ContractToViewAdapter(inhc);
return fe;
}
}
}
Imports System.AddIn.Contract
Imports System.AddIn.Pipeline
Imports System.Windows
Imports Contracts
Imports HostViews
Namespace HostSideAdapters
''' <summary>
''' Adapts the add-in contract to the host's view of the add-in
''' </summary>
<HostAdapter>
Public Class WPFAddIn_ContractToViewHostSideAdapter
Implements IWPFAddInHostView
Private wpfAddInContract As IWPFAddInContract
Private wpfAddInContractHandle As ContractHandle
Public Sub New(ByVal wpfAddInContract As IWPFAddInContract)
' Adapt the contract (IWPFAddInContract) to the host application's
' view of the contract (IWPFAddInHostView)
Me.wpfAddInContract = wpfAddInContract
' Prevent the reference to the contract from being released while the
' host application uses the add-in
Me.wpfAddInContractHandle = New ContractHandle(wpfAddInContract)
End Sub
Public Function GetAddInUI() As FrameworkElement Implements IWPFAddInHostView.GetAddInUI
' Convert the INativeHandleContract that was passed from the add-in side
' of the isolation boundary to a FrameworkElement
Dim inhc As INativeHandleContract = Me.wpfAddInContract.GetAddInUI()
Dim fe As FrameworkElement = FrameworkElementAdapters.ContractToViewAdapter(inhc)
Return fe
End Function
End Class
End Namespace
De Add-In implementeren
Wanneer de invoegtoepassingsadapter en de weergave zijn gemaakt, moet de invoegtoepassing (WPFAddIn1.AddIn
) de IWPFAddInView.GetAddInUI
-methode implementeren om een FrameworkElement-object (een UserControl in dit voorbeeld) te retourneren. De implementatie van de UserControl, AddInUI
, wordt weergegeven met de volgende code.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFAddIn1.AddInUI">
<StackPanel>
<Button Click="clickMeButton_Click" Content="Click Me!" />
</StackPanel>
</UserControl>
using System.Windows;
using System.Windows.Controls;
namespace WPFAddIn1
{
public partial class AddInUI : UserControl
{
public AddInUI()
{
InitializeComponent();
}
void clickMeButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello from WPFAddIn1");
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Namespace WPFAddIn1
Partial Public Class AddInUI
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub clickMeButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
MessageBox.Show("Hello from WPFAddIn1")
End Sub
End Class
End Namespace
De implementatie van de IWPFAddInView.GetAddInUI
door de invoegtoepassing hoeft slechts een nieuw exemplaar van AddInUI
te retourneren, zoals te zien is in de volgende code.
using System.AddIn;
using System.Windows;
using AddInViews;
namespace WPFAddIn1
{
/// <summary>
/// Add-In implementation
/// </summary>
[AddIn("WPF Add-In 1")]
public class WPFAddIn : IWPFAddInView
{
public FrameworkElement GetAddInUI()
{
// Return add-in UI
return new AddInUI();
}
}
}
Imports System.AddIn
Imports System.Windows
Imports AddInViews
Namespace WPFAddIn1
''' <summary>
''' Add-In implementation
''' </summary>
<AddIn("WPF Add-In 1")>
Public Class WPFAddIn
Implements IWPFAddInView
Public Function GetAddInUI() As FrameworkElement Implements IWPFAddInView.GetAddInUI
' Return add-in UI
Return New AddInUI()
End Function
End Class
End Namespace
De implementatie van de hostapplicatie
Wanneer de hostadapter en de hostweergave zijn gemaakt, kan de hosttoepassing het .NET Framework-invoegtoepassingsmodel gebruiken om de pijplijn te openen, een hostweergave van de invoegtoepassing te verkrijgen en de IWPFAddInHostView.GetAddInUI
methode aan te roepen. Deze stappen worden weergegeven in de volgende code.
// Get add-in pipeline folder (the folder in which this application was launched from)
string appPath = Environment.CurrentDirectory;
// Rebuild visual add-in pipeline
string[] warnings = AddInStore.Rebuild(appPath);
if (warnings.Length > 0)
{
string msg = "Could not rebuild pipeline:";
foreach (string warning in warnings) msg += "\n" + warning;
MessageBox.Show(msg);
return;
}
// Activate add-in with Internet zone security isolation
Collection<AddInToken> addInTokens = AddInStore.FindAddIns(typeof(IWPFAddInHostView), appPath);
AddInToken wpfAddInToken = addInTokens[0];
this.wpfAddInHostView = wpfAddInToken.Activate<IWPFAddInHostView>(AddInSecurityLevel.Internet);
// Get and display add-in UI
FrameworkElement addInUI = this.wpfAddInHostView.GetAddInUI();
this.addInUIHostGrid.Children.Add(addInUI);
' Get add-in pipeline folder (the folder in which this application was launched from)
Dim appPath As String = Environment.CurrentDirectory
' Rebuild visual add-in pipeline
Dim warnings() As String = AddInStore.Rebuild(appPath)
If warnings.Length > 0 Then
Dim msg As String = "Could not rebuild pipeline:"
For Each warning As String In warnings
msg &= vbLf & warning
Next warning
MessageBox.Show(msg)
Return
End If
' Activate add-in with Internet zone security isolation
Dim addInTokens As Collection(Of AddInToken) = AddInStore.FindAddIns(GetType(IWPFAddInHostView), appPath)
Dim wpfAddInToken As AddInToken = addInTokens(0)
Me.wpfAddInHostView = wpfAddInToken.Activate(Of IWPFAddInHostView)(AddInSecurityLevel.Internet)
' Get and display add-in UI
Dim addInUI As FrameworkElement = Me.wpfAddInHostView.GetAddInUI()
Me.addInUIHostGrid.Children.Add(addInUI)
Zie ook
.NET Desktop feedback