Share via


IComponentConnector Interface

Definition

Provides infrastructure support for event wiring and build actions.

public interface class IComponentConnector
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(2906658834, 45201, 20944, 185, 21, 45, 104, 44, 210, 175, 16)]
struct IComponentConnector
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.Guid(2906658834, 45201, 20944, 185, 21, 45, 104, 44, 210, 175, 16)]
public interface IComponentConnector
Public Interface IComponentConnector
Attributes

Examples

The Microsoft Visual Studio generated code for IComponentConnector that parallels the "button1" scenario described above might resemble the following:

partial class MainPage : Windows.UI.Xaml.Controls.Page, IComponentConnector
    {
        [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]

        public void Connect(int connectionId, object target)
        {
            switch(connectionId)
            {
            case 1:
                #line 21 "..\..\MainPage.xaml"
                ((Windows.UI.Xaml.Controls.Primitives.ButtonBase)(target)).Click += this.button1_Click_1;
                 #line default
                 #line hidden
                break;
            }
            this._contentLoaded = true;
        }
    }

Remarks

Unless you are substantially extending the capabilities of a XAML framework or XAML design tools, you probably won't need to generate or implement IComponentConnector. The remainder of the remarks here are intended to orient you to the purpose of IComponentConnector in the Application-based app model and to explain the role of IComponentConnector in the generated code that Microsoft Visual Studio infrastructure creates as part of a typical XAML project.

By default, when you add a XAML page to a UWP app project in Microsoft Visual Studio, its BuildAction is Page. When you build the project, all project items with that build action are processed, and code files that match the programming language choice of the project are generated. The generated code files all contain the string ".g" in their name and can be observed in the obj folder of the project after compilation. The generated files implement one part of the partial class definition that the Application-based app model uses to connect XAML and code aspects of an app definition. The process of generating partial classes from XAML is sometimes referred to as markup compilation.

Every element in XAML that has a XAML name (x:Name attribute or Name attribute applied) or an event handler declared generates a call to IComponentConnector.Connect within the generated code file. The infrastructure code for XAML build actions then defines fields that match the names on the elements. If there's event wiring done in XAML, the build actions attach the event handlers to the XAML-created instances. The fields provide the access point that both app code and infrastructure code can use to reference the object that is created as a result of parsing the XAML.

For example, if there is a XAML element for a Button named "button1" in the XAML file and it has an attribute for the Click event that references a named handler method, Microsoft Visual Studio will autogenerate an implementation of Connect method from the IComponentConnector interface. The connectionId parameter is an identifier token to distinguish calls, and the target parameter is the target to connect events and names to.

Methods

Connect(Int32, Object)

Attaches events and names to XAML-initiated content.

GetBindingConnector(Int32, Object)

Retrieves the IComponentConnector for this connection.

Applies to

See also