Xamarin.Forms:使用SkiaSharp更新绘制的文本值

我使用SkiaSharp库在给定坐标处绘制了一个文本 . 该文本以“hh:mm”格式表示计时器计数值 . 如何根据计时器计数更新文本值 .

我的代码:

Timer.cs

public class Timer
    {
        private readonly TimeSpan _timeSpan;
        private readonly Action _callback;

        private static CancellationTokenSource _cancellationTokenSource;

        public Timer(TimeSpan timeSpan, Action callback)
        {
            _timeSpan = timeSpan;
            _callback = callback;
            _cancellationTokenSource = new CancellationTokenSource();
        }
        public void Start()
        {
            CancellationTokenSource cts = _cancellationTokenSource; // safe copy
            Device.StartTimer(_timeSpan, () =>
            {
                if (cts.IsCancellationRequested)
                {
                    return false;
                }
                _callback.Invoke();
                return true; //true to continuous, false to single use
            });
        }

        public void Stop()
        {
            Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
        }
    }

MainPage.xaml

<ContentPage.Content>
    <views:SKCanvasView PaintSurface="OnPainting" />
</ContentPage.Content>

MainPage.xaml.cs

public partial class MainPage : ContentPage,INotifyPropertyChanged
    {
        private Timer _timer;

        private TimeSpan _totalSeconds = new TimeSpan(0, 0, 0, 10);
        public TimeSpan TotalSeconds
        {
            get { return _totalSeconds; }
            set 
            {
                _totalSeconds = value;
                OnPropertyChanged("TotalSeconds");
            }
        }

        public TableLayoutView()
        {
            InitializeComponent();

            _timer = new Timer(TimeSpan.FromSeconds(1), CountDown);
            TotalSeconds = _totalSeconds;
        }

        private void CountDown()
        {
            if (_totalSeconds.TotalSeconds == 0)
            {
                //do something after hitting 0, in this example it just stops/resets the timer
                StopTimerCommand();
            }
            else
            {
                TotalSeconds = _totalSeconds.Subtract(new TimeSpan(0, 0, 0, 1));
            }
        }

        public void OnPainting(object sender, SKPaintSurfaceEventArgs e)
        {
            var surface = e.Surface;
            var canvas = surface.Canvas;
            canvas.Clear(SKColors.White);

            // create the paint for the text
            var textPaint = new SKPaint
            {
                IsAntialias = true,
                Style = SKPaintStyle.Fill,
                Color = SKColors.Black,
                TextSize = 80
            };
            // draw the text (from the baseline)
            canvas.DrawText(TotalSeconds.ToString(@"hh\:mm"), 60, 160 + 80, textPaint);

            _timer.Start();
        }
    }

如何将计时器计数值与在运行时使用SkiaSharp作为独立线程绘制的文本绑定,同样我可能有多个具有不同计时器计数的文本,它们应作为线程运行而不阻塞其他线程 . 我有要求使用SkiaSharp绘制文本,我不能使用普通的标签控件 .

回答(0)