Xamarin表单 - 在重新加载页面时填充两个异步选择器错误

时间:2017-10-24 09:30:47

标签: asynchronous xamarin xamarin.forms picker

第二个取决于第一个选择者electionsIndexChanged事件。

我需要先设置每个选择器的源,然后设置任何用户首选项,以便选择具有正确值的每个选择器。我尝试了很多方法,但我不知道最好的方法。这就是我现在正在做的事情。

用于填充每个下拉列表的方法是异步的,因此我必须执行以下操作:

public UserPreferencesPage()
    {
        InitializeComponent();
        BindingContext = viewModel = new UserPreferencesViewModel();



    }

    protected async override void OnAppearing()
    {
        base.OnAppearing();

        await LoadData();
        await SetPreferences();



    }

    private async Task LoadData()
    {

        await viewModel.LoadPreferenceData();
        await viewModel.LoadServiceData();
       if (!string.IsNullOrEmpty(viewModel.UserPreferences.ServiceCode))
        {
            await viewModel.LoadTeamClinicianData(viewModel.UserPreferences.ServiceCode);
        }

    }
    private async Task SetPreferences()
    {
        try
        {


        var serviceCode = viewModel.UserPreferences.ServiceCode;

        ServicePicker.SelectedIndex = viewModel.Services.IndexOf(viewModel.Services.Where(x => x.ServiceCode == serviceCode).FirstOrDefault());

        }
        catch (Exception ex)
        {

            throw;
        }

    }


    private async void ServicePicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var TeamClinicianCode = viewModel.UserPreferences.TeamClinicianCode;
        //    //save preference
        if (ServicePicker.SelectedItem != null)
        {
            viewModel.UserPreferences.ServiceCode = ((Service)ServicePicker.SelectedItem).ServiceCode.Trim();
           var a = await App.Database.SaveUserPreferenceAsync(viewModel.UserPreferences);
            //Load Dropdown
            if (!string.IsNullOrEmpty(viewModel.UserPreferences.ServiceCode))
            {
                viewModel.UserPreferences.TeamClinicianCode = string.Empty;

                await viewModel.LoadTeamClinicianData(viewModel.UserPreferences.ServiceCode);
                TeamClinicianPicker.SelectedIndex = viewModel.TeamClinicians.IndexOf(viewModel.TeamClinicians.Where(x => x.Code.Trim() == TeamClinicianCode.Trim()).FirstOrDefault());

                var b = await App.Database.SaveUserPreferenceAsync(viewModel.UserPreferences);
            }
        }
    }

    private async void TeamClinicianPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        //    //save preference
        viewModel.UserPreferences.TeamClinicianCode = ((TeamClinican)TeamClinicianPicker.SelectedItem).Code.Trim();
        var a = await App.Database.SaveUserPreferenceAsync(viewModel.UserPreferences);
    }

第一次加载页面时,它会正确加载并设置值。第二次加载页面时,有一个空例外,但我无法像在iOS项目中那样正确调试

我在iOS上使用Xamarin表单

由于

0 个答案:

没有答案