For Loop and SkiaSharp

Yusuf 681 Reputation points
2022-04-11T11:40:35.067+00:00

Hi,
How can I use for in SkiaSharp,
Let's say I want to show every word for 10 seconds (600 frames).

It sounds simple, but I haven't figured it out today.

for code

string[] numbers = { "one", "two", "three", "four",  "five"};
foreach (string i in numbers)
{
    text = i;
}

SkiaSharp code

using SkiaSharp;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyApp1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        string text = "";

        public Page1()
        {
            InitializeComponent();

             Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
            {
                canvasView.InvalidateSurface();
                return true;
            });

         }

        private void canvas_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs e)
        {

            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;

            int width = e.Info.Width;
            int height = e.Info.Height;

            canvas.Clear();  

            SKPaint textPaint = new SKPaint();
            textPaint.Color = SKColors.MidnightBlue;
            textPaint.TextSize = 30;
            canvas.DrawText(text, width / 2, height / 2, textPaint);
        }
    }
}

Thank you in advance.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,216 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,196 Reputation points
    2022-04-11T16:58:51.643+00:00

    use the timer:

    Action doLoop<int> = (count) =>
    {            
       if (count >= numbers.Length)
            return;
       text = numbers[count];
       Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
       {
              canvasView.InvalidateSurface();
              doLoop(--count); 
             return true;
       });
    }
    doLoop();
    

  2. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2022-04-19T09:25:28.673+00:00

    Hello,

    For this problem, you can use nested function to achieve this .

    I used a Label to simulate this functionality and loop through the array.

    You can refer to my code:

    public partial class MainPage : ContentPage  
    {  
        string[] numbers = { "one", "two", "three", "four", "five" };  
      
        string text = "one";  
      
      
        public MainPage()  
        {  
            InitializeComponent();  
        }  
      
        private void Button_Clicked(object sender, EventArgs e)  
        {  
            MyLoop(0);  
        }  
      
      
        public void MyLoop(int count)   
        {  
            if (count >= numbers.Length) {  
                count = 0;  
            }  
      
            text = numbers[count];  
      
            Device.StartTimer(TimeSpan.FromMilliseconds(10000), () =>  
            {  
      
                mLabel.Text = text;  
      
                MyLoop(++count);  
                return false;  
            });  
        }  
    }  
    

    Note:

    When the last element of the data is traversed, the first element is traversed again.

     if (count >= numbers.Length) {  
                    count = 0;  
                }  
    

    Best Regards,
    Jessie Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.