Page view counter
Javascript timer in silverlight app
Last post 08-28-2008 2:21 AM by HarshBardhan. 5 replies.
Sort Posts:
08-27-2008 5:49 AM
Javascript timer in silverlight app

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 !

 

Sarah_17

Loading...
Joined on 08-27-2008
Posts 3
08-27-2008 6:08 AM
Re: Javascript timer in silverlight app

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

<Canvas Loaded="Canvas_Loaded" >

<TextBlock x:Name="TimerText" />

</Canvas>

//Page.Xaml.cs copy paste this below page constructor

int initialValue = 0;
private void Canvas_Loaded(object sender, RoutedEventArgs e)

{

CallTimer();

}

public void CallTimer()

{

DispatcherTimer Timer = new DispatcherTimer();

Timer.Interval = new TimeSpan(0, 0, 0, 1);

//Every One sec tick event will be fired

Timer.Tick+=new EventHandler(Timer_Tick); ;

Timer.Start();

}

void Timer_Tick(object sender, EventArgs e)

{

TimerText.Text = (initialValue++).ToString();

}

 

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
08-27-2008 6:27 AM
Re: Re: Javascript timer in silverlight app

thnks for the quick reply...

i was trying out ur code ...do i need too add some namespace for DispatcherTimer ??

which one is it?

 

Sarah_17

Loading...
Joined on 08-27-2008
Posts 3
08-27-2008 6:38 AM
Re: Re: Javascript timer in silverlight app

Hi.

Sorry I missed that to mention .

It is

using System.Windows.Threading;

Thanks.

Mark as answer if it helps..

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
08-28-2008 2:06 AM
Re: Re: Re: Javascript timer in silverlight app

Hi,  

Could U Please let me know how to expose c# method as scriptable.

May I access js in xaml?

Sarah_17

Loading...
Joined on 08-27-2008
Posts 3
08-28-2008 2:21 AM
Marked as Answer
Re: Re: Re: Javascript timer in silverlight app

Hi,

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,

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
Microsoft Communities