更改计时器背景 iOS Xamarin

Change Background on Timer iOS Xamarin

我正在构建一个 iOS 应用程序,我试图每隔几秒更改一次背景图片。我使用此处的代码设置了一个计时器:https://developer.xamarin.com/api/type/System.Threading.Timer/

我的代码如下所示:

using System;
using System.Threading;

using UIKit;

namespace TestApp
{
    public partial class ViewController : UIViewController
   {
    // New Timer
    class BackgroundTimer
    {
        public int counter = 0;
        public Timer tmr;

    }
    // Load screen
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        BackgroundTimer s = new BackgroundTimer();
        // Create the delegate that invokes methods for the timer.
        TimerCallback timerDelegate = new TimerCallback(ChangeBackground);

        // Create a timer that waits three seconds, then invokes every three seconds.
        Timer timer = new Timer(timerDelegate, s, 3000, 3000);

        // Keep a handle to the timer, so it can be disposed.
        s.tmr = timer;

        void ChangeBackground(Object state)
        {
            BackgroundTimer t = (BackgroundTimer)state;
            t.counter++;
            Background.Image = UIImage.FromBundle("HydePark");
        }

当我在模拟器中运行时,出现以下错误:

UIKit.UIKitThreadAccessException has been thrown

UIKit Consistency error: you are calling a UIKit method that can only 
be invoked from the UI thread.

我不确定那是什么意思。

Update/Edit:(2017 年 7 月 23 日)

下面的问题解决了我的问题。我想循环图像,所以我制作了一个随机生成器,但它只循环图像一次。

 // Load screen
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Random number generator 
        Random RandomImage = new Random();
        int RandomImg = RandomImage.Next(1, 6);
        string image = "";

        // Switch statement for displaying different messages
        switch (RandomImg)
        {
            case 1:
                image = "Image1";
                break;
            case 2:
                image = "Image2";
                break;
            case 3:
                image = "Image3";
                break;
            case 4:
                image = "Image4";
                break;
            case 5:
                image = "Image5";
                break;
        }

        BackgroundTimer s = new BackgroundTimer();
        // Create the delegate that invokes methods for the timer.
        TimerCallback timerDelegate = new TimerCallback(ChangeBackground);

        // Create a timer that waits three seconds, then invokes every three seconds.
        Timer timer = new Timer(timerDelegate, s, 3000, 3000);

        // Keep a handle to the timer, so it can be disposed.
        s.tmr = timer;

        void ChangeBackground(Object state)
        {
            BackgroundTimer t = (BackgroundTimer)state;
            t.counter++;
            InvokeOnMainThread(() =>
            {
                Background.Image = UIImage.FromBundle(image);
            });

        }

更新 UI 需要您在主线程(UI 线程),将您的 Background.Image 调用放在 InvokeOnMainThread 中将解决 UIKit 异常:

void ChangeBackground(Object state)
{
    BackgroundTimer t = (BackgroundTimer)state;
    t.counter++;
    InvokeOnMainThread(() =>
    {
        Background.Image = UIImage.FromBundle("HydePark");
    });
}

Xamarin 有一个指南,概述了 why/how 这些需要完成的工作: