ToastButton builders
This commit is contained in:
Родитель
89ed383f5d
Коммит
5c8fc1b970
|
@ -48,26 +48,6 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a button to the current toast.
|
||||
/// </summary>
|
||||
/// <param name="content">Text to display on the button.</param>
|
||||
/// <param name="activationType">Type of activation this button will use when clicked. Defaults to Foreground.</param>
|
||||
/// <param name="arguments">App-defined arguments that the app can later retrieve once it is activated when the user clicks the button.</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
#endif
|
||||
public ToastContentBuilder AddButton(string content, ToastActivationType activationType, ToastArguments arguments)
|
||||
{
|
||||
AddButton(content, activationType, SerializeArgumentsIncludingGeneric(arguments));
|
||||
|
||||
// Remove this button from the custom arguments list
|
||||
_buttonsUsingCustomArguments.RemoveAt(_buttonsUsingCustomArguments.Count - 1);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private string SerializeArgumentsIncludingGeneric(ToastArguments arguments)
|
||||
{
|
||||
if (_genericArguments.Count == 0)
|
||||
|
@ -86,27 +66,6 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
return arguments.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a button to the current toast.
|
||||
/// </summary>
|
||||
/// <param name="content">Text to display on the button.</param>
|
||||
/// <param name="activationType">Type of activation this button will use when clicked. Defaults to Foreground.</param>
|
||||
/// <param name="arguments">App-defined arguments that the app can later retrieve once it is activated when the user clicks the button.</param>
|
||||
/// <param name="imageUri">Optional image icon for the button to display (required for buttons adjacent to inputs like quick reply).</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
#endif
|
||||
public ToastContentBuilder AddButton(string content, ToastActivationType activationType, ToastArguments arguments, Uri imageUri)
|
||||
{
|
||||
AddButton(content, activationType, SerializeArgumentsIncludingGeneric(arguments), imageUri);
|
||||
|
||||
// Remove this button from the custom arguments list
|
||||
_buttonsUsingCustomArguments.RemoveAt(_buttonsUsingCustomArguments.Count - 1);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a button to the current toast.
|
||||
/// </summary>
|
||||
|
@ -119,19 +78,6 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
return AddButton(content, activationType, arguments, default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an button to the toast that will be display to the right of the input text box, achieving a quick reply scenario.
|
||||
/// </summary>
|
||||
/// <param name="textBoxId">ID of an existing <see cref="ToastTextBox"/> in order to have this button display to the right of the input, achieving a quick reply scenario.</param>
|
||||
/// <param name="content">Text to display on the button.</param>
|
||||
/// <param name="activationType">Type of activation this button will use when clicked. Defaults to Foreground.</param>
|
||||
/// <param name="arguments">App-defined arguments that the app can later retrieve once it is activated when the user clicks the button.</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
public ToastContentBuilder AddButton(string textBoxId, string content, ToastActivationType activationType, ToastArguments arguments)
|
||||
{
|
||||
return AddButton(textBoxId, content, activationType, arguments, default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a button to the current toast.
|
||||
/// </summary>
|
||||
|
@ -140,6 +86,9 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
/// <param name="arguments">App-defined string of arguments that the app can later retrieve once it is activated when the user clicks the button.</param>
|
||||
/// <param name="imageUri">Optional image icon for the button to display (required for buttons adjacent to inputs like quick reply).</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
#endif
|
||||
public ToastContentBuilder AddButton(string content, ToastActivationType activationType, string arguments, Uri imageUri)
|
||||
{
|
||||
// Add new button
|
||||
|
@ -156,28 +105,6 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
return AddButton(button);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an button to the toast that will be display to the right of the input text box, achieving a quick reply scenario.
|
||||
/// </summary>
|
||||
/// <param name="textBoxId">ID of an existing <see cref="ToastTextBox"/> in order to have this button display to the right of the input, achieving a quick reply scenario.</param>
|
||||
/// <param name="content">Text to display on the button.</param>
|
||||
/// <param name="activationType">Type of activation this button will use when clicked. Defaults to Foreground.</param>
|
||||
/// <param name="arguments">App-defined arguments that the app can later retrieve once it is activated when the user clicks the button.</param>
|
||||
/// <param name="imageUri">An optional image icon for the button to display (required for buttons adjacent to inputs like quick reply)</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
#endif
|
||||
public ToastContentBuilder AddButton(string textBoxId, string content, ToastActivationType activationType, ToastArguments arguments, Uri imageUri)
|
||||
{
|
||||
AddButton(textBoxId, content, activationType, SerializeArgumentsIncludingGeneric(arguments), imageUri);
|
||||
|
||||
// Remove this button from the custom arguments list
|
||||
_buttonsUsingCustomArguments.RemoveAt(_buttonsUsingCustomArguments.Count - 1);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a button to the current toast.
|
||||
/// </summary>
|
||||
|
@ -185,19 +112,30 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
public ToastContentBuilder AddButton(IToastButton button)
|
||||
{
|
||||
if (button is ToastButton toastButton && toastButton.Content == null)
|
||||
{
|
||||
throw new InvalidOperationException("Content is required on button.");
|
||||
}
|
||||
|
||||
// List has max 5 buttons
|
||||
if (ButtonList.Count == 5)
|
||||
{
|
||||
throw new InvalidOperationException("A toast can't have more than 5 buttons");
|
||||
}
|
||||
|
||||
ButtonList.Add(button);
|
||||
|
||||
if (button is ToastButton b)
|
||||
if (button is ToastButton b && b.CanAddArguments())
|
||||
{
|
||||
_buttonsUsingCustomArguments.Add(b);
|
||||
foreach (var arg in _genericArguments)
|
||||
{
|
||||
if (!b.ContainsArgument(arg.Key))
|
||||
{
|
||||
b.AddArgument(arg.Key, arg.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ButtonList.Add(button);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,13 +11,14 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
/// Builder class used to create <see cref="ToastContent"/>
|
||||
/// </summary>
|
||||
public partial class ToastContentBuilder
|
||||
#if !WINRT
|
||||
: IToastActivateableBuilder<ToastContentBuilder>
|
||||
#endif
|
||||
{
|
||||
private Dictionary<string, string> _genericArguments = new Dictionary<string, string>();
|
||||
|
||||
private bool _customArgumentsUsedOnToastItself;
|
||||
|
||||
private List<ToastButton> _buttonsUsingCustomArguments = new List<ToastButton>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets internal instance of <see cref="ToastContent"/>. This is equivalent to the call to <see cref="ToastContentBuilder.GetToastContent"/>.
|
||||
/// </summary>
|
||||
|
@ -196,14 +197,9 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
{
|
||||
foreach (var button in actions.Buttons)
|
||||
{
|
||||
if (button is ToastButton b && b.ActivationType != ToastActivationType.Protocol && !_buttonsUsingCustomArguments.Contains(b))
|
||||
if (button is ToastButton b && b.CanAddArguments() && !b.ContainsArgument(key))
|
||||
{
|
||||
var bArgs = ToastArguments.Parse(b.Arguments);
|
||||
if (!bArgs.Contains(key))
|
||||
{
|
||||
bArgs.Add(key, value);
|
||||
b.Arguments = bArgs.ToString();
|
||||
}
|
||||
b.AddArgument(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -243,9 +239,31 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
public ToastContentBuilder SetProtocolActivation(Uri protocol)
|
||||
{
|
||||
return SetProtocolActivation(protocol, default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the toast notification to launch the specified url when the toast body is clicked.
|
||||
/// </summary>
|
||||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <param name="targetApplicationPfn">New in Creators Update: The target PFN, so that regardless of whether multiple apps are registered to handle the same protocol uri, your desired app will always be launched.</param>
|
||||
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
|
||||
public ToastContentBuilder SetProtocolActivation(Uri protocol, string targetApplicationPfn)
|
||||
{
|
||||
Content.Launch = protocol.ToString();
|
||||
Content.ActivationType = ToastActivationType.Protocol;
|
||||
|
||||
if (targetApplicationPfn != null)
|
||||
{
|
||||
if (Content.ActivationOptions == null)
|
||||
{
|
||||
Content.ActivationOptions = new ToastActivationOptions();
|
||||
}
|
||||
|
||||
Content.ActivationOptions.ProtocolActivationTargetApplicationPfn = targetApplicationPfn;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Toolkit.Uwp.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// Interfaces for classes that can have activation info added to them.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the host object.</typeparam>
|
||||
internal interface IToastActivateableBuilder<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a key (without value) to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
#endif
|
||||
T AddArgument(string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key, int value);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key, double value);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key, float value);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key, bool value);
|
||||
|
||||
#if !WINRT
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the content is clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself. Note that the enums are stored using their numeric value, so be aware that changing your enum number values might break existing activation of toasts currently in Action Center.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T AddArgument(string key, Enum value);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Configures the content to use background activation when it is clicked.
|
||||
/// </summary>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T SetBackgroundActivation();
|
||||
|
||||
/// <summary>
|
||||
/// Configures the content to use protocol activation when it is clicked.
|
||||
/// </summary>
|
||||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T SetProtocolActivation(Uri protocol);
|
||||
|
||||
/// <summary>
|
||||
/// Configures the content to use protocol activation when it is clicked.
|
||||
/// </summary>
|
||||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <param name="targetApplicationPfn">New in Creators Update: The target PFN, so that regardless of whether multiple apps are registered to handle the same protocol uri, your desired app will always be launched.</param>
|
||||
/// <returns>The current instance of the object.</returns>
|
||||
T SetProtocolActivation(Uri protocol, string targetApplicationPfn);
|
||||
}
|
||||
}
|
|
@ -3,14 +3,23 @@
|
|||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.Toolkit.Uwp.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// A button that the user can click on a Toast notification.
|
||||
/// </summary>
|
||||
public sealed class ToastButton : IToastButton
|
||||
public sealed class ToastButton :
|
||||
#if !WINRT
|
||||
IToastActivateableBuilder<ToastButton>,
|
||||
#endif
|
||||
IToastButton
|
||||
{
|
||||
private Dictionary<string, string> _arguments = new Dictionary<string, string>();
|
||||
|
||||
private bool _usingCustomArguments;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ToastButton"/> class.
|
||||
/// </summary>
|
||||
|
@ -30,6 +39,17 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
|
||||
Content = content;
|
||||
Arguments = arguments;
|
||||
|
||||
_usingCustomArguments = arguments.Length > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ToastButton"/> class.
|
||||
/// </summary>
|
||||
public ToastButton()
|
||||
{
|
||||
// Arguments are required (we'll initialize to empty string which is fine).
|
||||
Arguments = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -71,6 +91,272 @@ namespace Microsoft.Toolkit.Uwp.Notifications
|
|||
/// </summary>
|
||||
public string HintActionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the text to display on the button.
|
||||
/// </summary>
|
||||
/// <param name="content">The text to display on the button.</param>
|
||||
/// <returns>The current instance of the <see cref="ToastButton"/>.</returns>
|
||||
public ToastButton SetContent(string content)
|
||||
{
|
||||
Content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key (without value) to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton AddArgument(string key)
|
||||
{
|
||||
return AddArgumentHelper(key, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
#if WINRT
|
||||
[Windows.Foundation.Metadata.DefaultOverload]
|
||||
[return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("ToastButton")]
|
||||
#endif
|
||||
public ToastButton AddArgument(string key, string value)
|
||||
{
|
||||
return AddArgumentHelper(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
#if WINRT
|
||||
[return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("ToastButton")]
|
||||
#endif
|
||||
public ToastButton AddArgument(string key, int value)
|
||||
{
|
||||
return AddArgumentHelper(key, value.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
#if WINRT
|
||||
[return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("ToastButton")]
|
||||
#endif
|
||||
public ToastButton AddArgument(string key, double value)
|
||||
{
|
||||
return AddArgumentHelper(key, value.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
#if WINRT
|
||||
[return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("ToastButton")]
|
||||
#endif
|
||||
public ToastButton AddArgument(string key, float value)
|
||||
{
|
||||
return AddArgumentHelper(key, value.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
#if WINRT
|
||||
[return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("ToastButton")]
|
||||
#endif
|
||||
public ToastButton AddArgument(string key, bool value)
|
||||
{
|
||||
return AddArgumentHelper(key, value ? "1" : "0"); // Encode as 1 or 0 to save string space
|
||||
}
|
||||
|
||||
#if !WINRT
|
||||
/// <summary>
|
||||
/// Adds a key/value to the activation arguments that will be returned when the toast notification or its buttons are clicked.
|
||||
/// </summary>
|
||||
/// <param name="key">The key for this value.</param>
|
||||
/// <param name="value">The value itself. Note that the enums are stored using their numeric value, so be aware that changing your enum number values might break existing activation of toasts currently in Action Center.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton AddArgument(string key, Enum value)
|
||||
{
|
||||
return AddArgumentHelper(key, ((int)(object)value).ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
private ToastButton AddArgumentHelper(string key, string value)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (_usingCustomArguments)
|
||||
{
|
||||
throw new InvalidOperationException("You cannot use the AddArgument methods if you've set the Arguments property. Use the default ToastButton constructor instead.");
|
||||
}
|
||||
|
||||
if (ActivationType == ToastActivationType.Protocol)
|
||||
{
|
||||
throw new InvalidOperationException("You cannot use the AddArgument methods when using protocol activation.");
|
||||
}
|
||||
|
||||
bool alreadyExists = _arguments.ContainsKey(key);
|
||||
|
||||
_arguments[key] = value;
|
||||
|
||||
Arguments = alreadyExists ? SerializeArgumentsHelper(_arguments) : AddArgumentHelper(Arguments, key, value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private string SerializeArgumentsHelper(IDictionary<string, string> arguments)
|
||||
{
|
||||
var args = new ToastArguments();
|
||||
|
||||
foreach (var a in arguments)
|
||||
{
|
||||
args.Add(a.Key, a.Value);
|
||||
}
|
||||
|
||||
return args.ToString();
|
||||
}
|
||||
|
||||
private string AddArgumentHelper(string existing, string key, string value)
|
||||
{
|
||||
string pair = ToastArguments.EncodePair(key, value);
|
||||
|
||||
if (string.IsNullOrEmpty(existing))
|
||||
{
|
||||
return pair;
|
||||
}
|
||||
else
|
||||
{
|
||||
return existing + ToastArguments.Separator + pair;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the button to launch the specified url when the button is clicked.
|
||||
/// </summary>
|
||||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetProtocolActivation(Uri protocol)
|
||||
{
|
||||
return SetProtocolActivation(protocol, default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the button to launch the specified url when the button is clicked.
|
||||
/// </summary>
|
||||
/// <param name="protocol">The protocol to launch.</param>
|
||||
/// <param name="targetApplicationPfn">New in Creators Update: The target PFN, so that regardless of whether multiple apps are registered to handle the same protocol uri, your desired app will always be launched.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetProtocolActivation(Uri protocol, string targetApplicationPfn)
|
||||
{
|
||||
if (_arguments.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException("SetProtocolActivation cannot be used in conjunction with AddArgument");
|
||||
}
|
||||
|
||||
Arguments = protocol.ToString();
|
||||
ActivationType = ToastActivationType.Protocol;
|
||||
|
||||
if (targetApplicationPfn != null)
|
||||
{
|
||||
if (ActivationOptions == null)
|
||||
{
|
||||
ActivationOptions = new ToastActivationOptions();
|
||||
}
|
||||
|
||||
ActivationOptions.ProtocolActivationTargetApplicationPfn = targetApplicationPfn;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the button to use background activation when the button is clicked.
|
||||
/// </summary>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetBackgroundActivation()
|
||||
{
|
||||
ActivationType = ToastActivationType.Background;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the behavior that the toast should use when the user invokes this button. Desktop-only, supported in builds 16251 or higher. New in Fall Creators Update.
|
||||
/// </summary>
|
||||
/// <param name="afterActivationBehavior">The behavior that the toast should use when the user invokes this button.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetAfterActivationBehavior(ToastAfterActivationBehavior afterActivationBehavior)
|
||||
{
|
||||
if (ActivationOptions == null)
|
||||
{
|
||||
ActivationOptions = new ToastActivationOptions();
|
||||
}
|
||||
|
||||
ActivationOptions.AfterActivationBehavior = afterActivationBehavior;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets an identifier used in telemetry to identify your category of action. This should be something like "Delete", "Reply", or "Archive". In the upcoming toast telemetry dashboard in Dev Center, you will be able to view how frequently your actions are being clicked.
|
||||
/// </summary>
|
||||
/// <param name="actionId">An identifier used in telemetry to identify your category of action.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetHintActionId(string actionId)
|
||||
{
|
||||
HintActionId = actionId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets an optional image icon for the button to display (required for buttons adjacent to inputs like quick reply).
|
||||
/// </summary>
|
||||
/// <param name="imageUri">An optional image icon for the button to display.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetImageUri(Uri imageUri)
|
||||
{
|
||||
ImageUri = imageUri.ToString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the ID of an existing <see cref="ToastTextBox"/> in order to have this button display to the right of the input, achieving a quick reply scenario.
|
||||
/// </summary>
|
||||
/// <param name="textBoxId">The ID of an existing <see cref="ToastTextBox"/>.</param>
|
||||
/// <returns>The current instance of <see cref="ToastButton"/></returns>
|
||||
public ToastButton SetTextBoxId(string textBoxId)
|
||||
{
|
||||
TextBoxId = textBoxId;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal bool CanAddArguments()
|
||||
{
|
||||
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments;
|
||||
}
|
||||
|
||||
internal bool ContainsArgument(string key)
|
||||
{
|
||||
return _arguments.ContainsKey(key);
|
||||
}
|
||||
|
||||
internal Element_ToastAction ConvertToElement()
|
||||
{
|
||||
var el = new Element_ToastAction()
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
private void PopToast()
|
||||
{
|
||||
// Generate the toast notification content and pop the toast
|
||||
new ToastContentBuilder().SetToastScenario(ToastScenario.Reminder)
|
||||
new ToastContentBuilder()
|
||||
.SetToastScenario(ToastScenario.Reminder)
|
||||
.AddArgument("action", "viewEvent")
|
||||
.AddArgument("eventId", 1983)
|
||||
.AddText("Adaptive Tiles Meeting")
|
||||
|
|
|
@ -49,15 +49,20 @@ namespace Microsoft.Toolkit.Win32.WpfCore.SampleApp
|
|||
.AddInputTextBox("tbReply", "Type a reply")
|
||||
|
||||
// Buttons
|
||||
.AddButton("Reply", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", MyToastActions.Reply))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Reply")
|
||||
.AddArgument("action", MyToastActions.Reply)
|
||||
.SetBackgroundActivation())
|
||||
|
||||
.AddButton("Like", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", MyToastActions.Like))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Like")
|
||||
.AddArgument("action", MyToastActions.Like)
|
||||
.SetBackgroundActivation())
|
||||
|
||||
.AddButton("View", ToastActivationType.Foreground, new ToastArguments()
|
||||
.Add("action", MyToastActions.ViewImage)
|
||||
.Add("imageUrl", image))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("View")
|
||||
.AddArgument("action", MyToastActions.ViewImage)
|
||||
.AddArgument("imageUrl", image))
|
||||
|
||||
// And show the toast!
|
||||
.Show();
|
||||
|
|
|
@ -150,17 +150,23 @@ namespace UnitTests.Notifications
|
|||
// Act
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
ToastContentBuilder anotherReference = builder
|
||||
.AddButton("Accept", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "accept"))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Accept")
|
||||
.AddArgument("action", "accept")
|
||||
.SetBackgroundActivation())
|
||||
.AddButton(new ToastButtonSnooze())
|
||||
.AddButton("View", ToastActivationType.Protocol, "https://msn.com")
|
||||
|
||||
// Add generic arguments halfway through (should be applied to existing buttons and to any subsequent buttons added later)
|
||||
.AddArgument(userIdKey, userIdValue)
|
||||
|
||||
.AddButton("Decline", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "decline"))
|
||||
.AddButton("Report", ToastActivationType.Protocol, "https://microsoft.com");
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Decline")
|
||||
.AddArgument("action", "decline")
|
||||
.SetBackgroundActivation())
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Report")
|
||||
.SetProtocolActivation(new Uri("https://microsoft.com")));
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
@ -188,7 +194,7 @@ namespace UnitTests.Notifications
|
|||
|
||||
var button5 = actions.Buttons[4] as ToastButton;
|
||||
Assert.AreEqual("Report", button5.Content);
|
||||
Assert.AreEqual("https://microsoft.com", button5.Arguments);
|
||||
Assert.AreEqual("https://microsoft.com/", button5.Arguments);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
@ -197,16 +203,20 @@ namespace UnitTests.Notifications
|
|||
// Act
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
ToastContentBuilder anotherReference = builder
|
||||
.AddButton("Accept", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "accept")
|
||||
.Add("userId", 601))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Accept")
|
||||
.AddArgument("action", "accept")
|
||||
.AddArgument("userId", 601)
|
||||
.SetBackgroundActivation())
|
||||
|
||||
// Add generic arguments halfway through (should be applied to existing buttons and to any subsequent buttons added later)
|
||||
// Add generic arguments halfway through (in this case shouldn't overwrite anything)
|
||||
.AddArgument("userId", 542)
|
||||
|
||||
.AddButton("Decline", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "decline")
|
||||
.Add("userId", 601));
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Decline")
|
||||
.AddArgument("action", "decline")
|
||||
.AddArgument("userId", 601)
|
||||
.SetBackgroundActivation());
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
@ -241,8 +251,9 @@ namespace UnitTests.Notifications
|
|||
|
||||
.AddButton("Decline", ToastActivationType.Background, "myDeclineStr")
|
||||
|
||||
.AddButton("View", ToastActivationType.Foreground, new ToastArguments()
|
||||
.Add("action", "view"));
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("View")
|
||||
.AddArgument("action", "view"));
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
@ -287,8 +298,10 @@ namespace UnitTests.Notifications
|
|||
// Act
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
ToastContentBuilder anotherReference = builder
|
||||
.AddButton("Accept", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "accept"))
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Accept")
|
||||
.AddArgument("action", "accept")
|
||||
.SetBackgroundActivation())
|
||||
|
||||
.AddArgument("userId", 542)
|
||||
|
||||
|
@ -296,8 +309,10 @@ namespace UnitTests.Notifications
|
|||
|
||||
.AddArgument("name", "Andrew")
|
||||
|
||||
.AddButton("Decline", ToastActivationType.Background, new ToastArguments()
|
||||
.Add("action", "decline"));
|
||||
.AddButton(new ToastButton()
|
||||
.SetContent("Decline")
|
||||
.AddArgument("action", "decline")
|
||||
.SetBackgroundActivation());
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
@ -315,6 +330,100 @@ namespace UnitTests.Notifications
|
|||
Assert.AreEqual("action=decline;userId=542;name=Andrew", button2.Arguments);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToastButtonBuilders_General_ReturnSelf()
|
||||
{
|
||||
ToastButton button = new ToastButton();
|
||||
ToastButton anotherReference = button
|
||||
.SetContent("View")
|
||||
.AddArgument("action", "view")
|
||||
.AddArgument("imageId", 601);
|
||||
|
||||
Assert.AreSame(button, anotherReference);
|
||||
Assert.AreEqual("View", button.Content);
|
||||
Assert.AreEqual("action=view;imageId=601", button.Arguments);
|
||||
Assert.AreEqual(ToastActivationType.Foreground, button.ActivationType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToastButtonBuilders_AllProperties_ReturnSelf()
|
||||
{
|
||||
ToastButton button = new ToastButton();
|
||||
ToastButton anotherReference = button
|
||||
.SetContent("View")
|
||||
.SetImageUri(new Uri("ms-appx:///Assets/view.png"))
|
||||
.AddArgument("action", "view")
|
||||
.SetBackgroundActivation()
|
||||
.SetAfterActivationBehavior(ToastAfterActivationBehavior.PendingUpdate)
|
||||
.SetHintActionId("viewImage");
|
||||
|
||||
Assert.AreSame(button, anotherReference);
|
||||
Assert.AreEqual("View", button.Content);
|
||||
Assert.AreEqual("action=view", button.Arguments);
|
||||
Assert.AreEqual("ms-appx:///Assets/view.png", button.ImageUri);
|
||||
Assert.AreEqual(ToastActivationType.Background, button.ActivationType);
|
||||
Assert.AreEqual(ToastAfterActivationBehavior.PendingUpdate, button.ActivationOptions.AfterActivationBehavior);
|
||||
Assert.AreEqual("viewImage", button.HintActionId);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToastButtonBuilders_ProtocolActivation_ReturnSelf()
|
||||
{
|
||||
ToastButton button = new ToastButton();
|
||||
ToastButton anotherReference = button
|
||||
.SetContent("View")
|
||||
.SetProtocolActivation(new Uri("https://msn.com"));
|
||||
|
||||
Assert.AreSame(button, anotherReference);
|
||||
Assert.AreEqual("View", button.Content);
|
||||
Assert.AreEqual("https://msn.com/", button.Arguments);
|
||||
Assert.AreEqual(ToastActivationType.Protocol, button.ActivationType);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToastButtonBuilders_ProtocolActivationWithPfn_ReturnSelf()
|
||||
{
|
||||
ToastButton button = new ToastButton();
|
||||
ToastButton anotherReference = button
|
||||
.SetContent("View")
|
||||
.SetProtocolActivation(new Uri("https://msn.com"), "MyPfn");
|
||||
|
||||
Assert.AreSame(button, anotherReference);
|
||||
Assert.AreEqual("View", button.Content);
|
||||
Assert.AreEqual("https://msn.com/", button.Arguments);
|
||||
Assert.AreEqual(ToastActivationType.Protocol, button.ActivationType);
|
||||
Assert.AreEqual("MyPfn", button.ActivationOptions.ProtocolActivationTargetApplicationPfn);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(InvalidOperationException))]
|
||||
public void ToastButtonBuilders_InvalidProtocolAfterArguments_ReturnSelf()
|
||||
{
|
||||
new ToastButton()
|
||||
.SetContent("View")
|
||||
.AddArgument("action", "view")
|
||||
.SetProtocolActivation(new Uri("https://msn.com"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(InvalidOperationException))]
|
||||
public void ToastButtonBuilders_InvalidArgumentsAfterProtocol_ReturnSelf()
|
||||
{
|
||||
new ToastButton()
|
||||
.SetContent("View")
|
||||
.SetProtocolActivation(new Uri("https://msn.com"))
|
||||
.AddArgument("action", "view");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(InvalidOperationException))]
|
||||
public void ToastButtonBuilders_InvalidArgumentsAfterCustomArguments_ReturnSelf()
|
||||
{
|
||||
var button = new ToastButton("View", "viewArgs");
|
||||
|
||||
button.AddArgument("action", "view");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SetToastDurationTest_WithCustomToastDuration_ReturnSelfWithCustomToastDurationSet()
|
||||
{
|
||||
|
@ -780,27 +889,6 @@ namespace UnitTests.Notifications
|
|||
Assert.AreEqual(testButtonLaunchArgs, button.Arguments);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddButtonTest_WithTextOnlyButtonAndToastArguments_ReturnSelfWithButtonAdded()
|
||||
{
|
||||
// Arrange
|
||||
string testButtonContent = "Test Button Content";
|
||||
ToastActivationType testToastActivationType = ToastActivationType.Background;
|
||||
var testButtonLaunchArgs = new ToastArguments().Add("action", "view");
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
|
||||
// Act
|
||||
ToastContentBuilder anotherReference = builder.AddButton(testButtonContent, testToastActivationType, testButtonLaunchArgs);
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
||||
var button = (builder.Content.Actions as ToastActionsCustom).Buttons.First() as ToastButton;
|
||||
Assert.AreEqual(testButtonContent, button.Content);
|
||||
Assert.AreEqual(testToastActivationType, button.ActivationType);
|
||||
Assert.AreEqual(testButtonLaunchArgs.ToString(), button.Arguments);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddButtonTest_WithCustomImageAndTextButton_ReturnSelfWithButtonAdded()
|
||||
{
|
||||
|
@ -825,30 +913,6 @@ namespace UnitTests.Notifications
|
|||
Assert.AreEqual(testImageUriSrc.OriginalString, button.ImageUri);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddButtonTest_WithCustomImageAndTextButtonAndToastArguments_ReturnSelfWithButtonAdded()
|
||||
{
|
||||
// Arrange
|
||||
string testButtonContent = "Test Button Content";
|
||||
ToastActivationType testToastActivationType = ToastActivationType.Background;
|
||||
var testButtonLaunchArgs = new ToastArguments().Add("action", "accept");
|
||||
Uri testImageUriSrc = new Uri("C:/justatesturi.jpg");
|
||||
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
|
||||
// Act
|
||||
ToastContentBuilder anotherReference = builder.AddButton(testButtonContent, testToastActivationType, testButtonLaunchArgs, testImageUriSrc);
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
||||
var button = (builder.Content.Actions as ToastActionsCustom).Buttons.First() as ToastButton;
|
||||
Assert.AreEqual(testButtonContent, button.Content);
|
||||
Assert.AreEqual(testToastActivationType, button.ActivationType);
|
||||
Assert.AreEqual(testButtonLaunchArgs.ToString(), button.Arguments);
|
||||
Assert.AreEqual(testImageUriSrc.OriginalString, button.ImageUri);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddButtonTest_WithTextBoxId_ReturnSelfWithButtonAdded()
|
||||
{
|
||||
|
@ -875,32 +939,6 @@ namespace UnitTests.Notifications
|
|||
Assert.AreEqual(testImageUriSrc.OriginalString, button.ImageUri);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddButtonTest_WithTextBoxIdAndToastArguments_ReturnSelfWithButtonAdded()
|
||||
{
|
||||
// Arrange
|
||||
string testInputTextBoxId = Guid.NewGuid().ToString();
|
||||
string testButtonContent = "Test Button Content";
|
||||
ToastActivationType testToastActivationType = ToastActivationType.Background;
|
||||
var testButtonLaunchArgs = new ToastArguments().Add("action", "send");
|
||||
Uri testImageUriSrc = new Uri("C:/justatesturi.jpg");
|
||||
|
||||
ToastContentBuilder builder = new ToastContentBuilder();
|
||||
|
||||
// Act
|
||||
ToastContentBuilder anotherReference = builder.AddButton(testInputTextBoxId, testButtonContent, testToastActivationType, testButtonLaunchArgs, testImageUriSrc);
|
||||
|
||||
// Assert
|
||||
Assert.AreSame(builder, anotherReference);
|
||||
|
||||
var button = (builder.Content.Actions as ToastActionsCustom).Buttons.First() as ToastButton;
|
||||
Assert.AreEqual(testInputTextBoxId, button.TextBoxId);
|
||||
Assert.AreEqual(testButtonContent, button.Content);
|
||||
Assert.AreEqual(testToastActivationType, button.ActivationType);
|
||||
Assert.AreEqual(testButtonLaunchArgs.ToString(), button.Arguments);
|
||||
Assert.AreEqual(testImageUriSrc.OriginalString, button.ImageUri);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddInputTextBoxTest_WithStringIdOnly_ReturnSelfWithInputTextBoxAdded()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче