0

在此异步方法返回值之前,我需要启动另一个 ContentPage:

public class GettingCountry : ContentPage
{
    public static List<string> CountriesList = new List<string>();

    MainPage mainPage = new MainPage();
    public async Task<List<RootObject>> FetchAsync(string url)
    {
        string jsonString;
        using (var httpClient = new System.Net.Http.HttpClient())
        {
            var stream = await httpClient.GetStreamAsync(url);
            StreamReader reader = new StreamReader(stream);
            jsonString = reader.ReadToEnd();
        }

        var listOfCountries = new List<RootObject>();

        var responseCountries = JArray.Parse(JObject.Parse(jsonString)["response"]["items"].ToString());

        foreach (var countryInResponse in responseCountries)
        {
            var rootObject = new RootObject((int)countryInResponse["id"], (string)countryInResponse["title"]);

            CountriesList.Add(rootObject.Title);
        }

        //I NEED TO NAVIGATE TO FillingPage() FROM HERE:
        await Navigation.PushAsync(new FillingPage());

        //await Navigation.PushModalAsync(new NavigationPage(new FillingPage()));

        return listOfCountries;
    }

需要启动的页面是:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class FillingPage : ContentPage
{
    public FillingPage ()
    {
        GettingCountry gettingCountry = new GettingCountry();

        Label header = new Label
        {
            Text = "Заполните бланк",
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            TextColor = Color.Blue
        };

        Entry nameEntry = new Entry()
        {
            Placeholder = "Имя",
        };

        Entry surnameEntry = new Entry()
        {
            Placeholder = "Фамилия"
        };

        Picker countryPicker = new Picker()
        {
            Title = "Страна",
            VerticalOptions = LayoutOptions.CenterAndExpand
        };

        foreach (string country in GettingCountry.CountriesList)
        {
            countryPicker.Items.Add(country);
        }

        SearchBar townSearchBar = new SearchBar()
        {
            Placeholder = "Город",
            SearchCommand = new Command(() =>
            {
            })
        };

        SearchBar universitySearchBar = new SearchBar()
        {
            Placeholder = "Университет",
            SearchCommand = new Command(() =>
            {
            })
        };
        Button myButton = new Button()
       {
           TextColor = Color.Green,
           Text = "Выполнить",
           FontSize = 22
       };

        // Accomodate iPhone status bar.
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

        // Build the page.
        this.Content = new StackLayout
        {
            Children =
            {
                header,
                nameEntry,
                surnameEntry,
                countryPicker,
                townSearchBar,
                universitySearchBar,
                myButton
            }
        };
    }
}

}

但是这个代码await Navigation.PushAsync(new FillingPage());只有在我按下按钮时才能正常工作。当我按下按钮时,所需的页面会很好地启动。但是方法中的相同代码不起作用。我已经解压了它。FillingPage()当我尝试从异步方法内部启动它时,它会进入但不会启动它。

4

1 回答 1

4

这很可能是由于未在主线程上执行操作的结果。尝试像这样包装您的代码:

Device.BeginInvokeOnMainThread(async () =>
{
    await Navigation.PushAsync(new FillingPage());
}

编辑:私信后,我了解到问题中没有足够的信息来了解真正的问题。应用程序正在调用FetchAsyncApplication.OnStart它根本不是视图层次结构的一部分,因此导航方法不起作用。提供了以下内容:

protected override void OnStart ()
{
    getCountry();
}

private async void getCountry()
{
    var url = "...";
    GettingCountry gettingCountry = new GettingCountry();
    await gettingCountry.FetchAsync(url);
}

GettingCountry像某种ContentPage数据访问类一样被使用,并且它当前不是 UI 的一部分,因为MainPage它被设置为其他东西。快速破解更像是:

private async void getCountry()
{
    var url = "...";
    GettingCountry gettingCountry = new GettingCountry();
    var data = await gettingCountry.FetchAsync(url);
    await MainPage.Navigation.PushAsync(new FillingPage(data));
 }

我会建议另外两个需要改进的领域。

  1. 考虑重构GettingCountry,因为它不需要是ContentPage.
  2. 调查一个替代调用,以便async void不使用它。
于 2017-03-20T16:43:00.137 回答