diff --git a/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs b/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs
index ebee0a7..0a257fd 100644
--- a/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs
+++ b/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs
@@ -13,24 +13,42 @@ namespace CommunityToolkit.Authentication
public struct AccountsSettingsPaneConfig
{
///
- /// Gets or sets the header text for the accounts settings pane.
+ /// Gets or sets the header text for the add accounts settings pane.
///
- public string HeaderText { get; set; }
+ public string AddAccountHeaderText { get; set; }
+
+ ///
+ /// Gets or sets the header text for the manage accounts settings pane.
+ ///
+ public string ManageAccountHeaderText { get; set; }
///
/// Gets or sets the SettingsCommand collection for the account settings pane.
///
public IList Commands { get; set; }
+ ///
+ /// Gets or sets the WebAccountCommandParameter collection for the account settings pane.
+ ///
+ public WebAccountCommandParameter AccountCommandParameter { get; set; }
+
///
/// Initializes a new instance of the struct.
///
- /// The header text for the accounts settings pane.
+ /// The header text for the add accounts settings pane.
+ /// The header text for the manage accounts settings pane.
/// The SettingsCommand collection for the account settings pane.
- public AccountsSettingsPaneConfig(string headerText = null, IList commands = null)
+ /// The WebAccountCommandParameter for the account settings pane.
+ public AccountsSettingsPaneConfig(
+ string addAccountHeaderText = null,
+ string manageAccountHeaderText = null,
+ IList commands = null,
+ WebAccountCommandParameter accountCommandParameter = null)
{
- HeaderText = headerText;
+ AddAccountHeaderText = addAccountHeaderText;
+ ManageAccountHeaderText = manageAccountHeaderText;
Commands = commands;
+ AccountCommandParameter = accountCommandParameter;
}
}
}
diff --git a/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs b/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs
new file mode 100644
index 0000000..76733b5
--- /dev/null
+++ b/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs
@@ -0,0 +1,37 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Windows.UI.ApplicationSettings;
+
+namespace CommunityToolkit.Authentication
+{
+ ///
+ /// can be produced through this parameter.
+ ///
+ public class WebAccountCommandParameter
+ {
+ ///
+ /// Gets the delegate that's invoked when the user selects an account and a specific
+ /// action in the account settings pane.
+ ///
+ public WebAccountCommandInvokedHandler Invoked { get; }
+
+ ///
+ /// Gets the actions that the command performs on the web account in the accounts pane.
+ ///
+ public SupportedWebAccountActions Actions { get; }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The delegate that's invoked when the user selects an account and a specific
+ /// action in the account settings pane.
+ /// The actions that the command performs on the web account in the accounts pane.
+ public WebAccountCommandParameter(WebAccountCommandInvokedHandler invoked, SupportedWebAccountActions actions)
+ {
+ Invoked = invoked;
+ Actions = actions;
+ }
+ }
+}
diff --git a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
index d21f319..f35f98e 100644
--- a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
+++ b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
@@ -224,13 +224,94 @@ public override async Task GetTokenAsync(bool silentOnly = false)
return null;
}
+ ///
+ /// Display AccountSettingsPane for the management of logged-in users.
+ ///
+ /// .
+ public async Task ShowAccountManagementPaneAsync()
+ {
+ if (_webAccount == null)
+ {
+ throw new InvalidOperationException("Display account management pane requires at least one logged in account.");
+ }
+
+ if (_accountsSettingsPaneConfig?.AccountCommandParameter == null)
+ {
+ throw new ArgumentNullException("At least one account command is required to display the account management pane.");
+ }
+
+ // Build the AccountSettingsPane and configure it with available account commands.
+ void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSettingsPaneCommandsRequestedEventArgs e)
+ {
+ AccountsSettingsPaneEventDeferral deferral = e.GetDeferral();
+
+ // Apply the configured header.
+ var headerText = _accountsSettingsPaneConfig?.ManageAccountHeaderText;
+ if (!string.IsNullOrWhiteSpace(headerText))
+ {
+ e.HeaderText = headerText;
+ }
+
+ // Generate account command.
+ var commandParameter = _accountsSettingsPaneConfig?.AccountCommandParameter;
+ var webAccountCommand = new WebAccountCommand(
+ _webAccount,
+ async (command, args) =>
+ {
+ commandParameter.Invoked?.Invoke(command, args);
+
+ // When the logout command is triggered, we also need to modify the state of the Provider.
+ if (args.Action == WebAccountAction.Remove)
+ {
+ await SignOutAsync();
+ }
+ },
+ commandParameter.Actions);
+
+ e.WebAccountCommands.Add(webAccountCommand);
+
+ // Apply any configured setting commands.
+ var commands = _accountsSettingsPaneConfig?.Commands;
+ if (commands != null)
+ {
+ foreach (var command in commands)
+ {
+ e.Commands.Add(command);
+ }
+ }
+
+ deferral.Complete();
+ }
+
+ AccountsSettingsPane pane = null;
+ try
+ {
+ // GetForCurrentView may throw an exception if the current view isn't ready yet.
+ pane = AccountsSettingsPane.GetForCurrentView();
+ pane.AccountCommandsRequested += OnAccountCommandsRequested;
+
+ // Show the AccountSettingsPane and wait for the result.
+ await AccountsSettingsPane.ShowManageAccountsAsync();
+ }
+ catch (Exception)
+ {
+ }
+ finally
+ {
+ if (pane != null)
+ {
+ pane.AccountCommandsRequested -= OnAccountCommandsRequested;
+ }
+ }
+ }
+
private async Task SetAccountAsync(WebAccount account)
{
if (account == null)
{
// Clear account
- await SignOutAsync();
- return;
+ await SignOutAsync();
+ return;
}
else if (account.Id == _webAccount?.Id)
{
@@ -353,7 +434,7 @@ async void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSetti
}
// Apply the configured header.
- var headerText = _accountsSettingsPaneConfig?.HeaderText;
+ var headerText = _accountsSettingsPaneConfig?.AddAccountHeaderText;
if (!string.IsNullOrWhiteSpace(headerText))
{
e.HeaderText = headerText;
diff --git a/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml
new file mode 100644
index 0000000..ddac190
--- /dev/null
+++ b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
diff --git a/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml.cs b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml.cs
new file mode 100644
index 0000000..ae6f420
--- /dev/null
+++ b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml.cs
@@ -0,0 +1,29 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using CommunityToolkit.Authentication;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Controls;
+
+namespace UwpWindowsProviderSample
+{
+ ///
+ /// A simple button for triggering the globally configured WindowsProvider to manage account.
+ ///
+ public sealed partial class AccountManagerButton : UserControl
+ {
+ public AccountManagerButton()
+ {
+ this.InitializeComponent();
+ }
+
+ private async void ManagerButton_Click(object sender, RoutedEventArgs e)
+ {
+ if(ProviderManager.Instance.GlobalProvider is WindowsProvider provider)
+ {
+ await provider.ShowAccountManagementPaneAsync();
+ }
+ }
+ }
+}
diff --git a/Samples/UwpWindowsProviderSample/App.xaml.cs b/Samples/UwpWindowsProviderSample/App.xaml.cs
index 474ff48..eb650fb 100644
--- a/Samples/UwpWindowsProviderSample/App.xaml.cs
+++ b/Samples/UwpWindowsProviderSample/App.xaml.cs
@@ -3,8 +3,12 @@
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Authentication;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
using Windows.ApplicationModel.Activation;
using Windows.System;
+using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@@ -32,11 +36,31 @@ void ConfigureGlobalProvider()
if (ProviderManager.Instance.GlobalProvider == null)
{
string[] scopes = new string[] { "User.Read" };
- ProviderManager.Instance.GlobalProvider = new WindowsProvider(scopes);
+ var paneConfig = GetAccountsSettingsPaneConfig();
+ ProviderManager.Instance.GlobalProvider = new WindowsProvider(scopes, accountsSettingsPaneConfig: paneConfig);
}
});
}
+ AccountsSettingsPaneConfig GetAccountsSettingsPaneConfig()
+ {
+ void OnAccountCommandInvoked(WebAccountCommand command, WebAccountInvokedArgs args)
+ {
+ Debug.WriteLine($"Action: {args.Action}");
+ }
+
+ var accountCommandParameter = new WebAccountCommandParameter(
+ OnAccountCommandInvoked,
+ SupportedWebAccountActions.Remove | SupportedWebAccountActions.Manage);
+
+ var addAccountHeaderText = "Login account";
+ var manageAccountHeaderText = "Account management";
+
+ return new AccountsSettingsPaneConfig(addAccountHeaderText, manageAccountHeaderText, accountCommandParameter: accountCommandParameter);
+ }
+
+
+
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
diff --git a/Samples/UwpWindowsProviderSample/MainPage.xaml b/Samples/UwpWindowsProviderSample/MainPage.xaml
index e309926..fb31ca0 100644
--- a/Samples/UwpWindowsProviderSample/MainPage.xaml
+++ b/Samples/UwpWindowsProviderSample/MainPage.xaml
@@ -1,15 +1,17 @@
-
+
+
diff --git a/Samples/UwpWindowsProviderSample/MainPage.xaml.cs b/Samples/UwpWindowsProviderSample/MainPage.xaml.cs
index 81e60f1..3bb5de0 100644
--- a/Samples/UwpWindowsProviderSample/MainPage.xaml.cs
+++ b/Samples/UwpWindowsProviderSample/MainPage.xaml.cs
@@ -23,9 +23,11 @@ private async void OnProviderUpdated(object sender, ProviderUpdatedEventArgs e)
if (provider == null || provider.State != ProviderState.SignedIn)
{
SignedInUserTextBlock.Text = "Please sign in.";
+ ManagerButton.IsEnabled = false;
}
else
{
+ ManagerButton.IsEnabled = true;
SignedInUserTextBlock.Text = "Signed in as...";
var graphClient = provider.GetClient();
diff --git a/Samples/UwpWindowsProviderSample/UwpWindowsProviderSample.csproj b/Samples/UwpWindowsProviderSample/UwpWindowsProviderSample.csproj
index f5a8616..dfadcc1 100644
--- a/Samples/UwpWindowsProviderSample/UwpWindowsProviderSample.csproj
+++ b/Samples/UwpWindowsProviderSample/UwpWindowsProviderSample.csproj
@@ -116,6 +116,9 @@
PackageReference
+
+ AccountManagerButton.xaml
+
App.xaml
@@ -147,6 +150,10 @@
MSBuild:Compile
Designer
+
+ Designer
+ MSBuild:Compile
+
MSBuild:Compile
Designer