Creating a Wizard for Developing
Web Application Using Managed C++
In this section, we’ll look at how to create a custom application wizard that generate a Web application using ASP.NET and managed C++. We’ll look at the details involved in writing a Web Forms applications using ASP.NET and managed C++ in the second half of the book. For now, we’ll create an application wizard that can generate Web Forms applications. A Web Forms application involves several different kinds of files to be generated. In addition, we can add several options such as tracing/debugging options and include several kinds of controls to see how the application wizard works. The files included in the Web Forms application include source code for a Managed C++ DLL, an ASP.NET(ASPX) files, a Web.Config file, and a Visual Studio Solution file. Each of these files will need to contain a couple of different substitutions made by the application wizard.
We’ll create our wizard using the Custom Wizard. As mentioned, the Custom Wizard is a canned Visual Studio.NET wizard that creates a custom wizard. The name of the sample wizard for this chapter will be ManagedCWebForm-Wizard. The wizard will have a user interface consisting of one page in this example to make the sample more digestible.
使用托管C++开发WEB应用程序的向导
在本节,我们将使用ASP.NET和托管C++来产生WEB应用程序的向导.在本书的后半部分我将看到ASP.NET和托管C++写WEB窗体的应用程序的详细资料.现在,我们将生成WEB窗体应用程序的向导.WEB应用程序包括几个特别的性质的文件.另外,我能增加单独选项例如跟踪/调试选项和包括个别性质的控件来查看应用向导是怎么工作的.WEB窗体应用程序包括托管C++ DLL的源代码,一些ASP.NET(ASPX)文件,一个WEB.CONFIG文件和一个VS解决方案文件.这些文件将需要替换包含向导不同的连接.????
我们用自定义向导建立自己的向导.象提到的一样,自定义向导是VS.NET向导建立的自定义向导. 在本章该样例向导的名字叫ManagedCWebForm-Wizard.这个例子非常的简单也很容易消化,它由一个页面组成的用户界面.
The user interface itself will include check boxes for adding controls to the page and for turning on debugging and tracing options. Solution Explorer lets you get to the HTML page representing the wizard user interface. Editing this page is much like editing normal dialog boxes. You can select a control from the Toolbox on the left side of the Visual Studio .NET’s IDE, place the control on the page, and set its properties using the Properties window. The wizard has six check boxes on the interface page. Three of the check boxes will manage the controls on the Web Form—one each for adding a CheckBox control to the Web Form , for adding a Label control, and for adding a TextBox control. You can use the Properties window to provide Ids for each of the controls. The TextBox Check box has an ID of UseTextBox, the Label check box has an ID of UseLabel, and the CheckBox check box an ID of UseCheckBox. When the wizard generates the code, it looks for these symbols to add code to the ASPX page and the code page.
用户界面自己将在页面上增加跟踪调试的选择框控制选项.解决方案让你得到HTML页面的向导用户界面.编辑这个页面就象是编辑一般的对话框一样.你可以从VS.NET的IDE左边的工具栏上选择控件,将控件摆放出在页面上或在属性窗口中设置它的属性.这向导有六个选项框在用户界面上.三个选项框将管理WEB窗体的控件.每增加个勾选框控件到WEB窗体,就增加个LABEL控件和文本控件.你可以在属性窗口中为每个窗口提供一个ID.TextBox勾选框的ID叫UseTextBox,Label框的ID叫UseLabel,CheckBox叫UseCheckBox.当向导产生代码,你能看到这些符号在ASPX中和代码页中出现.
The three other check boxes are for managing debug options: one for page tracing, one fro request tracing, and one to turn on debugging. The check boxes have Ids of UsePageTracing, UseRequestTracing, and UsePageDebugging. As with the user interface page, the wizard will look for these symbols to add the right code to the generated project.
Figure 4-1 shows default.htm, the user interface page, as it will appear in the finished wizard.
Once the controls are on the page, they need to be associated with symbols that the wizard can use to make substitutions. The wizard’s default user interface page(default.htm) has a block of symbol entries. You then modify the symbols for the Web application wizard, as shown here:
这上个勾选框是管理调试选项的,一个页面跟踪,一个请求跟踪,一个启动和调试.这些勾选框的ID分别为UsePageTracing,UseRequestTracing,和UsePageDebugging.同样使用用户界面,向导将寻找这些符号并将正确的代码添加到项目中去.
图4-1显示的default.htm.用户界面,同样将出现在完成向导中.
一旦控件在页面上,它们就需要向导使用替换符号来联合它们.向导的缺省用户界面(default.htm)有整块符号.你可以在WEB应用程序向导上修改这些符号,如下显示:
Notice that each of these symbols is associated with a check box on the wizard user interface page.
The next step is to take the original source code and insert annotations where you want the wizard to add replacement code. Once we have the original boilerplate code, all the original boilerplate source code for the wizard will live under the Templates directory for that wizard. The final ManagedCWeb-Form will need to include three files: the header file containing the C++ class, the ASPX file containing the Web page layout information, and the Web.Config file containing the configuration settings. The boilerplate code for these files will be included in the Template directory for the wizard. Let’s take a look at the boilerplate code the wizard will use to generate the applications. Here’s the code for the C++ header file:
注意在向导用户界面上每个符号都是用CHECK BOX关联的.
下一步就是组织源代码并插入注释到你想在向导中的什么地方替换代码.一旦你准备好了样本文件,所有的样本源代码都将在向导的模板目录之下.完成的ManagedCWeb-Form必须包含三个文件:一个包含C++类的头文件,一个包含WEB规划信息的ASPX文件,一个包含配置信息的配置文件.这些文件也将包含在向导的模板目录.让我门看看向导使用这样本文件来产生的应用程序.这些代码是C++头文件里的:
// ManagedCWebForm.h
#pragma once
using namespace System;
#using
#using
using namespace System;
using namespace System::Web;
using namespace System::Web::UI
using namespace System::Web::UI::WebControls;
using namespace System::Collections;
using namespace System::ComponentModel;
namespace ProgVSNET_ManagedCWebForm
{
public __gc class ManagedCWebPage : pulbic Page
{
public:
Button * m_button;
[!if UseLabel]
Label* m_label;
[!endif]
[!if UseTextBox]
Textbox* m_text;
[!endif]
[!if UseChockBox]
Checkbox* m_check;
[!endif]
ManagedCWebPage()
{
//To do: Construction code here…
}
void SubmitEntry(Object* o ,EventArgs* e)
{
//Called when Submit button pressed
//To do : insert Page Loading code here…
String* Str;
Str= new sting(“Hello “);
Str=str->Concat(str,m_text->get_Text());
Str=str->Concat(str,new string(“you pushed Submit”));
[!if UseLabel]
m_label->set_Text(str);
[!if UseLabel]
}
void Page_Load(Object* o,EventArgs* e)
{
//To do: insert Page Loading code here…
[!if UsePageTracing]
Trace->Write(“Custom”,”Inside Page_Load”);
[!endif]
if(!IsPostBack){
}
}
};
}
When the wizard generates the final code, it looks for the key symbol contained in the square brace to see whether it is in the symbol table. In our example, the expressions are simply Boolean tests. If the check boxes are selected, the controls or debugging features are turned on. Otherwise, they’re turned off, and the specific code will be omitted from the generated source code. The same principle applies to every file that needs to be generated. For example, the wizard will take the following boilerplate code for the ASP.NET page and examine the UseRequestTracing, UseTextBox, UseLabel, and UseCheckBox symbols to figure out what code to include:
当向导完成了代码,你看到关键字都包含在中括号中而不管他是否在符号表中.比如我们的例子,这个表达式是简单的布尔测试.如果选项框被选择,控制和调试特征开启.否则,它们被关闭,产生的代码将忽略这些细节代码.相同道理应用到产生的每个文件.例如,向导将轮流检查UseRequestTracing, UseTextBox, UseLabel, 和UseCheckBox样本代码的ASP.NET页面文件的符号理解包含了什么代码:
ASP.NET Web Form
,/br>
[!If UseTextBox]
[!end]
[!if UseCheckBox]
[!end]
[!if UseLabel]
[!endif]
The last file that needs to be created is the Web.Config file-an XML file that ASP.NET looks for to learn how to configure the Web application. In this case, page-level tracing and page debugging are turned on or off depending on the state of the check boxes, as shown in the following code:
最后建立一个Web.Cofig的文件一个ASP.NET的XML文件,我们考虑和学习怎样配置Web的应用程序.在这个例子里,页跟踪和页调试级别的开关状态由选项框决定,如下列代码显示:
[!if UsePageDebugging]
[!endif]
[!if UsePageTracing]
[!endif]
In addition to the code boilerplate, the wizard also needs to know which files to include when it generates the application. The Templates directory for the wizards includes a file named Templates,inf that includes a list of files to generate when it produces the application. Templates.inf tells the wizard which files to include in the final project. For our example, we’ll add ManagedCWeb-Form.cpp, ManagedCW
EbForm.aspx, and Web.config to this file. This file works the same way as the other files described earlier—the wizard checks for symbols in the symbol table and generates the application based on selections made within the user interface. As the script code generates the project, the script code calls the GetTargetName function to change the name of the core files (ManagedCWebForm.aspx, ManagedCWebForm.cpp, and ManagedWegForm.h) to reflect the name of the project typed in by the developer when he or she runs the wizard. Here’s the GetTargetName method modified to make the file name substitutions.
除了这些样本代码文件,向导还需要知道应用程序产生时的还包括哪些其他的文件.在向导目录下有个模板目录中名为Templates.inf的文件,该文件包含了应用程序在产生是的文件.Templates.inf文件告诉,向导哪些文件包含在最后的项目中.比如我们的例子,我们增加ManagedCWeb-Form.cpp,ManagedCWebForm.aspx,和Web.config到这个文件中.这个文件象早期的其它文件一样工作,向导检查应用程序基于用户在界面上所选择的选项所产生的符号表中的符号.如项目产生的代码一样,代码调用GetTargetName函数来改变内核文件名.在开发者或其它人运行向导时可以通过名字来反应项目类型.这儿的GetTargetName方法来修改文件名.
Function GetTargetName(strName,strProjectName)
{
try
{
var strTarget=strName;
if (strName,substr(0,15)==”ManagedCWebForm”)
{
var strlen=strName.length;
strTarger=strProjectName + strName.substr(15,strlen-15);
}
return strTarget;
}
catch(e)
{
throw e;
}
}
After the wizard generates the files, it creates a project out of those files. The scripts for creating the project are found in the scripts subdirectory for the wizard project. The default scripts generated by the Custom Wizard include a method named AddConfig. Visual Studio.NET includes a project object model that lets you change the project configuration of the generated project. Following is the source code that flips the DLL switch on and generates a managed assembly. (We’ll cover managed code in the last part of the book.)
向导产生这些文件之后,它建立项目以外的文件.脚本在向导项目中创建脚本子目录.缺省的脚本产生的自定义向导包含一个名为AddConfig的方法.VS.NET包括一个项目对象模块来让我们能改变项目的配置.跟踪这些源代码能弹出DLL开关和产生一个托管汇编.(在本书的最后部分将包含托管代码).
Function AddConfig(proj,strProjectName)
{
try
{
var config=proj.Object.Configurations(‘Debug’);
config.IntermediatdDirectory=’Debug’;
config.OutputDirectory=’Debug’;
config.ConfigurationType=typeDynamicLibrary;
var CLTool=config.Tools(VCCLCompilerTool’);
//TODO: Add compiler settings
CLTool.CompileAsManaged=managedAssembly;
Var LinkTool=config.Tools(‘VCLinkerTool’);
//TODO: Add linker settings
config=proj.Object.Configurations(‘Release’);
config.IntermediateDirectory=’Release’;
config.OutputDirectory=’Release’;
var CLTool=config.Tools(‘VCCLCompilerTool’);
//TODO: Add compiler settings
CLTool.CompileAsManaged=managedAssmbly;
Var LinkTool=config.Tools(‘VCLLinkerTool’);
//TODO: Add linker settings
}
catch(e)
{
throw e;
}
}
Once the wizard has been create, you need to let Visual Studio .NET know of its existence. In order for Visual Studio.NET to pick up on the wizard. The wizard needs its own directory under \Program Files\Microsoft Visual Studio.NET\VC7\VCWizards. The user interface files go into the HTML directory underneath the wizard directory, the template files(boilerplate code) go into the Templates directory underneath the wizard directory, the images go in the Images directory under the wizard directory, and the scripts go under the Scripts directory underneath the wizard directory. Both the user interface files and the template files can be localized. The VSDIR file, the VSZ file, and the icon file go under \Program Files\Microsoft Visual Studio.NET\VC7\VCProjects. As mentioned earlier, the VSDIR and VSZ files are generated by the Custom Wizard.
当向导一旦被建立,你应该让VS.NET知道它的存在.使VS.NET熟悉向导的次序.向导必须在\Program Files\Microsoft Visual Studio.NET\VC7\VCWizards的目录下.用户的界面文件在向导目录下HTML目录中.模板文件(样板代码)在向导目录下的模板目录下.图象在向导目录下的图象目录中,脚本则位于向导目录下的脚本目录下.VSDIR文件,VSZ文件,和图标文件位于\Program Files\Microsoft Visual Studio.NET\VC7\VCProjects目录下. 前面提过,VSDIR和VSZ文件是自定义向导产生的.
The application wizard model within Visual Studio.NET is rich and flexible. We only looked at substitutions that use the state of a check box to determine whether to include code. There are many other ways to set up the application wizard to generate any kind of application. In fact, this wizard architecture is also how Visual Studio.NET implements its other wizards—including the ATL Simple Object Wizard, the Generic C++ Class Wizard, and the Add Member Variable Wizard.
VS.NET的应用程序向导模块是丰富和灵活的.我们仅看看替换法使用选项框的状态不管包括的代码.这儿有许多其他方法设置向导来产生应用程序的所有性质.事实上,VS.NET的其它向导的也具有相同的向导架构—包括ATL简单对象向导,通用C++类向导,和新增成员向导.
Each of these wizards can reach into Visual Studio .NET and access the entire Visual Studio object model, which is how the environment seems to understand the classes and other code within your application.
Be sure to check out \Program Files\Microsoft Visual Studio .NET\VC7\VCWizards for more examples --you’ll find all of Visual Studio .NET’s wizards there.
每个这些向导都能延伸到VS.NET和访问全部的VS对象模型,哪些环境象是理解你的类和你应用程序里的其它代码.
确信在\Program Files\Microsoft Visual Studio .NET\VC7\VCWizards 下有许多例子—你将在此找到很多VS.NET向导.
本文地址:http://com.8s8s.com/it/it25134.htm