Forum Xamarin Xamarin.iOS

Basic Screen manipulation

DanDickersonDanDickerson USMember ✭✭
edited July 2018 in Xamarin.iOS

I am new to iOS programming, so forgive me if this is obvious.

I have a Timer that runs in the Main Application. Among other things, I need it to update fields in the Main.storyboard
In Main.cs:

        public static void StartTimer()
            DateTime startTime;
            startTime = DateTime.UtcNow;
            var dispatcherTimer = new System.Threading.Timer(DispatcherTimer_Tick, startTime, 0, 100);

        public static void DispatcherTimer_Tick(object state)

In the ViewController, I have a routine that updates the screen:

        public static  void SetFieldsTick(int tick)
            switch (tick)
                case 0:     // Display the Time data
                        UTC_Data.Text = Vars.DateStrZ;

If I declare SetFieldsTick as static, then I get the message

An object reference is required for the non-static field …

for each of the labels that I am trying to update.

If I don't declare it static, then it can refer to the labels, but I get the same error message when calling it in the Main.cs file.

Any suggestions on how to resolve this problem?




  • andnesandnes USMember ✭✭✭

    You could just start the timer from inside your ViewController class. Or is there a reason that you want to start it from your Main class?

  • DanDickersonDanDickerson USMember ✭✭

    It processes many things, in addition to updating the ViewController. Furthermore, it has to run continuously, even if the ViewController is closed or superseded by another one.

  • DanDickersonDanDickerson USMember ✭✭

    I tried setting a timer from inside the ViewController. When I ran it I got the exception:

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

    How do I update fields in the ViewController in real-time?

  • IanSchoenrockIanSchoenrock USMember ✭✭

    This might help, forgive me if you have already seen this:

    This is a great example on how to make a timer app in Xamarin.iOS.

    Hope this helps!

Sign In or Register to comment.