Search code examples
c#xamarin.formstaskrefit

task oncomplete is not moving on


enter image description hereI need to go to the next activity when a task is completed but it won't. It successfully makes an account in the database but won't move on.

As seen below I've tried oncomplete(task) but once I enter credentials and hit register it creates the account but doesn't move on.

Code below (using refit for Http request):

using Kula.API;
using Refit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Kula
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
    // set variables 
    Entry RinputEmail;
    Entry RinputPassword;
    Entry RinputUsername;
    Entry RinputFullName;
    IMyAPI myAPI;
    APIRequestHelper apiRequestHelper;
    public MainPage()
    {
        InitializeComponent();

        myAPI = RestService.For<IMyAPI>("http://10.0.2.2:8080");

        apiRequestHelper = new APIRequestHelper(myAPI);
        // reference mainpage.xamal
        RinputEmail = this.FindByName<Entry>("Remail");
        RinputPassword = this.FindByName<Entry>("Rpassword");
        RinputUsername = this.FindByName<Entry>("Rusername");
        RinputFullName = this.FindByName<Entry>("Rfullname");

    }

    async private void GTLogin_Clicked(object sender, EventArgs e)
    {
        //navigate to Login page
        await Navigation.PushAsync(new Login());
    }

    async private void registerUser_Clicked(object sender, EventArgs e)
    {
        await apiRequestHelper.RequestRegisterUserAsync((RinputEmail.Text).ToString(), 
  (RinputPassword.Text).ToString(), (RinputUsername.Text).ToString(), 
  (RinputFullName.Text).ToString()).ContinueWith((nextpage) => { Navigation.PushAsync(new 
  Login()); });


    }



}
};

API_REQUEST_HELPER

   namespace Kula.API
 {
public class APIRequestHelper
{

    IMyAPI myAPI;

    public APIRequestHelper(IMyAPI myAPI)
    {

        this.myAPI = myAPI;
    }

    public async Task RequestRegisterUserAsync(string email, string 
   password, string username, string fullname)
    {


        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, 
   object>();
        data.Add("username", username);
        data.Add("email", email);
        data.Add("password", password);
        data.Add("fullname", fullname);

         await myAPI.RegisterUser(data);






    }

    /*
    public async Task RequestRegisterUserAsync(string email, string 
   password, Boolean returntoken)
    {


        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, 
   object>();
        data.Add("email", email);
        data.Add("password", password);
        data.Add("returnSecureToken", returntoken);


        string result = await myAPI.RegisterUsertwo(data);

    }
    */
    public async Task RequestLoginUserAsync(string email, string 
  password, Boolean returntoken)
    {


        //create params for request 
          Dictionary<string, object> data = new Dictionary<string, 
  object>();
        data.Add("email", email);
        data.Add("password", password);
        data.Add("returnSecureToken", returntoken);


        string result = await myAPI.LoginUser(data);

    }

    public async Task RequestLoginUserAsync(string email, string password)
    {


        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, 
  object>();
        data.Add("email", email);
        data.Add("password", password);



        string result = await myAPI.LoginUser(data);

    }

    public async Task RequestSaveProfileInfoAsync(string username, string fullname)
    {


        //create params for request 
        Dictionary<string, object> data = new Dictionary<string, 
 object>();
        data.Add("username", username);
        data.Add("fullname", fullname);
        string result = await myAPI.SaveProfileInfo(data);

    }
  }
}

MY_API

using Kula.Models;
using Refit;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Kula.API
{

public interface IMyAPI
{
    [Get("/Getuid")]
    Task<string> GetUid();

    [Post("/register")]
    Task<string> 
 RegisterUser([Body(BodySerializationMethod.UrlEncoded)] 
  Dictionary<string, object> data);


    [Get("/response")]
    Task<string> GetResponse();

    [Post("/accounts:signInWithPassword?key=")]
    Task<string> LoginUser([Body(BodySerializationMethod.UrlEncoded)] 
   Dictionary<string, object> data);

    [Post("/SaveProfileInfo")]
    Task<string> 
    SaveProfileInfo([Body(BodySerializationMethod.UrlEncoded)] 
    Dictionary<string, object> data);

    [Get("/GetProfileInfo")]
    Task<List<string>> GetProfileInfo();

    [Get("/GetSocialFeedTiles")]
    Task<List<string>> GetSocialFeedTiles();



}

Solution

  • I simulated your problem with the following simple code:

    async private void registerUser_Clicked(object sender, EventArgs e)
    {
        await Task.Delay(5000).ContinueWith((x) => Navigation.PushAsync(new Page1()));
    }
    

    If you put a breakpoint in the line with Navigation.PushAsync you will notice that it is called from a thread that is not the main thread. That is why even when the code reaches that part, it has no effect: You can only change the UI from the Main Thread!

    To achieve this you can simply use async await in the regular way, as follows:

    async private void registerUser_Clicked(object sender, EventArgs e)
    {
        await Task.Delay(5000);
        await Navigation.PushAsync(new Page1());
    }
    

    Or comming back to your own code:

    async private void registerUser_Clicked(object sender, EventArgs e)
    {
        await apiRequestHelper.RequestRegisterUserAsync((RinputEmail.Text).ToString(),(RinputPassword.Text).ToString(), (RinputUsername.Text).ToString(),(RinputFullName.Text).ToString());
    
        await Navigation.PushAsync(new Login());
    }
    

    A nice post about async/await + Xamarin.Forms can be found here:

    https://devblogs.microsoft.com/xamarin/getting-started-with-async-await/


    BONUS:

    You mentioned using OnComplete: A way to use that to get your EventHandler launching the Login page would be to configure the task to return to the main thread, by using ConfigureAwait:

    Task.Delay(5000).ConfigureAwait(true).GetAwaiter().OnCompleted(()=>Navigation.PushAsync(new Page1()));
    

    Note the use of ConfigureAwait(true), if you use instead ConfigureAwait(false) then the PushAsync method is again called from a thread other than the main thread!