Timer for Silverlight 1.1
August 13, 2007 at 10:27 am | In Silverlight | 3 CommentsI needed a timer control for a Silverlight 1.1 application but it is not supported in the Alpha release. My solution was to use the timer in Javascript and have it send events to the C# code.
Event sending from Javascript to C# is described in the Silverlight QuickStart document.
Here is the initialization of the timer in Javascript:
//initialize silverlight event hookup and start timer var control; function init(sender){ control = sender; control.Content.basic.AgEvents = agEventHandler; setInterval(jtimer,10); }
In this routine, the timer is set to execute the jtimer function every 10 ms. (The agEvents statement is used for sending information to the Javascript. I will describe this later.)
The init function is called by the CreateSilverlight function in the page.js file:
// JScript source code //contains calls to silverlight.js, example below loads Page.xaml function createSilverlight() { Silverlight.createObjectEx({ source: "Page.xaml", parentElement: document.getElementById("SilverlightControlHost"), id: "SilverlightControl", properties: { width: "100%", height: "100%", version: "1.1", enableHtmlAccess: "true", isWindowless: "True" }, events: { onLoad: init } }); // Give the keyboard focus to the Silverlight control by default document.body.onload = function() { var silverlightControl = document.getElementById('SilverlightControl'); if (silverlightControl) silverlightControl.focus(); } }
Here is the jtimer function:
//Send position of player to silverlight based on a timer function jtimer(){ control.Content.basic.timer(playerGetPosition()); }
In this function, a function timer is called in the C# code with a value that is passed as an argument.
On the C# side, the timer function looks like this:
//This receives the timer event from Javascript [Scriptable] public void timer(double arg) { tnow = arg;
…
Note the ‘[Scriptable]‘ tag which makes the timer function usable from Javascript.
Finally, the page load event needs some boilerplate to hook the Javascript to C# communications:
public void Page_Loaded(object o, EventArgs e) { // Required to initialize variables InitializeComponent(); //Register the scriptable endpoints WebApplication.Current.RegisterScriptableObject("basic", this);
Testing the Visual Studio Plug-in
August 12, 2007 at 7:33 pm | In Visual Studio | Leave a CommentHere is a code snippet from Visual Studio using the plug-in for Windows Live Writer.
private void button1_Click(object sender, EventArgs e) { string[] names = { "Scott", "George", "Mike" }; Database1DataSet.Table1Row row; foreach (string name in names) { row = database1DataSet.Table1.NewTable1Row(); row["name"] = name; database1DataSet.Table1.AddTable1Row(row); }
table1TableAdapter.Update(database1DataSet.Table1);
database1DataSet.Table1.AcceptChanges();
}
public void Page_Loaded(object o, EventArgs e) { // Required to initialize variables InitializeComponent(); //Register the scriptable endpoints WebApplication.Current.RegisterScriptableObject("basic", this);
It’s a start
August 12, 2007 at 7:28 pm | In Uncategorized | Leave a CommentOK. I am all set now. I have my blog and have just installed Windows Live Writer. Now all I need is an idea!
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.