Skip to content

Commit

Permalink
Update nullable annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Sep 3, 2024
1 parent e4ed081 commit c03aec4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Avalonia.Xaml.Interactions/Core/CallMethodAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class CallMethodAction : Avalonia.Xaml.Interactivity.Action
/// <summary>
/// Identifies the <seealso cref="MethodName"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string> MethodNameProperty =
AvaloniaProperty.Register<CallMethodAction, string>(nameof(MethodName));
public static readonly StyledProperty<string?> MethodNameProperty =
AvaloniaProperty.Register<CallMethodAction, string?>(nameof(MethodName));

/// <summary>
/// Identifies the <seealso cref="TargetObject"/> avalonia property.
Expand All @@ -35,7 +35,7 @@ public class CallMethodAction : Avalonia.Xaml.Interactivity.Action
/// <summary>
/// Gets or sets the name of the method to invoke. This is a avalonia property.
/// </summary>
public string MethodName
public string? MethodName
{
get => GetValue(MethodNameProperty);
set => SetValue(MethodNameProperty, value);
Expand Down
32 changes: 22 additions & 10 deletions src/Avalonia.Xaml.Interactions/Core/ChangePropertyAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public class ChangePropertyAction : Avalonia.Xaml.Interactivity.Action
/// <summary>
/// Identifies the <seealso cref="PropertyName"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string> PropertyNameProperty =
AvaloniaProperty.Register<ChangePropertyAction, string>(nameof(PropertyName));
public static readonly StyledProperty<string?> PropertyNameProperty =
AvaloniaProperty.Register<ChangePropertyAction, string?>(nameof(PropertyName));

/// <summary>
/// Identifies the <seealso cref="TargetObject"/> avalonia property.
Expand All @@ -92,7 +92,7 @@ public class ChangePropertyAction : Avalonia.Xaml.Interactivity.Action
/// <summary>
/// Gets or sets the name of the property to change. This is a avalonia property.
/// </summary>
public string PropertyName
public string? PropertyName
{
get => GetValue(PropertyNameProperty);
set => SetValue(PropertyNameProperty, value);
Expand Down Expand Up @@ -146,11 +146,17 @@ public override object Execute(object? sender, object? parameter)
return false;
}

var propertyName = PropertyName;
if (propertyName is null)
{
return false;
}

if (targetObject is AvaloniaObject avaloniaObject)
{
if (PropertyName.Contains('.'))
if (propertyName.Contains('.'))
{
var avaloniaProperty = FindAttachedProperty(targetObject, PropertyName);
var avaloniaProperty = FindAttachedProperty(targetObject, propertyName);
if (avaloniaProperty is not null)
{
UpdateAvaloniaPropertyValue(avaloniaObject, avaloniaProperty);
Expand All @@ -161,7 +167,7 @@ public override object Execute(object? sender, object? parameter)
}
else
{
var avaloniaProperty = AvaloniaPropertyRegistry.Instance.FindRegistered(avaloniaObject, PropertyName);
var avaloniaProperty = AvaloniaPropertyRegistry.Instance.FindRegistered(avaloniaObject, propertyName);
if (avaloniaProperty is not null)
{
UpdateAvaloniaPropertyValue(avaloniaObject, avaloniaProperty);
Expand All @@ -177,24 +183,30 @@ public override object Execute(object? sender, object? parameter)
[RequiresUnreferencedCode("This functionality is not compatible with trimming.")]
private void UpdatePropertyValue(object targetObject)
{
var propertyName = PropertyName;
if (propertyName is null)
{
return;
}

var targetType = targetObject.GetType();
var targetTypeName = targetType.Name;
var propertyInfo = targetType.GetRuntimeProperty(PropertyName);
var propertyInfo = targetType.GetRuntimeProperty(propertyName);

if (propertyInfo is null)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
"Cannot find a property named {0} on type {1}.",
PropertyName,
propertyName,
targetTypeName));
}
else if (!propertyInfo.CanWrite)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
"Cannot find a property named {0} on type {1}.",
PropertyName,
propertyName,
targetTypeName));
}

Expand Down Expand Up @@ -240,7 +252,7 @@ private void UpdatePropertyValue(object targetObject)
CultureInfo.CurrentCulture,
"Cannot assign value of type {0} to property {1} of type {2}. The {1} property can be assigned only values of type {2}.",
Value?.GetType().Name ?? "null",
PropertyName,
propertyName,
propertyInfo.PropertyType.Name),
innerException);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Avalonia.Xaml.Interactions/Core/EventTriggerBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class EventTriggerBehavior : Trigger
/// <summary>
/// Identifies the <seealso cref="EventName"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string> EventNameProperty =
AvaloniaProperty.Register<EventTriggerBehavior, string>(nameof(EventName), EventNameDefaultValue);
public static readonly StyledProperty<string?> EventNameProperty =
AvaloniaProperty.Register<EventTriggerBehavior, string?>(nameof(EventName), EventNameDefaultValue);

/// <summary>
/// Identifies the <seealso cref="SourceObject"/> avalonia property.
Expand All @@ -34,7 +34,7 @@ public class EventTriggerBehavior : Trigger
/// <summary>
/// Gets or sets the name of the event to listen for. This is a avalonia property.
/// </summary>
public string EventName
public string? EventName
{
get => GetValue(EventNameProperty);
set => SetValue(EventNameProperty, value);
Expand Down Expand Up @@ -159,6 +159,11 @@ private void RegisterEvent(string eventName)
{
return;
}

if (EventName is null)
{
return;
}

var sourceObjectType = _resolvedSource.GetType();
var eventInfo = sourceObjectType.GetRuntimeEvent(EventName);
Expand Down

0 comments on commit c03aec4

Please sign in to comment.