I am new to Xamarin.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)
{
GetTime();
ViewController.SetFieldsTick(Vars.dcounter));
}
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?
Thanks, Dan
I guess ViewController isn't static that's why you can't call a function from it like that, anyway your code is a bit difficult to understand, if you want to build an application with a clock or just displaying time take a look at this Xamarin.Forms example:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm
Thanks to DataBinding you don't need to set the UI manually, you should try.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.