다음을 통해 공유


InfoPath 2003 개체 모델을 사용하는 초기화 및 정리 코드

기본적으로 InfoPath 2003과 호환되는 양식 서식 파일 프로젝트용으로 만들어지는 FormCode.cs 또는 FormCode.vb 파일에는 양식의 프로그래밍 논리에 사용할 수 있는 원본 코드가 모두 포함되어 있습니다. 프로젝트의 서식 파일은 다음 예제의 클래스처럼 양식 이벤트용 처리기뿐 아니라 초기화 및 정리 코드를 정의할 수 있는 클래스를 FormCode.cs 또는 FormCode.vb 파일에 생성합니다. FormCode.cs 및 FormCode.vb 파일은 클래스를 이벤트 처리기가 구현되는 유일한 클래스로 식별하는 어셈블리 수준의 System.ComponentModel.DescriptionAttribute 특성을 적용합니다. InfoPathNamespaceAttribute 유형을 통해 구현되는 InfoPathNamespace 특성이 클래스에 적용되어 클래스에 사용된 XML DOM 선택 영역 네임스페이스를 식별합니다. InfoPathNamespace에서 참조된 네임스페이스는 InfoPath 프로젝트 시스템에서 유지 관리합니다.

FormCode 클래스 자체는 양식이 열려 있는 동안 표준 InfoPath 기능 외에 추가로 필요한 모든 구성 요소에 대한 초기화 및 정리 루틴을 수행하는 데 사용되는 _Startup 및 _Shutdown 메서드를 제공합니다.

중요

_Startup 및 _Shutdown 메서드 내에서 InfoPath 개체 모델의 멤버를 호출하지 마십시오. 이 메서드에서는 외부 구성 요소의 멤버만 초기화하고 호출해야 합니다.

using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;

// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]

namespace Template1
{
    // The namespace prefixes defined in this attribute must remain synchronized with
    // those in the form definition file (.xsf).
    [InfoPathNamespace(
        "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-29T22-27-27'")]
    public partial class FormCode
    {
        private XDocument thisXDocument;
        private Application thisApplication;

        public void _Startup(Application app, XDocument doc)
        {
            thisXDocument = doc;
            thisApplication = app;

            // You can add additional initialization code here.
        }

        public void _Shutdown()
        {
        }
    }
}
Imports System
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports Microsoft.VisualBasic

