await Tasks returned from Actions

Fixes #53
This commit is contained in:
Antony Male 2021-02-19 08:41:02 +00:00
parent e195d29c01
commit 8684dc067f
4 changed files with 19 additions and 10 deletions

View File

@ -24,8 +24,9 @@ namespace Stylet.Samples.HelloDialog
this.NameString = "Click the button to show the dialog";
}
public void ShowDialog()
public async System.Threading.Tasks.Task ShowDialog()
{
throw new Exception("KABLAMMO");
var dialogVm = this.dialogFactory.CreateDialog1();
var result = this.windowManager.ShowDialog(dialogVm);
if (result.GetValueOrDefault())

View File

@ -5,6 +5,7 @@ using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
@ -189,19 +190,19 @@ namespace Stylet.Xaml
/// </summary>
/// <param name="targetMethodInfo">MethodInfo of method on new target</param>
/// <param name="newTargetType">Type of new target</param>
protected internal abstract void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType);
private protected abstract void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType);
/// <summary>
/// Invoked when a new target is set, after all other action has been taken
/// </summary>
/// <param name="oldTarget">Previous target</param>
/// <param name="newTarget">New target</param>
protected internal virtual void OnTargetChanged(object oldTarget, object newTarget) { }
private protected virtual void OnTargetChanged(object oldTarget, object newTarget) { }
/// <summary>
/// Assert that the target is not View.InitialActionTarget
/// </summary>
protected internal void AssertTargetSet()
private protected void AssertTargetSet()
{
// If we've made it this far and the target is still the default, then something's wrong
// Make sure they know
@ -226,14 +227,19 @@ namespace Stylet.Xaml
/// Invoke the target method with the given parameters
/// </summary>
/// <param name="parameters">Parameters to pass to the target method</param>
protected internal void InvokeTargetMethod(object[] parameters)
private protected void InvokeTargetMethod(object[] parameters)
{
this.logger.Info("Invoking method {0} on {1} with parameters ({2})", this.MethodName, this.TargetName(), parameters == null ? "none" : String.Join(", ", parameters));
try
{
var target = this.TargetMethodInfo.IsStatic ? null : this.Target;
this.TargetMethodInfo.Invoke(target, parameters);
var result = this.TargetMethodInfo.Invoke(target, parameters);
// Be nice and make sure that any exceptions get rethrown
if (result is Task task)
{
AwaitTask(task);
}
}
catch (TargetInvocationException e)
{
@ -243,6 +249,8 @@ namespace Stylet.Xaml
// http://stackoverflow.com/a/17091351/1086121
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
}
async void AwaitTask(Task t) => await t;
}
private string TargetName()

View File

@ -58,7 +58,7 @@ namespace Stylet.Xaml
/// </summary>
/// <param name="targetMethodInfo">MethodInfo of method on new target</param>
/// <param name="newTargetType">Type of new target</param>
protected internal override void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType)
private protected override void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType)
{
var methodParameters = targetMethodInfo.GetParameters();
if (methodParameters.Length > 1)
@ -74,7 +74,7 @@ namespace Stylet.Xaml
/// </summary>
/// <param name="oldTarget">Previous target</param>
/// <param name="newTarget">New target</param>
protected internal override void OnTargetChanged(object oldTarget, object newTarget)
private protected override void OnTargetChanged(object oldTarget, object newTarget)
{
if (oldTarget is INotifyPropertyChanged oldInpc)
PropertyChangedEventManager.RemoveHandler(oldInpc, this.PropertyChangedHandler, this.GuardName);

View File

@ -66,14 +66,14 @@ namespace Stylet.Xaml
/// </summary>
/// <param name="targetMethodInfo">MethodInfo of method on new target</param>
/// <param name="newTargetType">Type of new target</param>
protected internal override void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType)
private protected override void AssertTargetMethodInfo(MethodInfo targetMethodInfo, Type newTargetType)
{
var methodParameters = targetMethodInfo.GetParameters();
if (!(methodParameters.Length == 0 ||
(methodParameters.Length == 1 && (typeof(EventArgs).IsAssignableFrom(methodParameters[0].ParameterType) || methodParameters[0].ParameterType == typeof(DependencyPropertyChangedEventArgs))) ||
(methodParameters.Length == 2 && (typeof(EventArgs).IsAssignableFrom(methodParameters[1].ParameterType) || methodParameters[1].ParameterType == typeof(DependencyPropertyChangedEventArgs)))))
{
var e = new ActionSignatureInvalidException(String.Format("Method {0} on {1} must have the signatures void Method(), void Method(EventArgsOrSubClass e), or void Method(object sender, EventArgsOrSubClass e)", this.MethodName, newTargetType.Name));
var e = new ActionSignatureInvalidException(String.Format("Method {0} on {1} must have the signatures Method(), Method(EventArgsOrSubClass e), or Method(object sender, EventArgsOrSubClass e)", this.MethodName, newTargetType.Name));
logger.Error(e);
throw e;
}