Condividi tramite


Windows Forms and WPF Applications

Although they are very different technologies, Windows Forms and Windows Presentation Foundation (WPF) share some fundamental characteristics. In particular, they both use window objects to implement the user interface and allow you to specify which code should run when the application starts up. By default, the startup code simply loads and shows the main window, but you can easily modify this to create a Unity container, populate it with the Enterprise Library configuration information, and resolve the objects used by the application.

For example, you may not wish to create all of the windows at startup to minimize startup times or memory usage. You can store a reference to the container and use it to resolve windows and other types on demand.

The following sections contain more details:

  • Windows Forms Applications
  • Windows Presentation Foundation Applications

Windows Forms Applications

This section describes how you can create and populate the Enterprise Library container in Windows Forms applications written in C# or Visual Basic .NET. It describes how you can modify your project in Visual Studio, and how you can achieve the same directly in code.

C# Windows Forms Applications

In a C# Windows Forms project, Visual Studio creates a Program.cs file that contains the application startup code in a routine named Main. You can edit this code to create and populate the container, and then resolve the main form (rather that creating it using the new keyword). This causes the container to populate the dependencies of all of the classes and forms the application uses (providing each can be resolved by the container). The following code shows an example.

static void Main()
{
  var container = new UnityContainer()
      .AddNewExtension<EnterpriseLibraryCoreExtension>();
  Application.Run(container.Resolve<Form1>());
}

Alternatively, you can create a Main method in your startup form containing the code shown above and specify this as the startup method in the properties for the project.

Visual Basic .NET Windows Forms Applications

Visual Basic .NET creates a class called MyApplication that you can use to execute code that creates and populates the Enterprise Library container. This class is not visible by default in Visual Studio Solution Explorer. To show the class, double-click My Project in Solution Explorer to open the properties for the project, and then click View Application Events.

You can add to this class a handler for the Startup event that creates and populates the container and then sets it as the main form of the application, as shown here. Notice that the form is resolved through the container to populate any dependencies in constructors or declared with attributes in all of the classes and forms the application uses.

'Usage
Partial Friend Class MyApplication

  Private Sub MyApplication_Startup(ByVal sender As Object, _
              ByVal e As _ Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    Dim container As IUnityContainer = New UnityContainer() _
        .AddNewExtension(Of EnterpriseLibraryCoreExtension)()
    Dim theForm As Form = container.Resolve(Of Form1)()
    MyApplication.ApplicationContext.MainForm = theForm
  End Sub

End Class

Alternative Approach for Windows Forms

While this is the recommended approach, you could alternatively include code in the Load event handler of your startup form that creates and populates the container, and then resolves the other forms and classes the application uses by calling the methods of the container or the service locator. However, this alternative approach will not resolve and populate dependencies in the main startup form unless you build it up through the container. For information about using the BuildUp method of the default Unity container, see Using BuildUp to Wire Up Objects Not Created by the Container.

Windows Presentation Foundation Applications

WPF applications use startup code to load and show the initial (main) window. The techniques you can use to create and initialize the container are very similar to those for Visual Basic Windows Forms applications discussed above. In addition, unlike Windows Forms applications, the approach you use for WPF applications written using C# and Visual Basic is identical.

When you create a WPF project, Visual Studio creates an application definition file. In C#, this is named App.xaml and has an associated code file named App.xaml.cs. In Visual Basic .NET, the startup file is named Application.xaml and has an associated code file named Application.xaml.vb. You can create a handler in the startup file for the application Startup event and use it to create and initialize the container and then populate any dependencies in your WPF windows and custom classes.

Double-click App.xaml or Application.xaml in Solution Explorer to open the designer and XML code editor windows. In the XML window, delete the StartupUri attribute of the main window definition and add a Startup attribute with the name of the handler you want to execute when the application starts. For example, change the XML to specify that WPF should execute a handler named CreateContainer, as shown here.

<Application x:Class="Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Startup="CreateContainer">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Next, right-click the Startup attribute in the XML editor window and click Navigate to Event Handler. This opens the App.xaml.cs or Application.xaml.vb code file. Add an event handler to the App class that creates and populates the container, resolves the startup window class for your application to populate all of the dependencies, and then calls its Show method. The following provides an example.

private void CreateContainer(object sender, StartupEventArgs e)
{
  var container = new UnityContainer()
      .AddNewExtension<EnterpriseLibraryCoreExtension>();
  Window1 theWindow = container.Resolve<Window1>();
  theWindow.Show();
}
'Usage
Private Sub CreateContainer(ByVal sender As System.Object, ByVal e As _ System.Windows.StartupEventArgs) 
  Dim container As IUnityContainer = New UnityContainer() _
      .AddNewExtension(Of EnterpriseLibraryCoreExtension)()
  Dim theWindow As Window1 = container.Resolve(Of Window1)()
  theWindow.Show()
End Sub

Alternative Approach for WPF

While this is the recommended approach, you could alternatively include code in the constructor of your startup window that creates and populates the container, and then resolves the other windows and classes the application uses. However, this alternative approach will not resolve and populate dependencies in the main startup window unless you build it up through the container. For information about using the BuildUp method of the default Unity container, see Using BuildUp to Wire Up Objects Not Created by the Container.