This article will describe how you can make AJAX callbacks when using Silverlight. Pretty much what this is, is a concept on how when you handle and event in your XAML it will run some server side code. This example will provide steps you need to accomplish this and assume you are running ASP.NET 2.0:
Step 1) Have your object in your xaml that has and event tied to it. For example in your XAML you may have something like this:
<Image Source="/images/pic1.jpg"
Tag="myPic"
x:Name="myPicXName"
Width="100"
Height="100"
Canvas.Top="20"
Canvas.Left="8"
Opacity=".5"
MouseLeftButtonDown="leftDownClick" />
Step 2) Have a function in one of your javascript files named what we named it in Step 1 for the MouseLeftButtonDown property. In addition, have the function you want to run when the call back is done. For example:
function leftDownClick(sender, args)
{
var objSender = sender;
var objTag = sender["Tag"];
var objName = sender["Name"];
//this function is located on default.aspx
doCallBack(objName);
}
//this is called from default.aspx.cs
function runClientCallBack(message, context)
{
//message is the returnValue variable in default.aspx.cs
//do whatever you want
}
Step 3) Have javascript functions your main ASPX page. For the purpose of this article will say on your default.aspx page. So for example your page may look something like this:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script type='text/javascript' src="js/Silverlight.js"></script>
<script type='text/javascript' src="js/createSilverlightMain.js"></script>
<script type="text/javascript">
//this gets called from createSilverlightMain.js
// in leftDownClick
function doCallBack(name)
{
var message = name;
var context = '';
//this is located in default.aspx.cs
<%=sCallBackFunctionInvocation%>
}
//this is used for ICallbackEventHandler
//in the Page_Load
function OnError(message, context)
{
alert('An unhandled exception has occurred:\n' + message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="SilverlightHost" style="width:650px; height:425px;">
<script type='text/javascript'>
createSilverlight();
</script>
</div>
</form>
</body>
</html>
Step 4) Handle the call back in your default.aspx.cs file:
1 - you need to have default.aspx.cs inherit the following:
System.Web.UI.ICallbackEventHandler
You can do this in the top where it looks something like this:
public partial class _default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
2 - Use the code sample below in your default.aspx.cs file:
protected string returnValue = "default";
public string sCallBackFunctionInvocation;
protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = this.Page.ClientScript.GetCallbackEventReference(this, "message", "runClientCallBack", "context", "OnError", false);
}
public void RaiseCallbackEvent(String eventArgument)
{
//whatever code you want to run
returnValue = //whatever string value you want
//you could even write out another xaml object here in a string
}
public string GetCallbackResult()
{
return returnValue;
}
This concept should get you started. This still doesn't let you access silverlight objects directly in code behind but does give you the ability to run server side code from a silverlight object. you'll notice on the initial leftDownClick button I pass the name property from the silverlight object. This lets me keep track of what object is making the callback.