简体   繁体   English

.NET MAUI 在页面加载时设置项目 SelectedIndex 延迟

[英].NET MAUI Setting an item SelectedIndex on Page Load is Delayed

When the view appears on the screen there is a short delay setting the values to each control.当视图出现在屏幕上时,会有一个短暂的延迟来设置每个控件的值。 Is it possible to set the values before the user sees the view?是否可以在用户看到视图之前设置值?

public UserSettingsView()
{
    InitializeComponent();
    LoadAsync();        
}

private async void LoadAsync()
{
    try
    {
        // Loading data from API
        Languages = await _languageService.GetAsync(AccessToken);
        USStates = await _uSStateService.GetAsync(AccessToken);
        
        // Assigning the list to the ItemSource of each Picker.
        ddlLanguages.ItemsSource = Languages;
        ddlUSStates.ItemsSource = USStates;

        // Getting the user's preferred settings
        var userSettings = await _accountService.GetSettingsAsync(UserID, AccessToken);

        if (userSettings != null)
        {
            // Setting user values to each Picker control. 
            // This is where the delay happens.
            ddlLanguages.SelectedIndex = Languages.FindIndex(x => x.ID == userSettings .LanguageID);
            ddlUSStates.SelectedIndex = USStates.FindIndex(x => x.ID == userSettings .USStateID);
            cbAge.IsChecked = currentSettings.AgeQualified;
        }
    }
    catch
    {
        await DisplayAlert("Oh no!", "Error loading the page", "OK");
    }
}  

To resolve the delay, I am passing the two lists for the languages and the States from the previous page.为了解决延迟问题,我传递了上一页中的语言和国家的两个列表。

public UserSettingsView(List<Language> _languages, List<USState> _usStates)
{                      
    InitializeComponent();

    Languages = _languages;
    USStates = _usStates;

    LoadAsync();
}    

private async void LoadAsync()
{
    try
    {            
        ddlLanguages.ItemsSource = Languages;
        ddlUSStates.ItemsSource = USStates;

        var currentSettings = await _accountService.GetSettingsAsync(UserID, AccessToken);

        if (currentSettings != null)
        {
            ddlLanguages.SelectedIndex = Languages.FindIndex(x => x.ID == currentSettings.LanguageID);
            ddlUSStates.SelectedIndex = USStates.FindIndex(x => x.ID == currentSettings.USStateID);
            switchAgeQualification.IsToggled = currentSettings.AgeQualified;
        }
    }
    catch
    {
        await DisplayAlert("Error", "Could not load page data", "OK");
    }
}

If I understand correctly, you currently have a line like this:如果我理解正确的话,你现在有这样一行:

await Navigation.PushModalAsync(new UserSettingsView());

I don't see the types of the properties involved, but the basic idea is to do all the slow await s BEFORE doing new UserSettingsView... .我没有看到涉及的属性类型,但基本思想是在执行new UserSettingsView...之前执行所有缓慢的await s。

Something like this:是这样的:

public class UserSettingsData
{
  SomeType1 Languages;
  SomeType2 USStates;
  SomeType3 UserSettings;
}

...
// Slow await calls.
var data = await UserSettingsView.PrepAsync(UserId, AccessToken);
// Now create and display the view.
await Navigation.PushModalAsync(new UserSettingsView(data));

...
  public static async UserSettingsData PrepAsync(SomeType4 UserId, SomeType5 AccessToken)
  {
    var data = new UserSettingsData();
    data.Languages = await _accountService.GetSettingsAsync(...);
    data.USStates = await ...;
    data.UserSettings = await ...;
  }
  public UserSettingsView(UserSettingsData data)
  {
    ...
    // NOT AN ASYNC METHOD, so happens immediately, before page is shown.
    Load(data);
  }

  // NOT AN ASYNC METHOD, so happens immediately.
  private void Load(UserSettingsData data)
  {
    Languages = data.Languages;
    USStates = data.USStates;
    var userSettings = data.UserSettings;
    ...

    // if still need DisplayAlert
    Dispatcher.InvokeOnMainThread(async () =>
      await DisplayAlert...
    );
  }

Replace "SomeType" etc with your actual types.将“SomeType”等替换为您的实际类型。

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

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