Skip to main content
Home Forums Silverlight Programming Programming with JavaScript Javascript timer in silverlight app
5 replies. Latest Post by HarshBardhan on August 28, 2008.
(0)
Sarah_17
Member
0 points
3 Posts
08-27-2008 5:49 AM |
Hi all ...
I am working on silverlight2.0, vs 2008 application. In it i have to inlcude a javascript timer. But im a bit confused as to where should i be putting the js code and where would i call it ? How can i display the timer on the page along with the silverlight controls ??
Plz help me out !
HarshBar...
Star
9908 points
1,719 Posts
08-27-2008 6:08 AM |
Hi.
You can write jave script in your Html page or Aspx Page.
You Can expose some c# method as scriptable which you can call from Javascript method as well as you can call javascript method from code behind also.You can create a timer using c# also.
I have created 1.My code is-
I have created a timer .
You can check this..//Page.Xaml
//Page.Xaml.cs copy paste this below page constructor
{
CallTimer();
}
Timer.Interval =
Timer.Start();
TimerText.Text = (initialValue++).ToString();
08-27-2008 6:27 AM |
thnks for the quick reply...
i was trying out ur code ...do i need too add some namespace for DispatcherTimer ??
which one is it?
08-27-2008 6:38 AM |
Sorry I missed that to mention .
using
Thanks.
Mark as answer if it helps..
08-28-2008 2:06 AM |
Hi,
Could U Please let me know how to expose c# method as scriptable.
May I access js in xaml?
08-28-2008 2:21 AM |
Create a Class Like this...
Register this Script in Your Page class(Page.Xaml.cs) Like this (in page_loaded event) like this
MyScript script = new MyScript(); HtmlPage.RegisterScriptableObject("myScript", script);
public class MyScript { [ScriptableMember()] public string MyName() { String str="Harsh"; return str.ToUpper(); }
[ScriptableMember()] public string Name { get; set; } } }
Now You Can call this from your Javascript.
<script type="text/javascript">var ctrl = null; function pluginLoaded(sender){ ctrl = sender.get_element(); alert(ctrl.Content.myScript. MyName());}
function Button1_onclick() { ctrl.Content.myScript.Name = navigator.appName; alert(ctrl.Content.myScript.Name);} </script>
Add A html button and modify your Xaml control in Default.aspx.cs like this..
<asp:Silverlight ID="Xaml1" runat="server" OnPluginLoaded="pluginLoaded" Source="~/ClientBin/Call_ManagedCode_frm_Javascript.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
<input id="Button1" type="button" value="Test" onclick="return Button1_onclick()" />
Thanks,