在计时器iOS Xamarin上更改背景

时间:2017-07-23 05:10:32

标签: c# ios xamarin xamarin.ios visual-studio-mac

我正在构建一个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.

我不确定这意味着什么。

更新/编辑:(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);
            });

        }

1 个答案:

答案 0 :(得分:3)

更新用户界面要求您在主线程(UI线程)上,将Background.Image电话放在InvokeOnMainThread内将解决UIKit例外:

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

Xamarin有一个指南,概述了为什么/如何完成这些工作: