none
using findName("myObject") without "sender"

    Question

  • I was wondering if there is a way to use findName without being called like sender.findName("myObject")

    For instance, if I was to make a timer using

    var myTimer = setInterval("myTimerEvent()", 1000),

    and in myTimerEvent I wanted to update a textblock in my XAML with more text:

    function myTimerEvent(){

       x.findName("myTextArea").Text += "more text";

    }

    is there a way I can directly reference the Silverlight object (x), similar to the way you can call _root in Flash?

     

    Thanks!

    Tuesday, September 25, 2007 6:49 PM

Answers

  • SilverlightControlHost is probably the id of the div containing the Silverlight control.  You want the id of the control itself.  You assign this when you create the Silverlight object via createObject or createObjectEx.  Although I agree with Dave that an animation is probably better than setTimeout, and Microsoft recommends that approach as well.

    Wednesday, September 26, 2007 11:48 AM

All replies

  • Sure!  You just need to know the name of the Silverlight object (the ID you give it when you create it).  For example:

    document.getElementById("SilverlightHost").content.findName("myTextArea")

    Tuesday, September 25, 2007 7:50 PM
  • I have the same problem with the timer:

    function startTimer()
    {
            document.getElementById("SilverlightControlHost").content.findName("timer").Text=c.toString();
        c=c+1;
        alert("timer:"+c);
        t=setTimeout("startTimer()",1000)
    }

    function stopCount()
    {
        clearTimeout(t);
        alert("stop timer");
    }


    function state_video(sender,args)
    {
        var mediaStateTextBlock = sender.findName("mediaStateTextBlock");
        var media = sender.findName("Video");
        mediaStateTextBlock.Text=media.CurrentState;
        if(media.CurrentState=="Playing"){
            Durata(sender,args);
            startTimer();
           }
        else if(media.CurrentState=="Stopped"){
           sender.findName("timer").text = "0:00";}
           
    }

    But my Jasvascript debugger  says me:

    document.getElementById("SilverlightControlHost").content has no properties:
    document.getElementById("SilverlightControlHost").content.findName("time...
     
    How can I solve this problem???
     
    thanks 
     

    Wednesday, September 26, 2007 10:34 AM
  • My suggestion would be to NOT use a SetTimeout, but instead use a Silverlight Animation.  You can create an animation that is used purely for timing purposes and in its completed event, you would have the "sender" object to do a findname on.

    Here's an example:

    <Storyboard x:Name="progressAnim" Duration="0:0:0.1"/> //Every Second Timer

    //Add Event Handler 

    this.progressAnim = rootElement.findname("progressAnim");

    this.progressAnim.addEventListener("Completed", Silverlight.createDelegate(this, this.handleProgressCheck));

     

    //Display the current time/location and move the marker     

    handleProgressCheck: function(sender, eventArgs) {

        this.progressAnim.stop(); //Stop Timer 

        // Do something fun

        this.progressAnim.begin();  //Restart Timer 

    }

     

    => Dave

    Wednesday, September 26, 2007 11:09 AM
  • SilverlightControlHost is probably the id of the div containing the Silverlight control.  You want the id of the control itself.  You assign this when you create the Silverlight object via createObject or createObjectEx.  Although I agree with Dave that an animation is probably better than setTimeout, and Microsoft recommends that approach as well.

    Wednesday, September 26, 2007 11:48 AM
  • I might also point out that you don't have to use a 'sender' in response to an event to do findName. If you grab the main canvas in a "Loaded" handler, for instance, you can use that in your code as:

    mainCanvas.findName("MyTextBlock")

    That may be obvious to all involved, but thought I'd throw it out :)

    -Dave

    Wednesday, September 26, 2007 11:50 AM
  • I agree with Dave. In one of my projects, I needed a sound to start at a certain time in my animation. I created a 2nd timeline that would end at the time I wanted to have the sound play. For example: I wanted my sound to start at 3 seconds. So I created a 2nd timeline set to end at 3 seconds and set my audio file to AutoPlay=False (<mediaelement xname="audio" AutoPlay=False...>). Then, in my code I added.

    <xname="timeline2" Completed="BeginAudio"...>

    Where in my js:

    handleLoad: function BeginAudio(sender, eventargs){

    sender.findName("audio").Play();

    }

    Hope this helps!

     Also, please mark which response answers your question.

     

    Wednesday, September 26, 2007 4:01 PM
  • This is all great information and helped me solve my problem... thanks everyone!

    Wednesday, September 26, 2007 4:18 PM
  •  Thanks everyone!!!Smile

    Thursday, September 27, 2007 10:55 AM