1
votes

I just started to learn Xamarin forms and wanted to know if there is a method like Unity's void Update(). This is so I can update UI elements, not just on an event like a button press. Also I would like to change the value of a variable after a specific time, and currently with my knowledge I would have to press a button to change the variables value(as in to run the if statement that changes the variable I would have to click a button).

Also, do you guys have a list of topics I should know as a beginner Xamarin Forms developer.

2
just use a timer.Jonathan Alfaro
There are ome options. What exactly are you trying to achieve?Paul Kertscher

2 Answers

1
votes

wanted to know if there is a method like Unity's void Update(). This is so I can update UI elements, not just on an event like a button press.

There are lots of ways you can update UI. You can update UI at the lifecycle methods of Page such as page.onappearing and page.ondisappearing, or at the lifecycle methods of App and etc. Tell us your requirement and we can give you more specific information.

Also I would like to change the value of a variable after a specific time

You can use a Timer in Xamarin.forms:

Device.StartTimer (new TimeSpan (0, 0, 60), () =>
{
    // do something every 60 seconds
    Device.BeginInvokeOnMainThread (() => 
    {
      // interact with UI elements
    });
    return true; // runs again, or false to stop
});

do you guys have a list of topics I should know as a beginner Xamarin Forms developer.do you guys have a list of topics I should know as a beginner Xamarin Forms developer.

Here is the document about Xamarin.forms and you can read section by section to learn Xamarin.

Update, response to this video:

    public MainPage()
    {
        InitializeComponent();

        //If you don't want to trigger the event by click button, you can call the method here
        //the method will be called after the page's initialization

        TimerButton_Clicked(this,new EventArgs());
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        //also you can call it here
        //the method will be called after the page appeared

        //TimerButton_Clicked(this, new EventArgs());
    }

    private void TimerButton_Clicked(object sender, EventArgs e)
    {
        //use this variable to count the second
        int time = 0;

        Device.StartTimer(new TimeSpan(0, 0, 0,0,200), () =>
        {
            time += 200;

            // do something every 200 milliseconds
            Device.BeginInvokeOnMainThread(() =>
            {
                // interact with UI elements
                TimerLabel.Text = time.ToString() + "milliseconds";

            });

            if (time >= 3000)
            {
                return false;
            }

            return true; // runs again, or false to stop
        });
    }
 }

The best way to achieve your requirement is use a Timer, I wrote a sample project for you and you can check it here.

0
votes

Xamarin.Forms projects are typically structured using MVVM, with data binding being performed between Views (what the user is looking at) controls and ViewModel properties.

ViewModels then implement the INotifyPropertyChanged interface and as such can automatically signal an update to the View when their property values change. The framework code for the View then automatically handles the thread synchronisation for updating the UI.

By using MVVM structure you can run all of your program "business" logic without any UI, the ViewModel then forms an interface to the this, by allowing for Command interaction (actions taken) and parameter conversion.

The View uses a ViewModel as its DataContext, and a ViewModel can even contain other ViewModels if required.

Its too large a topic to handle here, but I'd suggest that you look at some of the Xamarin example projects.