windowswindows-store-appswindows-8.1winrt-async

Windows 8.1 store apps OnCommandsRequested doesn't add ApplicationCommands when async used


On the App.xaml.cs I have the following code

private async void OnCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs e)
    {
        var loader = ResourceLoader.GetForCurrentView();

        var generalCommand = new SettingsCommand("General Settings", "General Settings", handler =>
        {
            var generalSettings = new GeneralSettingsFlyout();
            generalSettings.Show();
        });
        e.Request.ApplicationCommands.Add(generalCommand);

        object data;
        IAuthService _authService = new AuthService();
        if (Global.UserId == 0)
            data = await _authService.GetSettingValueBySettingName(DatabaseType.GeneralDb, ApplicationConstants.GeneralDbSettingNames.ShowSupportInfo);
        else
            data = await _authService.GetSettingValueBySettingName(DatabaseType.UserDb, ApplicationConstants.UserDbSettingNames.ShowSupportInfo);

        if (data != null && data.ToString().Equals("1"))
        {
            var supportCommand = new SettingsCommand("Support", "Support", handler =>
            {
                var supportPane = new SupportFlyout();
                supportPane.Show();
            });
            e.Request.ApplicationCommands.Add(supportCommand);
        }

        var aboutCommand = new SettingsCommand("About", loader.GetString("Settings_OptionLabels_About"), handler =>
       {
           var aboutPane = new About();
           aboutPane.Show();
       });
       e.Request.ApplicationCommands.Add(aboutCommand);           

}

This code adds the setting "General Settings" but neither "Support" or "About" commands. Can anyone advice what's wrong with this code?


Solution

  • Instead of querying the commands from your service when they are requested you'll need to query them ahead of time and then add the already known commands.

    You cannot use await in OnCommandsRequested.

    A method returns when it gets to the first await, so only commands added to the request before the await will be used.

    Since the SettingsPaneCommandsRequestedEventArgs doesn't provide a deferral there is no way to tell the requester to wait for internal async calls to complete.

    Note also that SettingsPane is deprecated and not recommended for new app development for Windows 10.