Skip to content

Commit

Permalink
Component Generator: handle nulls from events
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Sep 6, 2023
1 parent 0103dd2 commit ffd7b97
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ private string GetLocalHandlerFunctionBody()
{
var componentPropertyType = GetComponentPropertyTypeName(_bindedProperty, ContainingType);
var mauiPropertyType = GetTypeNameAndAddNamespace(_bindedProperty.Type);
var cast = componentPropertyType == mauiPropertyType ? "" : $"({componentPropertyType})";

argument = $"{cast}NativeControl.{_bindedProperty.Name}";
argument = componentPropertyType == mauiPropertyType
? $"NativeControl.{_bindedProperty.Name}"
: $"NativeControl.{_bindedProperty.Name} is {componentPropertyType} item ? item : default({componentPropertyType})";
}
else
{
Expand Down Expand Up @@ -108,13 +109,13 @@ internal static GeneratedPropertyInfo[] GetEventCallbackProperties(GeneratedType
var componentEventName = $"{propertyInfo.Name}Changed";
var generatedPropertyInfo = new GeneratedPropertyInfo(
containingType,
"PropertyChanged",
containingType.GetTypeNameAndAddNamespace(componentType),
componentEventName,
GetEventCallbackType(containingType, null, propertyInfo),
GeneratedPropertyKind.EventCallback);
var generatedPropertyInfo = new GeneratedPropertyInfo(
containingType,
"PropertyChanged",
containingType.GetTypeNameAndAddNamespace(componentType),
componentEventName,
GetEventCallbackType(containingType, null, propertyInfo),
GeneratedPropertyKind.EventCallback);
generatedPropertyInfo._bindedProperty = propertyInfo;
generatedPropertyInfo._eventHandlerType = (INamedTypeSymbol)eventInfo.Type;
Expand All @@ -128,35 +129,35 @@ internal static GeneratedPropertyInfo[] GetEventCallbackProperties(GeneratedType
{
var isBindEvent = IsBindEvent(eventInfo, out var bindedProperty);
if (isBindEvent && IsRenderFragmentPropertySymbol(containingType, bindedProperty))
return null;
if (isBindEvent && IsRenderFragmentPropertySymbol(containingType, bindedProperty))
return null;
var eventCallbackName = isBindEvent ? $"{bindedProperty.Name}Changed" : GetEventCallbackName(eventInfo);
var eventCallbackName = isBindEvent ? $"{bindedProperty.Name}Changed" : GetEventCallbackName(eventInfo);
var generatedPropertyInfo = new GeneratedPropertyInfo(
containingType,
eventInfo.Name,
containingType.GetTypeNameAndAddNamespace(componentType),
eventCallbackName,
GetEventCallbackType(containingType, eventInfo, bindedProperty),
GeneratedPropertyKind.EventCallback);
var generatedPropertyInfo = new GeneratedPropertyInfo(
containingType,
eventInfo.Name,
containingType.GetTypeNameAndAddNamespace(componentType),
eventCallbackName,
GetEventCallbackType(containingType, eventInfo, bindedProperty),
GeneratedPropertyKind.EventCallback);
generatedPropertyInfo._bindedProperty = bindedProperty;
generatedPropertyInfo._eventHandlerType = (INamedTypeSymbol)eventInfo.Type;
return generatedPropertyInfo;
})
generatedPropertyInfo._bindedProperty = bindedProperty;
generatedPropertyInfo._eventHandlerType = (INamedTypeSymbol)eventInfo.Type;
return generatedPropertyInfo;
})
.Where(e => e != null);

return propertyChangedEvents.Concat(inferredEvents).ToArray();
}

private static string GetEventCallbackType(GeneratedTypeInfo containingType, IEventSymbol eventInfo, IPropertySymbol bindedProperty)
private static string GetEventCallbackType(GeneratedTypeInfo containingType, IEventSymbol eventInfo, IPropertySymbol bindedProperty)
{
if (bindedProperty != null)
{
if (bindedProperty != null)
{
var typeName = GetComponentPropertyTypeName(bindedProperty, containingType);
return $"EventCallback<{typeName}>";
}
var typeName = GetComponentPropertyTypeName(bindedProperty, containingType);
return $"EventCallback<{typeName}>";
}

var eventArgType = GetEventArgType(eventInfo.Type);
if (eventArgType.Name != nameof(EventArgs))
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorBindings.Maui/Elements/CarouselView.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected override void HandleParameter(string name, object value)
{
void NativeControlCurrentItemChanged(object sender, MC.CurrentItemChangedEventArgs e)
{
var value = (T)NativeControl.CurrentItem;
var value = NativeControl.CurrentItem is T item ? item : default(T);
CurrentItem = value;
InvokeEventCallback(CurrentItemChanged, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorBindings.Maui/Elements/Picker.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.SelectedItem))
{
var value = (T)NativeControl.SelectedItem;
var value = NativeControl.SelectedItem is T item ? item : default(T);
SelectedItem = value;
InvokeEventCallback(SelectedItemChanged, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.SelectedItem))
{
var value = (T)NativeControl.SelectedItem;
var value = NativeControl.SelectedItem is T item ? item : default(T);
SelectedItem = value;
InvokeEventCallback(SelectedItemChanged, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.SelectedItem))
{
var value = (T)NativeControl.SelectedItem;
var value = NativeControl.SelectedItem is T item ? item : default(T);
SelectedItem = value;
InvokeEventCallback(SelectedItemChanged, value);
}
Expand Down

0 comments on commit ffd7b97

Please sign in to comment.