' Office integration attribute. Identifies the startup class for the form. Do not
' modify.
<Assembly: System.ComponentModel.DescriptionAttribute( _
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")>

Namespace Template1
    ' The namespace prefixes defined in this attribute must remain synchronized with
    ' those in the form definition file (.xsf).
    <InfoPathNamespace( _
        "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-29T22-36-40'")> _
    Public Class FormCode

        Private thisXDocument As XDocument
        Private thisApplication As Application

        Public Sub _Startup(app As Application, doc As XDocument)
            thisXDocument = doc
            thisApplication = app

            ' You can add additional initialization code here.
        End Sub

        Public Sub _Shutdown()
        End Sub
    End Class
End Namespace

_Startup 메서드

_Startup 메서드는 추가 구성 요소의 초기화 코드를 쓸 위치를 제공할 뿐 아니라, InfoPath 개체 모델에 있는 XDocumentApplication 클래스의 멤버에 액세스할 때 양식 코드에서 사용할 수 있는 thisXDocument 및 thisApplication 변수를 초기화합니다. 두 변수를 초기화하는 데 필요한 코드는 프로젝트 서식 파일에서 자동으로 생성합니다.

    private XDocument thisXDocument;
    private Application thisApplication;

    public void _Startup(Application app, XDocument doc)
    {
        thisXDocument = doc;
        thisApplication = app;

        // You can add additional initialization code here.
    }
    Private thisXDocument As XDocument
    Private thisApplication As Application

    Public Sub _Startup(app As Application, doc As XDocument)
        thisXDocument = doc
        thisApplication = app

        ' You can add additional initialization code here.
    End Sub

다음 예제에서는 thisXDocument 변수를 사용하여 UIObject 유형의 Alert 메서드에 액세스하는 단추에 대한 간단한 이벤트 처리기를 보여 줍니다.

[InfoPathEventHandler(MatchPath="CTRL1_5", EventType=InfoPathEventType.OnClick)]
public void CTRL1_5_OnClick(DocActionEvent e)
{
    // Write your code here.
    thisXDocument.UI.Alert("Hello!");
}
<InfoPathEventHandler(MatchPath:="CTRL1_5", EventType:=InfoPathEventType.OnClick)> _
Public Sub CTRL1_5_OnClick(ByVal e As DocActionEvent)
    ' Write your code here.
    thisXDocument.UI.Alert("Hello!")
End Sub

이벤트 처리기를 만드는 방법에 대한 자세한 내용은 방법: InfoPath 2003 개체 모델을 사용하여 이벤트 처리기 추가를 참조하십시오.

_ShutDown 메서드

_Shutdown 메서드는 양식을 닫을 때 마지막으로 호출되는 메서드입니다. 양식에 사용된 구성 요소를 정리하거나 끝내는 데 필요한 코드를 이 메서드로 작성할 수 있습니다.

    public void _Shutdown()
    {
    }
    Public Sub _Shutdown()
    End    Sub

초기화 및 정리 코드 예제

다음 예제에서는 _Startup 메서드로 Microsoft SQL Server 데이터베이스에 대한 연결을 초기화하고 _Shutdown 메서드로 연결을 닫는 방법을 보여 줍니다. 이 예제가 제대로 실행되려면 먼저 프로젝트 메뉴에서 참조 추가를 클릭한 후 .NET 탭에서 System.Data.dll 구성 요소를 선택하여 .NET Framework의 System.Data 어셈블리에 대한 참조를 설정해야 합니다. 또한 양식 코드 파일 맨 위에 using System.Data.SqlClient 또는 Imports System.Data.SqlClient 지시문을 추가하여 키 입력을 줄인 것에 유의하십시오.

참고 사항참고 사항

SQL Server 데이터베이스와 연결되는 양식 코드가 있는 InfoPath 양식 사용자의 경우 양식 배포 방법과 정책 정의 방법에 따라 보안 권한이 필요할 수 있습니다. 보안에 대한 자세한 내용은 관리 코드 양식 서식 파일의 보안 모델방법: 관리 코드 양식 서식 파일의 보안 설정 구성을 참조하십시오.

using System;
using System.Data.SqlClient;
using Microsoft.Office.Interop.InfoPath.SemiTrust;

// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")]

namespace Template1
{
    // The namespace prefixes defined in this attribute must remain synchronized with
    // those in the form definition file (.xsf).
    [InfoPathNamespace(
        "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-05T20-56-13'")]
    public partial class Template1
    {
        private XDocument    thisXDocument;
        private Application    thisApplication;
        private SqlConnection sqlConnect;

        public void _Startup(Application app, XDocument doc)
        {
            thisXDocument = doc;
            thisApplication = app;

            // Initialize variable for SQL Server connection.
            sqlConnect= new SqlConnection("server=localhost;Trusted_Connection=yes;database=Northwind");
        }

        public void _Shutdown()
        {
            // Close SQL Server connection at shut down.
            sqlConnect.Close();
        }
    }
}
Imports System
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.InfoPath.SemiTrust
Imports Microsoft.VisualBasic

' Office integration attribute. Identifies the startup class for the form. Do not
' modify.
<Assembly: System.ComponentModel.DescriptionAttribute( _
    "InfoPathStartupClass, Version=1.0, Class=Template1.FormCode")>

Namespace Template1
        ' The namespace prefixes defined in this attribute must remain synchronized with
        ' those in the form definition file (.xsf).
        <InfoPathNamespace( _
            "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-08T18-47-33'")>        _
        Public Class Template1

            Private thisXDocument As XDocument
            Private thisApplication As Application
            Private sqlConnect As SqlConnection

            Public Sub _Startup(app As Application, doc As XDocument)
                thisXDocument = doc
                thisApplication = app

                ' Initialize variable for SQL Server connection.
                sqlConnect = New SqlConnection _("server=localhost;Trusted_Connection=yes;database=Northwind")
            End Sub

        Public Sub _Shutdown()
            ' Close SQL Server connection.
            sqlConnect.Close()
        End Sub
    End Class
End Namespace

참고 항목

작업

방법: InfoPath 2003 개체 모델을 사용하여 이벤트 처리기 추가