Updating the UI with Xamarin

Viewed 8432

I am having difficulty updating the UI using Xamarin. The objective is to make reactive UI so the user knows that application is thinking. Below are my attempts.

Attempt 1

private void BeginProcess(string fileName)
{
     Device.BeginInvokeOnMainThread(() => {
         iContent.Text = "UI Successfully updated.";
     });

     Device.BeginInvokeOnMainThread(() => {
         ProccessFolder testMethod = new ProccessFolder.Initialize(fileName);
     });
}

Attempt 2

private void UpdateUI () {
    iContent.Text = "UI Successfully updated.";
}

private void BeginProcess(string fileName)
{
     System.Threading.Thread t = new System.Threading.Thread(UpdateUI);
     t.Priority = System.Threading.ThreadPriority.Highest;
     t.IsBackground = false;
     t.Start();

     Device.BeginInvokeOnMainThread(() => {
         ProccessFolder testMethod = new ProccessFolder.Initialize(fileName);
     });
}

Attempt 3

private void BeginProcess(string fileName)
{
     Device.BeginInvokeOnMainThread(() => {
         iContent.Text = "UI Successfully updated.";
     });

     Task.Delay(5000);

     Device.BeginInvokeOnMainThread(() => {
         ProccessFolder testMethod = new ProccessFolder.Initialize(fileName);
     });
}

Unfortunately none of these work. What does work is if I place the ProcessFolder method in a background thread and invoke the UI changes on the main thread. However the completion time of the ProcessFolder method is slower.

Any suggestion on how I can update the UI while still executing ProcessFolder on the main thread?

2 Answers
Related