|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionSilverlight (formerly codenamed Windows Presentation Framework Everywhere) will allow developers to create richer web applications than ever before. We will see a new wave of web based applications that are not only highly usable, but also very visually appealing. Web pages will be able to take advantage of flexible media playback, animation and vector graphic drawing. It will be especially useful on web applications that have a small number of pages, but are used heavily. For example, somebody working in a call center or a broker who is buying and selling stocks all day. This is the reason I chose a stock ticker tape application as my sample. It could be embedded into an existing web page, but provides a better looking ticker than the HTML To make it valuable to users, there needs to be a way to efficiently pull information from corporate servers over the Internet. The latest version of Silverlight supports the CLR, giving access to web services, but there are times where you may not want to use managed code. In this case, you need to use a client side script solution, such as JavaScript. This article will discuss integrating Silverlight (home page) with ASP.NET AJAX (Exposing Web Services to Client Script in ASP.NET AJAX) to create a rich client/server-like application, but through a standard browser. I should mention that there are other ways of create the same visual experience that this sample provides, but I wanted to use a fairly simple example that people have seen before and to focus on the blending of the two technologies. There will be an ASP.NET AJAX UpdatePanel which is continuously getting new stock information while a Silverlight StoryBoard is presenting the information it retrieves. The key benefits of this solution are:
BackgroundOriginally my goal was to write an article about calling web services using managed code called from Silverlight. When I wrote the first version of this article, Silverlight didn't support managed code. Because of this limitation, I decided to write an article about ASP.NET AJAX integration. In the future, I may write an article that discusses using web services from Silverlight as a comparison of the two techniques. PrerequisitesClient Installation
Development or Server Installation
To run under Visual Studio 2005
OverviewThere are three high level concepts that will make it easier to understand the code. Continuous MotionTo give the illusion of continuous motion, the application moves two
Calling Web Services Using ASP.NET AJAXASP.NET AJAX will automatically generate a JavaScript proxy for any web services you register using the Note: The following diagrams are UML Sequence diagrams. It describes different objects and the messages between them. See the Wikipedia article for a quick overview if needed. Using a Queue to Share Data Between Silverlight and ASP.NET AJAXBecause there is no guarantee that we will always get data back in time with AJAX, the application uses a ASP.NET Description of Individual FilesThe following are a list of the files that I have created or modified in this sample. CreateSilverlight.jsThe following is the code that will instantiate the Silverlight plug in. If it has not been installed, it will prompt the user to download and install it. //contains calls to silverlight.js, example below loads TickerTape.xaml
function createSilverlight()
{
Silverlight.createObjectEx({
source: "TickerTape.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "500",
height: "25",
version: "0.95",
background: "#00000000",
isWindowless: false,
enableHtmlAccess: true },
events: {}
});
}
Ticker.aspxThis is the page that the client will see. It uses both Silverlight to represent the stock ticker and ASP.NET AJAX to retrieve the information from the server. There is a textbox, which is used as a queue. The ASP.NET AJAX code will fill the queue with stock data, which is then pulled off by the JavaScript that populates the Silverlight controls. The following ASP.NET AJAX code does a few important tasks. By adding the <!-- AJAX code -->
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="StockUpdate.asmx" />
</Services>
</asp:ScriptManager>
The following code will periodically call the server side code using AJAX enabled assembly which will populate the <asp:UpdatePanel ID="StockPanel" runat="server">
<ContentTemplate>
<asp:Timer ID="RefreshTimer" runat="server" Interval="3000"
OnTick="RefreshTimer_Tick"> </asp:Timer>
<asp:TextBox ID="NewStockQueue" runat="server" Width="500">
</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
TickerTape.xamlThe XAML is run by the Silverlight plug in. It will show the ticker tape as it scrolls around the screen. The following are two <!-- These contain the stock market information that are visible to
the client. -->
<TextBlock x:Name="tickerText1" Canvas.Top="3" FontSize="12"
Foreground="Yellow" FontFamily="Arial Black" Text="" />
<TextBlock x:Name="tickerText2" Canvas.Top="3" FontSize="12"
Foreground="Yellow" FontFamily="Arial Black" Text="" />
A <!-- This storyboard will animate the text boxes below,
moving them to the left until they hit the end.
Then it will restart, giving the appearance of continuously moving
information-->
<Storyboard x:Name="tickerAnimation" Completed="RefreshTicker" >
<DoubleAnimation x:Name="animationText1"
Storyboard.TargetName="tickerText1"
Storyboard.TargetProperty="(Canvas.Left)" BeginTime="0"
Duration="0:0:16" From="1" To="-550" />
<DoubleAnimation x:Name="animationText2"
Storyboard.TargetName="tickerText2"
Storyboard.TargetProperty="(Canvas.Left)" BeginTime="0"
Duration="0:0:16" From="550" To="0" />
</Storyboard>
StockUpdate.asmxThis is a very simple web service, that provides access to the After adding a reference to the // This is the ASP.NET AJAX reference we need
using System.Web.Script.Services;
We also have to add a [WebService(Namespace = "http://randar.name/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class StockUpdate : System.Web.Services.WebService
MarketManager.csThis is a completely fake class that returns random stocks, along with some information such as prices and trends. Web.configThis is the default web.config when you use the "ASP.NET AJAX-Enabled Web Application" template in Visual Studio 2005. ConclusionSilverlight enables the creation of rich, visually stunning, and interactive experiences that can run on multiple browsers. But a pretty application without data is just a screensaver. There needs to be a way to efficiently and quickly pull data from the server and present it. ASP.NET AJAX is one of many ways it can be done, but it is definitely one of the best ways to create a highly usable web application. History
|
||||||||||||||||||||||