简体   繁体   English

导航到xamarin形式的下一页

[英]Navigation to next page in xamarin forms

Assume that i'm in the Page_1 while click the button have to navigate to Page_2.In Page_2 Api call has to done. 假设我在Page_1中,同时单击按钮必须导航到Page_2。在Page_2中,必须进行Api调用。

MyIssue is when i'm clicking the Button it doesn't navigate to Page_2 immediately it waits for the API response. MyIssue是当我单击按钮时,它不会立即导航到Page_2,而是等待API响应。

How to Navigate Immediately to Page_2 without waiting for the APi response. 如何在不等待APi响应的情况下立即导航到Page_2。

Code: 码:

Page_1.cs Page_1.cs

public partial class Page_1 : ContentPage
{
    public Page_1()
    {
        InitializeComponent();
    }
    private void Btn_click(object sender, EventArgs e)
    {
        Navigation.PushAsync(new Page_2());
    }

}

Page_2: 第2页:

public Page_2()
    {
        InitializeComponent();
    }
    protected override void OnAppearing()
    {
        HttpClient httpClient = new HttpClient();
        var obj = httpClient.GetAsync("//Api//").Result;
        if (obj.IsSuccessStatusCode)
        {

        }
    }

Same code works good in iOS as expected 相同的代码在iOS中按预期效果良好

You could load your data in an other Task to prevent blocking the UI. 您可以将数据加载到其他任务中,以防止阻塞UI。

protected override void OnAppearing()
{
    Task.Run( () => LoadData());
    base.OnAppearing();
}

private async void LoadData()
{
    HttpClient httpClient = new HttpClient();
    var obj = await httpClient.GetAsync("//Api//");
    if (obj.IsSuccessStatusCode)
    {
        // If you need to set properties on the view be sure to use MainThread
        // otherwise you won't see it on the view.
        Device.BeginInvokeOnMainThread(() => Name = "your text";);
    }
}

As per your question you are calling the API on Page constructor that's why it's taking time to load web API then navigating on page2. 根据您的问题,您正在页面构造器上调用API,这就是为什么花时间加载Web API然后在page2上导航。 If you want to navigate on page2 before a load the api. 如果要在加载api之前在page2上导航。 Check below code 检查下面的代码

    public partial class Page2 : ContentPage
        {

            bool IsLoading{ get; set; }
            public Page2()
            {
                InitializeComponent();
                  IsLoading = false;
          }
             protected async override void OnAppearing()
            {
                base.OnAppearing();
                if (!IsLoading)
                {
                  IsLoading=true
                  **Call the Web API Method Here**
                }
                IsLoading=false
            }


         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM