Forum Xamarin.Forms

How to add Function JavaScript in Xamarin Forms

Hello everyone!

I need to add javascript function to xamarin forms.

Use this example in HTML, but I need to implement this example or something similar in Xamarin. Someone knows how I could do it.





image

<script type="text/javascript">

    function bulb()
    {
        var b = document.getElementById("imm");

        if (b.src.match("Images/img1.jpg")) {
            b.src = "Images/img2.jpg";
        }
        else if (b.src.match("Images/img2.jpg")) {
            b.src = "Images/img3.jpg";
        }
        else if (b.src.match("Images/img3.jpg")) {
            b.src = "Images/img1.jpg";
        }
    }       

    setInterval(bulb, 3000);

</script>


Best Answer

  • ArnoldUlateArnoldUlate US ✭✭
    Accepted Answer

    Thanks @ClintStLaurent

    With this code come to the solution.

     int counter = 1;
            Device.StartTimer(TimeSpan.FromSeconds(3), () =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    lblPrueba.Text = counter.ToString();
                });
    
                if (counter == 3)
                {
                    counter = 0;
                }
    
                return counter++ < 3;
            });
    

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @ArnoldUlate
    Rewrite it in C#. Its a simple method.

    Or are you asking someone to translate it from java to C# for you?

  • ArnoldUlateArnoldUlate USMember ✭✭

    @ClintStLaurent
    I need to show images from time to time, as a type of banner, but information, I have not found the way to make them in xamarin forms, I did it in Javascript because it is easier to handle, as an example. But can I do something similar in xamarin?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2017

    @ArnoldUlate
    I get that. You want an image rotator of sorts. Ever 300 seconds you want to get the next image.
    You can useDevice.Timer as documented on many Xamarin tutorials, and call your method that selects an image.

  • ArnoldUlateArnoldUlate USMember ✭✭

    Thanks @ClintStLaurent

    But where can I enter the statement to run while the screen is open?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @ArnoldUlate
    I'd probably do it in the ViewModel.
    Maybe use a bool property. When the page opens it sets the bool to true, when it disappears it sets the property to false.

  • ArnoldUlateArnoldUlate USMember ✭✭
    Accepted Answer

    Thanks @ClintStLaurent

    With this code come to the solution.

     int counter = 1;
            Device.StartTimer(TimeSpan.FromSeconds(3), () =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    lblPrueba.Text = counter.ToString();
                });
    
                if (counter == 3)
                {
                    counter = 0;
                }
    
                return counter++ < 3;
            });
    
Sign In or Register to comment.