Update UI label async/await

summeysummey USUniversity ✭✭

If you take a look at the screen shot I'm trying to update the Counts 0 label. This app talks to some hardware my company manufactures that reads values over wifi through UDP. For now I just wrote a infinite loop to generate numbers to test out the UI to imitate the counting . All the others will read in the values at startup up for now and then not update. The counts is the only thing that will not be static. I created this UI with Xaml and would like to data bind this eventually. I am just trying to understand how to update the UI with async

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ST375WifiViewer
{
    public partial class MainPage : ContentPage
    {

        public MainPage ()
        {
            InitializeComponent ();


        }

        async void OnButtonClicked (object sender, EventArgs args)
        {

            await DoIt (); 

        }

        async Task DoIt ()
        {
            await Task.Run (() => {

                int counter = 0;
                while (true) {
                    Debug.WriteLine(string.Format ("{0}", counter));

                    //readcounts.Text = (string.Format ("{0}", counter));
                    counter += 1;
                }
            });
        }


        void UpdateUi(string counter){

            this.readcounts.Text = (string.Format ("{0}", counter));
        }


    }
}
Tagged:

Answers

  • summeysummey USUniversity ✭✭

    After working on this for a few days i figured it out... This bit of code allows me to update the Counts 0 label with Counts. I anyone want to know more please hit me up.

    async void  DoCount ()
            {
                while (true) {
                    counter++;
                    counts = counter.ToString ();
    
                    await Task.Run (() => {
                        st375model.Count = counts;
                    });
                }
            }
    
Sign In or Register to comment.