Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
173
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

Module 2: Introduction to a Managed Execution Environment

33

 

 

 

5.What .NET component compiles MSIL into CPU specific native code?

The just-in-time (JIT) compiler.

6.What feature of .NET ensures that object memory is freed?

The garbage collection process.

Module 3: Working with

Components

Contents

 

Overview

1

An Introduction to Key .NET Framework

 

Development Technologies

2

Creating a Simple .NET Framework

 

Component

4

Lab 3.1: Creating a .NET Framework

 

Component

11

Creating a Simple Console Client

14

Lab 3.2: Creating a Simple Console-Based

 

Client

19

Demonstration: Creating a Windows

 

Forms Client

22

Creating an ASP.NET Client

27

Lab 3.3: Calling a Component Through

 

an ASP.NET Page

36

Review

40

Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

2001-2002 Microsoft Corporation. All rights reserved.

Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and

Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Module 3: Working with Components

iii

 

 

 

Instructor Notes

Presentation:

70 Minutes

Lab:

60 Minutes

After completing this module, students will be able to:

!Create a simple Microsoft® .NET Framework component in C#.

!Implement structured exception handling.

!Create a simple .NET Framework console application that calls a component.

!Create a .NET Framework client application by using the Windows Forms library.

!Create an ASP.NET page that uses the previously developed .NET Framework component to create an ASP.NET application.

Materials and Preparation

This section provides the materials and preparation tasks that you need to teach this module.

Required Materials

To teach this module, you need the Microsoft PowerPoint® file 2349B_03.ppt.

Preparation Tasks

To prepare for this module, you should:

!Read all of the materials for this module.

!Practice the demonstration.

!Review the animation.

!Complete the lab.

iv

Module 3: Working with Components

Demonstrations

This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes.

Creating a Windows Forms Client

This demonstration shows how to build a simple Windows Forms client in Microsoft Visual Studio® .NET. If time permits, you can show the demonstration by using Microsoft Visual Basic®. The solution code for this demonstration is located in <install folder>\DemoCode\Mod03\C# and <install folder>\DemoCode\Mod03\VB.

In this demonstration, point out how the Windows Form is created in the Main method. Also, point out how the Windows Forms Designer adds code to create and initialize the controls. Consider stepping through the code to explain the code to students.

!To create a Windows Form

1.Open Visual Studio .NET and create a Microsoft Visual C#project from the Windows Application template. Name the project WinForm_Client.

2.Add two buttons and a listbox to the form. Set the following properties.

a.Set the button1 Text property to &Execute.

b.Set the button2 Text property to &Close.

c.Set the Form1 Text property to Client.

!To add references

1.On the Project menu, click Add Reference.

2.In the Add Reference dialog box, click the Browse button.

3.Select the CompCS.dll and CompVB.dll assemblies as references. These assemblies are located in <install folder>\DemoCode\Mod03\C#. Then click the Open button.

4.Click OK.

5.Add references to the assemblies in the form code. Use aliases to avoid name conflicts.

using CSStringComp = CompCS.StringComponent; using VBStringComp = CompVB.StringComponent;

Module 3: Working with Components

v

 

 

 

! To add button event handlers

1.In the form, double-click the Execute button to create a button-click event handler.

2.Add the following code to the event handler to create an instance of the C# component and Visual Basic component, and add their strings to the listbox.

//Local Variables

CSStringComp myCompCS = new CSStringComp(); VBStringComp myCompVB = new VBStringComp(); int stringCount=0;

//Display results from C# Component

for (stringCount=0; stringCount<myCompCS.Count;! stringCount++)

{

listBox1.Items.Add(myCompCS.GetString(stringCount));

}

//Display results from Visual Basic Component for (stringCount=0; stringCount<myCompVB.Count;!

stringCount++)

{

listBox1.Items.Add(myCompVB.GetString(stringCount));

}

3.In the form, double-click the Close button to create a button-click event handler.

4.In the Close button event handler, call the Close method to close the form.

5.Compile and run the application. When you click the Execute button, the list box should be filled with four C# strings and four Visual Basic strings.

Testing the ASP.NET Client

To test the ASP.NET page, the following software must be installed on the test computer:

!Microsoft Internet Information Services (IIS)

!The .NET Framework common language runtime

!The .aspx file that contains the client code

!All compiled components that are required by the client application

You must configure a virtual directory that points to the directory that contains the .aspx file. You can use the New Directory Wizard in the IIS snap-in to do this.

viModule 3: Working with Components

!To configure a virtual directory

1.Click the Start menu, click Control Panel, click Performance and Maintenance, click Administrative Tools, and then click Internet Information Services.

2.Expand the computer icon, expand the Web Sites folder, and then expand and select Default Web Site.

3.On the Action menu, point to New, and then click Virtual Directory. The Virtual Directory Creation Wizard is launched.

4.Click Next to continue.

5.In the Alias text box, type Test and click Next.

6.Browse to the <install folder>\Labs\Lab03.3\SOLUTION\ASP.NET_Client folder that contains the file ClientASP.NET.aspx, click OK, and then click

Next.

7.On the Access Permissions page, accept the default selections, click Next, and then click Finish.

The Test folder is added to the Default Web Site.

Ensure that the compiled component DLLs, CompCS.dll and CompVB.dll, are in a \Bin subdirectory under the starting point for the application virtual directory.

Module 3: Working with Components

vii

 

 

 

! To test the ASP.NET Client

Open Microsoft Internet Explorer, and type the following text in the Address bar:

http://localhost/Test/ClientASP.NET.aspx

When you press ENTER, the following display, or one similar to it, appears.

Multimedia

This section lists the multimedia items that are part of this module. Instructions for launching and playing the multimedia are included with the relevant slides.

ASP.NET Execution Model

This animation will show students how ASP.NET pages are processed on the server.

viii

Module 3: Working with Components

Module Strategy

Use the following strategy to present this module:

!An Introduction to Key .NET Framework Technologies

Briefly introduce Windows Forms, Web Forms, and XML Web services. Explain that you will show an example of a Windows Form in this module. Tell students that they will learn about XML Web services in Module 13, “Remoting and XML Web Services,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# .NET).

!Creating a Simple .NET Framework Component

Show the basic approach to creating reusable classes by using C# and Visual Basic. Spend some time on the new structured exception handling techniques. Use the command line to compile the component.

Break the lecture at this point and instruct students to do Lab 3.1, Creating a

.NET Framework Component.

!Creating a Simple Console Client

Show how to write a simple console application that calls the .NET Framework runtime-compatible component that was created in Creating a Simple .NET Framework Component. Be sure to fully explain how to use a namespace alias to remove ambiguity to type references.

Instruct students to do Lab 3.2, Creating a Simple Console-Based Client.

!Creating an ASP.NET Client

Emphasize the .NET Framework’s capability to execute the component code that was created in Creating a Simple .NET Framework Component in a variety of environments. Contrast the use of the component by the standalone client applications with that of an IIS ASP.NET page.

Be sure to present the ASP.NET Execution Model animation to help students understand how ASP.NET pages are processed on the server. Instructions for running the animation are included in the Instructor Notes in the margins.

The main objective of this section is to show students how to use an ASP.NET page to obtain the string data from the .NET Framework component, to format that data in HTML, and to return this HTML to a Web browser for display.

Be sure to demonstrate how to test the ASP.NET page that was created in Creating an ASP.NET Client in this module. Students must understand how to configure a virtual directory that points to the directory that contains the

.aspx file.

Instruct students to do Lab 3.3, Calling a Component Through an ASP.NET Page.

Соседние файлы в папке c#