Share via


방법: UI를 반환하는 추가 기능 만들기

업데이트: 2007년 11월

이 예제에서는 WPF(Windows Presentation Foundation)UI(사용자 인터페이스)를 호스트 WPF 독립 실행형 응용 프로그램에 반환하는 추가 기능을 만드는 방법을 보여 줍니다.

추가 기능에서는 WPF 사용자 컨트롤인 UI를 반환합니다. 이 사용자 컨트롤의 콘텐츠는 클릭했을 때 메시지 상자를 표시하는 하나의 단추입니다. WPF 독립 실행형 응용 프로그램에서는 이 추가 기능을 호스팅하고 추가 기능에서 반환되는 사용자 컨트롤을 주 응용 프로그램 창 콘텐츠로 표시합니다.

사전 요구 사항

이 예제에서는 이 시나리오를 지원하는 .NET Framework 추가 기능 모델에 대한 WPF 확장을 중점적으로 다루며 다음과 같은 사항이 필요합니다.

샘플

이 항목의 전체 샘플을 보려면 UI 샘플을 반환하는 추가 기능을 참조하십시오.

예제

WPFUI를 반환하는 추가 기능을 만들려면 파이프라인 세그먼트, 추가 기능 및 호스트 응용 프로그램 각각에 대한 특정 코드가 필요합니다.

계약 파이프라인 세그먼트 구현

계약에서는 UI를 반환하고 반환 값이 INativeHandleContract 형식인 메서드를 정의해야 합니다. 다음 코드에서는 IWPFAddInContract 계약의 GetAddInUI 메서드를 통해 이를 보여 줍니다.

using System.AddIn.Contract; // IContract, INativeHandleContract
using System.AddIn.Pipeline; // AddInContractAttribute

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();
    }
}

추가 기능 뷰 파이프라인 세그먼트 구현

추가 기능은 FrameworkElement의 서브클래스로 제공되는 UI를 구현하므로 IWPFAddInView.GetAddInUI와 연결되는 추가 기능 뷰의 메서드는 FrameworkElement 형식의 값을 반환해야 합니다. 다음 코드에서는 인터페이스로 구현된 계약의 추가 기능 뷰를 보여 줍니다.

using System.AddIn.Pipeline; // AddInBaseAttribute
using System.Windows; // FrameworkElement

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();
    }
}

추가 기능측 어댑터 파이프라인 세그먼트 구현

계약 메서드는 INativeHandleContract를 반환하지만 추가 기능은 추가 기능 뷰로 지정된 FrameworkElement를 반환합니다. 따라서 격리 경계를 통과하기 전에 FrameworkElementINativeHandleContract로 변환해야 합니다. 다음 코드와 같이 이 작업은 ViewToContractAdapter를 호출하여 추가 기능측 어댑터에서 수행됩니다.

using System.AddIn.Contract; // INativeHandleContract
using System.AddIn.Pipeline; // AddInAdapterAttribute, FrameworkElementAdapters, ContractBase
using System.Windows; // FrameworkElement

using AddInViews; // IWPFAddInView
using Contracts; // IWPFAddInContract

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;
        }
    }
}

호스트 뷰 파이프라인 세그먼트 구현

호스트 응용 프로그램은 FrameworkElement를 표시하므로 IWPFAddInHostView.GetAddInUI를 연결하는 호스트 뷰의 메서드는 FrameworkElement 형식의 값을 반환해야 합니다. 다음 코드에서는 인터페이스로 구현된 계약의 호스트 뷰를 보여 줍니다.

using System.Windows; // FrameworkElement

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();
    }
}

호스트측 어댑터 파이프라인 세그먼트 구현

계약 메서드는 INativeHandleContract를 반환하지만 호스트 응용 프로그램에는 호스트 뷰로 지정된 FrameworkElement가 필요합니다. 따라서 격리 경계를 통과한 후에 INativeHandleContractFrameworkElement로 변환해야 합니다. 다음 코드와 같이 이 작업은 ContractToViewAdapter를 호출하여 호스트측 어댑터에서 수행됩니다.

using System.AddIn.Contract; // INativeHandleContract
using System.AddIn.Pipeline; // HostAdapterAttribute, FrameworkElementAdapters, ContractHandle
using System.Windows; // FrameworkElement

using Contracts; // IWPFAddInContract
using HostViews; // IWPFAddInHostView

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;
        }
    }
}

추가 기능 구현

추가 기능측 어댑터와 추가 기능 뷰가 만들어지면 추가 기능(WPFAddIn1.AddIn)이 FrameworkElement 개체(이 예제의 경우 UserControl)를 반환하는 IWPFAddInView.GetAddInUI 메서드를 구현해야 합니다. UserControl의 구현인 AddInUI는 다음 코드에서 확인할 수 있습니다.

    <UserControl
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WPFAddIn1.AddInUI">

    <StackPanel>
        <Button Click="clickMeButton_Click" Content="Click Me!" />
    </StackPanel>

</UserControl>
using System.Windows; // MessageBox, RoutedEventArgs
using System.Windows.Controls; // UserControl

namespace WPFAddIn1
{
    public partial class AddInUI : UserControl
    {
        public AddInUI()
        {
            InitializeComponent();
        }

        void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello from WPFAddIn1");
        }
    }
}

다음 코드에서 볼 수 있듯이 추가 기능을 통한 IWPFAddInView.GetAddInUI의 구현은 AddInUI의 새 인스턴스를 반환하기만 하면 됩니다.

using System.AddIn; // AddInAttribute
using System.Windows; // FrameworkElement

using AddInViews; // IWPFAddInView

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();
        }
    }
}

호스트 응용 프로그램 구현

호스트측 어댑터와 호스트 뷰가 만들어지면 호스트 응용 프로그램은 .NET Framework 추가 기능 모델을 사용하여 파이프라인을 열고 추가 기능의 호스트 뷰를 가져오고 IWPFAddInHostView.GetAddInUI 메서드를 호출할 수 있습니다. 이러한 단계는 다음 코드에서 볼 수 있습니다.

// 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);

참고 항목

작업

UI 샘플을 반환하는 추가 기능

개념

추가 기능 개요

Windows Presentation Foundation 추가 기능 개요