Create a “Hello World” Application with GOA WinForms for Silverlight
July 6th, 2007Introduction
This QuickStart Topic describes how to create a basic "Hello World" project using GOA WinForms for Silverlight.
Prerequisites: Silverlight (available from the Silverlight website):
- Microsoft Silverlight 2.0 Beta 2
- Microsoft Visual Studio 2008
- Microsoft Silverlight Tools Beta 2 for Visual Studio 2008
GOA WinForms for Silverlight (available from the NETiKA Technologies site)
- GOA WinForms for Silverlight or GOA WinForms Professional for Silverlight
Creating a GOA Project Using Visual Studio
- Start Visual Studio 2008.
- On the File menu, click New Project.
- In the Project types tree view on the left of the New Project dialog box, select the Visual C# root node.
- In the Templates list on the right, select GOA WinForms Application.
- Name your application "HelloWorld" and click OK.
A new Sliverlight Visual Studio project is created.
Creating an ASP .NET Web Application Using Visual Studio (This step is optional)
- In the Solution Explorer, right click the Solution node, in the drop down menu, select the Add New Project item.
- In the Project types tree view on the left of the New Project dialog box, select the Visual C# root node.
- In the Templates list on the right, select ASP .NET Web Application.
- Name your application "HelloWorldWeb" and click OK.
Link the "HelloWorld" project and the "HelloWorldWeb" project (This step is optional)
- In Solution Explorer, right-click the "HelloWorldWeb" project node you just created, and select Add Silverlight Link from the context menu.
- When prompted to select a project, choose the "HelloWorld" project, and then click OK.
- When prompted whether you want to enable Silverlight debugging for this project, click Yes.
- Right-click the "HelloWorldWeb" ASP.NET project node again, and click Set as Startup Project.
- Delete the Default.aspx page from the ASP.NET project.
Compile and run the HelloWorldWeb application (This step is optional)
- Make sure the "HelloWorldWeb" project is the Startup project.
- In the Solution Explorer, right click the TestPage.html file and, in the drop down menu, select the Set As Start Page item.
- On the Debug menu, click Start Debugging.
- When you are prompted to modify the Web.config file to enable debugging, click OK.
- The application starts. A form holding a button is displayed.
Explore the "Hello World" project
The Hello World project is mainly made of standard Silverlight 2 project files. If you need an introduction on the files that are used by a basic Silverlight project, please read the "How to: Create a Silverlight Project" QuickStart.
Explore the references
- In the Solution Explorer expand the References node located in the "HelloWorld" project.
Among the assemblies referenced by your project, you can see a reference to the Goa.Windows.Forms assembly. The Goa.Windows.Forms assembly holds the System.Windows.Forms namespace that we are using in this sample. It also provides all the GOA basic features and it includes a large set of controls.
More advanced controls are provided in the GOAGrid and GOAToolkit assemblies.
Explore the MyForm.cs file
- In the Solution Explorer double click the MyForm.cs file.
The content of the file is displayed in the Visual Studio Code Editor. In this file, you can watch the code of an "almost standard" .NET Windows Form holding a button. As in standard .NET Windows Forms applications, a static Main method has been created:
GOA will use this method as an entry point in order to start MyForm when requested.
Note that this method must be public.
Explore the Page.xaml file
- In the Solution Explorer double click the Page.xaml file.
- The content of the file is displayed in the Visual Studio Code Editor.
The Page.xaml file holds the description of the canvas that Silverlight displays when the application starts. It also uses some GOA feature in order to display MyForm WinForm inside the root canvas:
- The following attribute as been added to the root tag:
This attribute is used to map an xmlns to reference the Goa.Windows.Forms assembly.xmlns:goa="clr-namespace:NETiKA.Silverlight;assembly=ClientBin/Goa.Windows.Forms.dll" - The following element has been added inside the parent canvas of Page.xaml:
The WinFormHost element is used to display the forms created using the System.Windows.Forms namespace. The main attribute of this element is "FormClass". You can use this attribute to tell which form must be displayed in the WinFormHost element. The value you set in the FormClass must have the following format: "NameSpaceOfTheForm.ClassNameOfTheForm". Note: The form that will be displayed in the WinFormHost element must provide an entry point using a public static Main method.<winformhost Width ="640" Height ="480" Canvas.Left="0" Canvas.Top="0" FormClass="HelloWorld.MyForm" />
Add a button and a label to the MyForm form
Now that we understand the basics of GOA WinForms for Silverlight, we can write some code in the MyForm class to create our Hello World application.
Note that we are coding our project in a standard way as if we were creating a .NET Windows Forms desktop application.
We are adding a label at the top of the form and moving the button at the bottom. When the user will click the button, the label will display the "Hello World" text.
- First, let's add a label to the form and move the button at the bottom of the form. The code that follows is very close to any code generated by the Visual Studio Windows Forms Designer:
public class MyForm : System.Windows.Forms.Form { private Button button1; private Label label1; private System.ComponentModel.Container components = null; public MyForm() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); this.button1.Location = new System.Drawing.Point(50, 50); this.button1.Name = "button1"; this.button1.Text = "Click Me"; this.label1.Location = new System.Drawing.Point(50, 30); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(200, 44); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.ResumeLayout(false); } public static void Main() { Application.Run(new MyForm()); } }
- We also need to add an event handler to manage the button click event.Add the following line in the InitializeComponent method of the form:
And, of course, we also have to add the method that will set the text of the label:void button1_Click(object sender, EventArgs e) { this.label1.Text = "Hello World"; }
Congratulations, you can start your first GOA WinForms application.