Code refactoring, improved XML docs

This commit is contained in:
Sergio Pedri 2020-09-23 01:01:23 +02:00
Родитель 288e0c75bf
Коммит 83d84753b4
21 изменённых файлов: 70 добавлений и 46 удалений

Просмотреть файл

@ -13,6 +13,7 @@ using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Enumeration;
using Windows.System;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Connectivity
{

Просмотреть файл

@ -18,6 +18,7 @@ using Windows.Devices.Enumeration;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml.Media.Imaging;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Connectivity
{

Просмотреть файл

@ -13,6 +13,7 @@ using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Windows.System;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Connectivity
{

Просмотреть файл

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.System;
using Windows.UI.Xaml;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{

Просмотреть файл

@ -12,6 +12,7 @@ using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Xaml.Media.Imaging;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.UI
{

Просмотреть файл

@ -13,6 +13,7 @@ using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.UI.Extensions
{

Просмотреть файл

@ -11,6 +11,7 @@ using Windows.Foundation.Metadata;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Microsoft.Toolkit.Uwp.Extensions;
[assembly: InternalsVisibleTo("UnitTests.XamlIslands.UWPApp")]

Просмотреть файл

@ -7,21 +7,22 @@ using System.Threading.Tasks;
using Microsoft.Toolkit.Diagnostics;
using Windows.System;
namespace Microsoft.Toolkit.Uwp.Helpers
namespace Microsoft.Toolkit.Uwp.Extensions
{
/// <summary>
/// This class provides static methods helper for executing code in a DispatcherQueue.
/// Helpers for executing code in a <see cref="DispatcherQueue"/>.
/// </summary>
public static class DispatcherQueueHelper
public static class DispatcherQueueExtensions
{
/// <summary>
/// Extension method for <see cref="DispatcherQueue"/>. Offering an actual awaitable <see cref="Task"/> with optional result that will be executed on the given dispatcher.
/// Invokes a given function on the target <see cref="DispatcherQueue"/> and returns a
/// <see cref="Task"/> that completes when the invocation of the function is completed.
/// </summary>
/// <param name="dispatcher">DispatcherQueue of a thread to run <paramref name="function"/>.</param>
/// <param name="function"> Function to be executed on the given dispatcher.</param>
/// <param name="priority">DispatcherQueue execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
/// <param name="dispatcher">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
/// <param name="function">The <see cref="Action"/> to invoke.</param>
/// <param name="priority">The priority level for the function to invoke.</param>
/// <returns>A <see cref="Task"/> that completes when the invocation of <paramref name="function"/> is over.</returns>
/// <remarks>If the current thread has access to <paramref name="dispatcher"/>, <paramref name="function"/> will be invoked directly.</remarks>
public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
Guard.IsNotNull(function, nameof(function));
@ -71,14 +72,15 @@ namespace Microsoft.Toolkit.Uwp.Helpers
}
/// <summary>
/// Extension method for <see cref="DispatcherQueue"/>. Offering an actual awaitable <see cref="Task{T}"/> with optional result that will be executed on the given dispatcher.
/// Invokes a given function on the target <see cref="DispatcherQueue"/> and returns a
/// <see cref="Task{TResult}"/> that completes when the invocation of the function is completed.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="dispatcher">DispatcherQueue of a thread to run <paramref name="function"/>.</param>
/// <param name="function"> Function to be executed on the given dispatcher.</param>
/// <param name="priority">DispatcherQueue execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
/// <typeparam name="T">The return type of <paramref name="function"/> to relay through the returned <see cref="Task{TResult}"/>.</typeparam>
/// <param name="dispatcher">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
/// <param name="function">The <see cref="Func{TResult}"/> to invoke.</param>
/// <param name="priority">The priority level for the function to invoke.</param>
/// <returns>A <see cref="Task"/> that completes when the invocation of <paramref name="function"/> is over.</returns>
/// <remarks>If the current thread has access to <paramref name="dispatcher"/>, <paramref name="function"/> will be invoked directly.</remarks>
public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<T> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
Guard.IsNotNull(function, nameof(function));
@ -121,13 +123,14 @@ namespace Microsoft.Toolkit.Uwp.Helpers
}
/// <summary>
/// Extension method for <see cref="DispatcherQueue"/>. Offering an actual awaitable <see cref="Task"/> with optional result that will be executed on the given dispatcher.
/// Invokes a given function on the target <see cref="DispatcherQueue"/> and returns a
/// <see cref="Task"/> that acts as a proxy for the one returned by the given function.
/// </summary>
/// <param name="dispatcher">DispatcherQueue of a thread to run <paramref name="function"/>.</param>
/// <param name="function">Asynchronous function to be executed on the given dispatcher.</param>
/// <param name="priority">DispatcherQueue execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
/// <param name="dispatcher">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
/// <param name="function">The <see cref="Func{TResult}"/> to invoke.</param>
/// <param name="priority">The priority level for the function to invoke.</param>
/// <returns>A <see cref="Task"/> that acts as a proxy for the one returned by <paramref name="function"/>.</returns>
/// <remarks>If the current thread has access to <paramref name="dispatcher"/>, <paramref name="function"/> will be invoked directly.</remarks>
public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func<Task> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
Guard.IsNotNull(function, nameof(function));
@ -188,14 +191,15 @@ namespace Microsoft.Toolkit.Uwp.Helpers
}
/// <summary>
/// Extension method for <see cref="DispatcherQueue"/>. Offering an actual awaitable <see cref="Task{T}"/> with optional result that will be executed on the given dispatcher.
/// Invokes a given function on the target <see cref="DispatcherQueue"/> and returns a
/// <see cref="Task{TResult}"/> that acts as a proxy for the one returned by the given function.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="dispatcher">DispatcherQueue of a thread to run <paramref name="function"/>.</param>
/// <param name="function">Asynchronous function to be executed Asynchronously on the given dispatcher.</param>
/// <param name="priority">DispatcherQueue execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
/// <typeparam name="T">The return type of <paramref name="function"/> to relay through the returned <see cref="Task{TResult}"/>.</typeparam>
/// <param name="dispatcher">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
/// <param name="function">The <see cref="Func{TResult}"/> to invoke.</param>
/// <param name="priority">The priority level for the function to invoke.</param>
/// <returns>A <see cref="Task{TResult}"/> that relays the one returned by <paramref name="function"/>.</returns>
/// <remarks>If the current thread has access to <paramref name="dispatcher"/>, <paramref name="function"/> will be invoked directly.</remarks>
public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<Task<T>> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
Guard.IsNotNull(function, nameof(function));

Просмотреть файл

@ -11,6 +11,7 @@ using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Printing;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Helpers
{

Просмотреть файл

@ -4,6 +4,7 @@
using Windows.System;
using Windows.UI.Xaml;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Helpers
{

Просмотреть файл

@ -8,6 +8,7 @@ using System.Collections.ObjectModel;
using System.Linq;
using Windows.System;
using Windows.System.RemoteSystems;
using Microsoft.Toolkit.Uwp.Extensions;
namespace Microsoft.Toolkit.Uwp.Helpers
{

Просмотреть файл

@ -13,6 +13,7 @@ using Microsoft.Toolkit.Uwp.Helpers;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.System;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.Helpers
{
@ -27,14 +28,14 @@ namespace UnitTests.Helpers
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherQueueHelper_Action_Null()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action));
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Action));
}
[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherQueueHelper_Action_Ok_UIThread()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_UIThread) };
@ -55,7 +56,7 @@ namespace UnitTests.Helpers
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
await Task.Run(async () =>
{
await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () =>
await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_Action_Ok_NonUIThread) };
@ -77,7 +78,7 @@ namespace UnitTests.Helpers
[UITestMethod]
public void Test_DispatcherQueueHelper_Action_Exception()
{
var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
{
throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_Action_Exception));
});
@ -93,14 +94,14 @@ namespace UnitTests.Helpers
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherQueueHelper_FuncOfT_Null()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<int>));
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<int>));
}
[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread()
{
var textBlock = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
var textBlock = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
{
return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_UIThread) };
}).Result;
@ -121,11 +122,11 @@ namespace UnitTests.Helpers
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
await Task.Run(async () =>
{
var textBlock = await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () =>
var textBlock = await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () =>
{
return new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread) };
});
await DispatcherQueueHelper.EnqueueAsync(dispatcherQueue, () =>
await DispatcherQueueExtensions.EnqueueAsync(dispatcherQueue, () =>
{
Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherQueueHelper_FuncOfT_Ok_NonUIThread));
taskSource.SetResult(null);
@ -144,7 +145,7 @@ namespace UnitTests.Helpers
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfT_Exception()
{
var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<int>(() =>
var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<int>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfT_Exception));
}));
@ -161,14 +162,14 @@ namespace UnitTests.Helpers
[SuppressMessage("Style", "IDE0034", Justification = "Explicit overload for clarity")]
public void Test_DispatcherQueueHelper_FuncOfTask_Null()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<Task>));
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<Task>));
}
[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTask_Ok_UIThread) };
@ -188,7 +189,7 @@ namespace UnitTests.Helpers
{
try
{
await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () =>
await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () =>
{
await Task.Yield();
@ -211,7 +212,7 @@ namespace UnitTests.Helpers
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfTask_Exception()
{
var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<Task>(() =>
var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<Task>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTask_Exception));
}));
@ -227,14 +228,14 @@ namespace UnitTests.Helpers
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Null()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<Task<int>>));
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), default(Func<Task<int>>));
}
[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread()
{
DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), () =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherQueueHelper_FuncOfTaskOfT_Ok_UIThread) };
@ -254,7 +255,7 @@ namespace UnitTests.Helpers
{
try
{
await DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () =>
await DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), async () =>
{
await Task.Yield();
@ -279,7 +280,7 @@ namespace UnitTests.Helpers
[UITestMethod]
public void Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception()
{
var task = DispatcherQueueHelper.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<Task<int>>(() =>
var task = DispatcherQueueExtensions.EnqueueAsync(DispatcherQueue.GetForCurrentThread(), new Func<Task<int>>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherQueueHelper_FuncOfTaskOfT_Exception));
}));

Просмотреть файл

@ -16,6 +16,7 @@ using Windows.Globalization;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.UI.Controls
{

Просмотреть файл

@ -14,6 +14,7 @@ using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -8,6 +8,7 @@ using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -9,6 +9,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.ApplicationModel.Activation;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -8,6 +8,7 @@ using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -7,6 +7,7 @@ using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -10,6 +10,7 @@ using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{

Просмотреть файл

@ -9,6 +9,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Microsoft.Toolkit.Uwp.Extensions;
namespace UnitTests.XamlIslands.UWPApp
{