Ensure that we don't throw if actions are bound before the ActionTarget is assigned

This commit is contained in:
Antony Male 2014-07-24 16:30:57 +01:00
parent 3e4e09a598
commit b53d0f5195
3 changed files with 22 additions and 2 deletions

View File

@ -72,7 +72,14 @@ namespace Stylet.Xaml
{
var newTarget = View.GetActionTarget(this.Subject);
MethodInfo targetMethodInfo = null;
// If it's being set to the initial value, ignore it
// At this point, we're executing the View's InitializeComponent method, and the ActionTarget hasn't yet been assigned
// If they've opted to throw if the target is null, then this will cause that exception.
// We'll just wait until the ActionTarget is assigned, and we're called again
if (newTarget == View.InitialActionTarget)
return;
this.guardPropertyGetter = null;
if (newTarget == null)
{

View File

@ -67,6 +67,13 @@ namespace Stylet.Xaml
var newTarget = View.GetActionTarget(this.subject);
MethodInfo targetMethodInfo = null;
// If it's being set to the initial value, ignore it
// At this point, we're executing the View's InitializeComponent method, and the ActionTarget hasn't yet been assigned
// If they've opted to throw if the target is null, then this will cause that exception.
// We'll just wait until the ActionTarget is assigned, and we're called again
if (newTarget == View.InitialActionTarget)
return;
if (newTarget == null)
{
if (this.targetNullBehaviour == ActionUnavailableBehaviour.Throw)

View File

@ -10,6 +10,12 @@ namespace Stylet.Xaml
/// </summary>
public class View : DependencyObject
{
/// <summary>
/// Initial value of the ActionTarget property.
/// This can be used as a marker - if the property has this value, it hasn't yet been assigned to anything else.
/// </summary>
public static readonly object InitialActionTarget = new object();
private static readonly ContentPropertyAttribute defaultContentProperty = new ContentPropertyAttribute("Content");
/// <summary>
@ -41,7 +47,7 @@ namespace Stylet.Xaml
/// The object's ActionTarget. This is used to determine what object to call Actions on by the ActionExtension markup extension.
/// </summary>
public static readonly DependencyProperty ActionTargetProperty =
DependencyProperty.RegisterAttached("ActionTarget", typeof(object), typeof(View), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
DependencyProperty.RegisterAttached("ActionTarget", typeof(object), typeof(View), new FrameworkPropertyMetadata(InitialActionTarget, FrameworkPropertyMetadataOptions.Inherits));
/// <summary>
/// Fetch the ViewModel currently associated with a given object