Creating a New Silverlight Application using VS 2010 (C#)
|
We'll start our Silverlight application by selecting the New Project File -> New
-> Project -> Visual C# -> Silverlight to create a "Silverlight Application".
|
|
|
We'll name the project "Silverlight1". When we click the "OK" button Visual
Studio will prompt us with an additional dialog that allows us to choose whether
we want to create just a Silverlight Application project, or optionally also add
a server-side ASP.NET Web project to our solution to host the Silverlight
Application within. For this sample we'll choose to add an ASP.NET Web
Application project to the solution as well and name it "Silverlight1.Web". When
we click "ok" Visual Studio will create a solution for us that has both a
Silverlight client application and an ASP.NET web server application in it:
When we do a "build" Visual Studio will automatically copy the compiled
Silverlight application to our web server project (no manual step or
configuration required). The default web server project that VS created for us
contains both an ASP.NET page (Silverligh1TestPage.aspx ) and a static HTML page
(Silverligh1TestPage.html) that we can use to run and test our Silverlight
Application within.
Note: Silverlight Applications can be used with any web-server (including
Apache on Linux) and hosted within static HTML files or any server-side
generated page (including PHP, Java, Python, Ruby, etc). For this sample we
won't be writing any server-side code - we'll instead use the cross-domain
networking feature of Silverlight . I chose to create the ASP.NET web server
project mainly to get automatic deployment and use its built-in web-server for
testing.
By default a newly created Silverlight application project contains a Page.xaml
and App.xaml file, as well as code behind class files (which can be written in
VB, C#, Ruby or Python) that are associated with them:
Page.xaml > Page.xaml.cs
App.xaml > App.xaml.cs
XAML files are XML text files that can be used to declaratively specify the UI
of a Silverlight or WPF application. XAML can also be used more broadly to
declaratively represent .NET objects. The App.xaml file is typically used to
declare resources, such as brush and style objects that are shared across the
application. The Application code-behind class for the App.xaml file can be used
to handle application level events - like Application_Startup, Application_Exit
and Application_UnhandledException. The Page.xaml file is by default the initial
UI control that is loaded when the application activates. Within it we can use
UI controls to define our user interface, and then handle events off of them
within the Page code-behind class (much more on this later).
When we build our project, Visual Studio will by default compile the code and
.XAML markup into a standard .NET assembly file, and then package it and any
static resources (like images or static files we want to include in it) into a ClientBin/Silverlight1.xap
".xap" files (pronounced "zap") use the standard .zip compression algorithm to
minimize client download size.
To host and run a Silverlight 2 application, you can add an < object > tag into
any standard HTML page (no JavaScript required) that points to the .xap file.
Silverlight will then automatically download the .xap file, instantiate it, and
host it within that HTML page in the browser. This works cross browser (Safari,
FireFox, IE, etc) and cross platform (Windows, Mac, and Linux). Test HTML and
ASP.NET pages (containing the < object > tag reference that points to our
Silverlight application) were automatically added for us when we created our
project - which means we can just hit F5 to build, run and test it.
Silverligh1TestPage.html
Silverligh1TestPage.aspx
Learning how to Add Controls and Handle Events Right now our Digg application
doesn't do anything, and when it is run it brings up an empty page. We can
change this by opening up the Page.xaml file in the project and adding some
content to it:
We will add a button. The Select Button control to draw on windows.
Annual event to create controls. Double-click the. Controls directly. To make
VS2010 build events. button1_Click ().
Default method name that will appear. button1_Click the file Page.xaml.cs.
Write to us in order that
the Method is button1.Content = "Pushed";
We can just hit F5 to build, run and test it.
After making the change above we can re-run the application and push the button.
and now its content is updated with a "Pushed" message.
|
|
|
|