diff --git a/src/BlazorBindings.Maui.ComponentGenerator/GeneratedPropertyInfo.EventCallback.cs b/src/BlazorBindings.Maui.ComponentGenerator/GeneratedPropertyInfo.EventCallback.cs index 6f348788..252f1587 100644 --- a/src/BlazorBindings.Maui.ComponentGenerator/GeneratedPropertyInfo.EventCallback.cs +++ b/src/BlazorBindings.Maui.ComponentGenerator/GeneratedPropertyInfo.EventCallback.cs @@ -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 { @@ -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; @@ -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)) diff --git a/src/BlazorBindings.Maui/Elements/CarouselView.generated.cs b/src/BlazorBindings.Maui/Elements/CarouselView.generated.cs index ebea5f68..37fdc10c 100644 --- a/src/BlazorBindings.Maui/Elements/CarouselView.generated.cs +++ b/src/BlazorBindings.Maui/Elements/CarouselView.generated.cs @@ -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); } diff --git a/src/BlazorBindings.Maui/Elements/Picker.generated.cs b/src/BlazorBindings.Maui/Elements/Picker.generated.cs index da58101f..ad67b9a1 100644 --- a/src/BlazorBindings.Maui/Elements/Picker.generated.cs +++ b/src/BlazorBindings.Maui/Elements/Picker.generated.cs @@ -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); } diff --git a/src/BlazorBindings.Maui/Elements/SearchHandler.generated.cs b/src/BlazorBindings.Maui/Elements/SearchHandler.generated.cs index 5a8c817d..e4243bff 100644 --- a/src/BlazorBindings.Maui/Elements/SearchHandler.generated.cs +++ b/src/BlazorBindings.Maui/Elements/SearchHandler.generated.cs @@ -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); } diff --git a/src/BlazorBindings.Maui/Elements/SelectableItemsView.generated.cs b/src/BlazorBindings.Maui/Elements/SelectableItemsView.generated.cs index be3617a8..da1b4a9c 100644 --- a/src/BlazorBindings.Maui/Elements/SelectableItemsView.generated.cs +++ b/src/BlazorBindings.Maui/Elements/SelectableItemsView.generated.cs @@ -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); }