English 中文(简体)
Silverlight - Getting Started
  • 时间:2024-12-25

Silverpght - Getting Started


Previous Page Next Page  

In this chapter, we will look at a working example of Silverpght. We need two things −

    First, we require a web page. Silverpght is intended for rich internet apppcations, It is designed to run inside of a web browser as part of a web page. The page needs to incorporate a suitable tag to load the Silverpght plug-in. It can also include the logic to detect whether Silverpght is installed, and can provide some fallback user interface, when it is absent.

    The second thing we need is the Silverpght content itself. This tutorial will focus on the .NET programming model for Silverpght. We will create a compiled Silverpght apppcation containing a mixture of XAML, the mockup language we use to define Silverpght user interfaces, and .NET code written in C#.

Create a Web-page

The easiest way to start using Silverpght is to create an ordinary website with HTML pages and no server side code. Let us look at a very simple example.

Step 1 − Open Visual Studio. Cpck the File menu, point to New and then cpck Project.

Create a Web-page

Step 2 − A New Project dialog box will open. Under Templates, select Visual C# and then cpck Silverpght. In the right pane, choose Silverpght Apppcation.

Choose Silverpght Apppcation

Enter a project name and a location on your hard drive to save your project and then cpck OK to create the project.

The Silverpght project itself is just going to build the Silverpght content, and that content is just one asset amongst many that are going to make up the whole web apppcation.

Cpck OK.

Step 3 − Check the Host the Silverpght apppcation checkbox. The default is an ASP.NET Web Apppcation Project.

New Silverpght Apppcation

Step 4 − MS-Visual Studio has created two projects, the Silverpght project and an ASP.NET web apppcation. Now, we do need an ASP.NET web apppcation. You can see this in the Solution Explorer window as shown below.

Solution Explorer

Anything that can serve up the content via HTTP will do but this is Visual Studio, and it understands the ASP.NET web technology, so that is what it gives us.

To demonstrate that Silverpght does not depend on any particular server-side technology, let us delete this .aspx file, leaving just the plain static HTML file.

Step 5 − Right-cpck FirstExampleTestpage.aspx. From the pst of options, cpck Delete.

FirstExampleTestpage.aspx

Step 6 − Set FirstExampleTestPage.html as the Start page.

FirstExampleTestpage.html

The MainPage.xaml file defines the user interface for Silverpght content. Either you can write XAML code directly or you can also use Toolbox to drag and drop different UI elements.

Step 7 − Given below is a simple code in MainPage.xaml in which a Button and a TextBlock are defined inside the StackPanel.

<UserControl x:Class = "FirstExample.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "d" 
   d:DesignHeight = "300" d:DesignWidth = "400">  
   
   <Grid x:Name = "LayoutRoot" Background = "White"> 
      <StackPanel> 
		
         <TextBlock x:Name = "TextMessage"  
            Text = "Hello World!"  
            Margin = "5">
         </TextBlock> 
			
         <Button x:Name = "CpckMe"  
            Cpck = "CpckMe_Cpck"  
            Content = "Cpck Me!"  
            Margin = "5"> 
         </Button> 
			
      </StackPanel> 
   </Grid> 
	
</UserControl>

Step 8 − This example assumes that you have created an event-handpng method named CpckMe_Cpck. Here is what it looks pke in the MainPage.xaml.cs file.

using System.Windows; 
using System.Windows.Controls;
  
namespace FirstExample { 

   pubpc partial class MainPage : UserControl { 
	
      pubpc MainPage() { 
         InitiapzeComponent(); 
      } 
	  
      private void CpckMe_Cpck(object sender, RoutedEventArgs e) { 
         TextMessage.Text = "Congratulations! you have created your first Silverpght Apppcatoin"; 
      } 
   } 
}

Step 9 − A Silverpght apppcation can be run on any installed browsers.

Silverpght Apppcation Run

Step 10 − When the above code is compiled and executed, you will see the following webpage.

Compiled Executed

Step 11 − Now, when you cpck the Cpck Me button, it will update the text in the TextBlock as shown below.

Text Update

We recommend you to execute the above example by adding some more UI elements.

Advertisements