mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-19 23:03:49 +02:00
Playing with Prisms TaskExtension
This commit is contained in:
parent
26094d25b4
commit
90a0f89010
@ -11,6 +11,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace ModernWpfPlayground
|
namespace ModernWpfPlayground
|
||||||
{
|
{
|
||||||
|
// ReSharper disable once ClassNeverInstantiated.Global
|
||||||
public class MainWindowViewModel : BaseViewModel
|
public class MainWindowViewModel : BaseViewModel
|
||||||
{
|
{
|
||||||
private const string AppName = "TaBEA 3.0.0";
|
private const string AppName = "TaBEA 3.0.0";
|
||||||
@ -21,7 +22,7 @@ namespace ModernWpfPlayground
|
|||||||
|
|
||||||
public MainWindowViewModel()
|
public MainWindowViewModel()
|
||||||
{
|
{
|
||||||
ShowDialogCommand = new DelegateCommand(async () => await ShowDialogAsync().ConfigureAwait(false));
|
ShowDialogCommand = new DelegateCommand(() => ShowDialogAsync().Await());
|
||||||
CloseCommand = new DelegateCommand(() => Application.Current.Shutdown());
|
CloseCommand = new DelegateCommand(() => Application.Current.Shutdown());
|
||||||
OpenViewModelCommand = new DelegateCommand(LoadViewModel);
|
OpenViewModelCommand = new DelegateCommand(LoadViewModel);
|
||||||
SaveViewModelCommand = new DelegateCommand(SaveViewModel);
|
SaveViewModelCommand = new DelegateCommand(SaveViewModel);
|
||||||
|
@ -39,7 +39,7 @@ namespace ModernWpfPlayground.MvvmStuff
|
|||||||
|
|
||||||
protected void ResetViewModel(Func<string, bool>? predicate = null)
|
protected void ResetViewModel(Func<string, bool>? predicate = null)
|
||||||
{
|
{
|
||||||
IEnumerable<string> keys = _values.Keys;
|
IEnumerable<string> keys = _values.Keys.ToArray();
|
||||||
if (predicate != null) keys = keys.Where(predicate);
|
if (predicate != null) keys = keys.Where(predicate);
|
||||||
foreach (var key in keys)
|
foreach (var key in keys)
|
||||||
{
|
{
|
||||||
|
85
App/MvvmStuff/TaskExtensions.cs
Normal file
85
App/MvvmStuff/TaskExtensions.cs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
namespace System.Threading.Tasks
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for the Task object.
|
||||||
|
/// </summary>
|
||||||
|
public static class TaskExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
public static void Await(this Task task)
|
||||||
|
{
|
||||||
|
task.Await(null, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="configureAwait">Configures an awaiter used to await this task</param>
|
||||||
|
public static void Await(this Task task, bool configureAwait)
|
||||||
|
{
|
||||||
|
task.Await(null, null, configureAwait);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
public static void Await(this Task task, Action completedCallback)
|
||||||
|
{
|
||||||
|
task.Await(completedCallback, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
public static void Await(this Task task, Action completedCallback, Action<Exception> errorCallback)
|
||||||
|
{
|
||||||
|
task.Await(completedCallback, errorCallback, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
public static void Await(this Task task, Action<Exception> errorCallback)
|
||||||
|
{
|
||||||
|
task.Await(null, errorCallback, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
/// <param name="configureAwait">Configures an awaiter used to await this task</param>
|
||||||
|
public async static void Await(this Task task, Action? completedCallback, Action<Exception>? errorCallback, bool configureAwait)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await task.ConfigureAwait(configureAwait);
|
||||||
|
completedCallback?.Invoke();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
errorCallback?.Invoke(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
91
App/MvvmStuff/TaskExtensions{T}.cs
Normal file
91
App/MvvmStuff/TaskExtensions{T}.cs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
namespace System.Threading.Tasks
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for the Task object.
|
||||||
|
/// </summary>
|
||||||
|
public static class TaskExtensionsT
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
public static void Await<T>(this Task<T> task)
|
||||||
|
{
|
||||||
|
task.Await(null, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="configureAwait">Configures an awaiter used to await this task</param>
|
||||||
|
public static void Await<T>(this Task<T> task, bool configureAwait)
|
||||||
|
{
|
||||||
|
task.Await(null, null, configureAwait);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
public static void Await<T>(this Task<T> task, Action<T> completedCallback)
|
||||||
|
{
|
||||||
|
task.Await(completedCallback, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
public static void Await<T>(this Task<T> task, Action<T> completedCallback, Action<Exception> errorCallback)
|
||||||
|
{
|
||||||
|
task.Await(completedCallback, errorCallback, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
public static void Await<T>(this Task<T> task, Action<Exception> errorCallback)
|
||||||
|
{
|
||||||
|
task.Await(null, errorCallback, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Awaits a task without blocking the main thread.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Primarily used to replace async void scenarios such as ctor's and ICommands.</remarks>
|
||||||
|
/// <typeparam name="T">The result type</typeparam>
|
||||||
|
/// <param name="task">The task to be awaited</param>
|
||||||
|
/// <param name="completedCallback">The action to perform when the task is complete.</param>
|
||||||
|
/// <param name="errorCallback">The action to perform when an error occurs executing the task.</param>
|
||||||
|
/// <param name="configureAwait">Configures an awaiter used to await this task</param>
|
||||||
|
public async static void Await<T>(this Task<T> task, Action<T>? completedCallback, Action<Exception>? errorCallback, bool configureAwait)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await task.ConfigureAwait(configureAwait);
|
||||||
|
completedCallback?.Invoke(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
errorCallback?.Invoke(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,9 @@
|
|||||||
x:Class="Controls.PropertyPresenter"
|
x:Class="Controls.PropertyPresenter"
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:controls="clr-namespace:Controls"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:controls="clr-namespace:Controls"
|
|
||||||
x:Name="LayoutRoot"
|
x:Name="LayoutRoot"
|
||||||
d:DesignHeight="300"
|
d:DesignHeight="300"
|
||||||
d:DesignWidth="300"
|
d:DesignWidth="300"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user