Skip to content

Commit 74b366d

Browse files
Moved AOT trimming code changes
1 parent 46000ba commit 74b366d

29 files changed

+277
-140
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.Maui.Controls.Internals;
2+
3+
/// <summary>
4+
/// Helper class for creating bindings in .NET MAUI applications.
5+
/// </summary>
6+
public static class BindingHelper
7+
{
8+
/// <summary>
9+
/// Creates a typed binding for a given source and its properties.
10+
/// </summary>
11+
/// <typeparam name="TSource">The type of the binding source.</typeparam>
12+
/// <typeparam name="TProperty">The type of the property to bind.</typeparam>
13+
/// <param name="propertyName">The name of the property to bind to.</param>
14+
/// <param name="getter">A function to get the value of the property from the source.</param>
15+
/// <param name="setter">An optional action to set the value of the property on the source.</param>
16+
/// <param name="mode">The binding mode that determines the data flow direction.</param>
17+
/// <param name="converter">An optional value converter for the binding.</param>
18+
/// <param name="converterParameter">An optional parameter used by the converter.</param>
19+
/// <param name="source">The source object for binding.</param>
20+
/// <returns>A <see cref="BindingBase"/> representing the created binding.</returns>
21+
public static BindingBase CreateBinding<TSource, TProperty>(
22+
string propertyName,
23+
Func<TSource, TProperty> getter,
24+
Action<TSource, TProperty>? setter = null,
25+
BindingMode mode = BindingMode.Default,
26+
IValueConverter? converter = null,
27+
object? converterParameter = null,
28+
object? source = null)
29+
{
30+
return new TypedBinding<TSource, TProperty>(
31+
getter: source => (getter(source), true),
32+
setter,
33+
handlers:
34+
[
35+
new(static source => source, propertyName),
36+
])
37+
{
38+
Converter = converter,
39+
ConverterParameter = converterParameter,
40+
Mode = mode,
41+
Source = source,
42+
};
43+
}
44+
}

maui/src/Calendar/Helper/CalendarViewHelper.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,24 +1619,27 @@ internal static string GetLanguage(string calendarIdentifier)
16191619
/// <returns>A calendar instance.</returns>
16201620
internal static Globalization.Calendar GetCalendar(string calendarIdentifier)
16211621
{
1622-
var type = Type.GetType("System.Globalization." + calendarIdentifier + "Calendar");
1623-
if (type != null)
1622+
var calendarTypes = new Dictionary<string, Type>
1623+
{
1624+
{ "Gregorian", typeof(GregorianCalendar) },
1625+
{ "Hijri", typeof(HijriCalendar) },
1626+
{ "Persian", typeof(PersianCalendar) },
1627+
{ "ThaiBuddhist", typeof(ThaiBuddhistCalendar) },
1628+
{ "Taiwan", typeof(TaiwanCalendar) },
1629+
{ "UmAlQura", typeof(UmAlQuraCalendar) },
1630+
{ "Korean", typeof(KoreanCalendar) }
1631+
};
1632+
1633+
if (calendarTypes.TryGetValue(calendarIdentifier, out var type))
16241634
{
1625-
var calendar = Activator.CreateInstance(type) as Globalization.Calendar;
1626-
if (calendar != null)
1635+
if (Activator.CreateInstance(type) is Globalization.Calendar calendar)
16271636
{
16281637
return calendar;
16291638
}
1630-
else
1631-
{
1632-
return CultureInfo.CurrentUICulture.DateTimeFormat.Calendar;
1633-
}
1634-
}
1635-
else
1636-
{
1637-
// If calendar identifier is specified wrongly, then default calendar will be used.
1638-
return CultureInfo.CurrentUICulture.DateTimeFormat.Calendar;
16391639
}
1640+
1641+
// If calendar identifier is specified wrongly, then default calendar will be used.
1642+
return CultureInfo.CurrentUICulture.DateTimeFormat.Calendar;
16401643
}
16411644

16421645
/// <summary>

maui/src/Charts/Axis/CategoryAxis.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace Syncfusion.Maui.Toolkit.Charts
45
{
@@ -84,6 +85,7 @@ internal void GroupData()
8485
}
8586
}
8687

88+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
8789
internal override void GenerateVisibleLabels()
8890
{
8991
if (VisibleRange.IsEmpty)

maui/src/Charts/Axis/DateTimeAxis.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using Microsoft.Maui.Graphics;
34

45
namespace Syncfusion.Maui.Toolkit.Charts
@@ -182,6 +183,7 @@ internal void OnMinMaxChanged()
182183
}
183184
}
184185

186+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
185187
internal override void GenerateVisibleLabels()
186188
{
187189
SmallTickPoints.Clear();

maui/src/Charts/Axis/LogarithmicAxis.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Syncfusion.Maui.Toolkit.Charts
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Syncfusion.Maui.Toolkit.Charts
24
{
35
public partial class LogarithmicAxis : RangeAxisBase
46
{
@@ -125,6 +127,7 @@ internal override DoubleRange AddDefaultRange(double start)
125127
return new DoubleRange(start, start + 1);
126128
}
127129

130+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
128131
internal override void GenerateVisibleLabels()
129132
{
130133
if (VisibleRange.IsEmpty)

maui/src/Charts/Axis/NumericalAxis.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Syncfusion.Maui.Toolkit.Charts
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Syncfusion.Maui.Toolkit.Charts
24
{
35
public partial class NumericalAxis : RangeAxisBase
46
{
@@ -110,6 +112,7 @@ internal override void RaiseCallBackActualRangeChanged()
110112
#endregion
111113

112114
#region Internal Methods
115+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
113116
internal override void GenerateVisibleLabels()
114117
{
115118
var actualLabels = VisibleLabels;

maui/src/Charts/Behaviors/ChartTooltipBehavior.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ void GenerateTooltip(IChart chart, float x, float y, bool canAnimate)
694694
tooltip.BindingContext = tooltipInfo;
695695
tooltip.Duration = Duration;
696696
tooltip.Position = tooltipInfo.Position;
697-
tooltip.SetBinding(SfTooltip.BackgroundProperty, nameof(TooltipInfo.Background));
697+
tooltip.SetBinding(SfTooltip.BackgroundProperty,
698+
BindingHelper.CreateBinding(nameof(TooltipInfo.Background), getter: static(TooltipInfo tooltipInfo1) => tooltipInfo1.Background));
698699
tooltip.Content = GetTooltipTemplate(tooltipInfo);
699700
tooltip.Show(seriesBounds, tooltipInfo.TargetRect, canAnimate);
700701
}

maui/src/Charts/Behaviors/ChartTrackballBehavior.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,18 +1119,25 @@ void GenerateTrackballGroupedLabel()
11191119
{
11201120
LineBreakMode = LineBreakMode.NoWrap
11211121
};
1122-
label.SetBinding(Label.TextProperty, new Binding() { Path = "Label" });
1122+
label.SetBinding(Label.TextProperty,
1123+
BindingHelper.CreateBinding(nameof(TrackballPointInfo.Label), getter: static(TrackballPointInfo info) => info.Label));
11231124
label.VerticalOptions = LayoutOptions.Fill;
11241125
label.HorizontalOptions = LayoutOptions.Fill;
11251126
label.VerticalTextAlignment = TextAlignment.Center;
11261127
label.HorizontalTextAlignment = TextAlignment.Center;
1127-
label.SetBinding(Label.TextColorProperty, new Binding() { Path = "LabelStyle.TextColor" });
1128-
label.SetBinding(Label.BackgroundProperty, new Binding() { Path = "LabelStyle.Background" });
1129-
label.SetBinding(Label.FontAttributesProperty, new Binding() { Path = "LabelStyle.FontAttributes" });
1130-
label.SetBinding(Label.FontFamilyProperty, new Binding() { Path = "LabelStyle.FontFamily" });
1131-
label.SetBinding(Label.FontSizeProperty, new Binding() { Path = "LabelStyle.FontSize" });
1132-
label.SetBinding(Label.MarginProperty, new Binding() { Path = "LabelStyle.Margin" });
1133-
return label;
1128+
label.SetBinding(Label.TextColorProperty,
1129+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.TextColor), getter: static(ChartLabelStyle labelStyle) => labelStyle.TextColor, source: LabelStyle));
1130+
label.SetBinding(Label.BackgroundProperty,
1131+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.Background), getter: static (ChartLabelStyle labelStyle) => labelStyle.Background, source: LabelStyle));
1132+
label.SetBinding(Label.FontAttributesProperty,
1133+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.FontAttributes), getter: static (ChartLabelStyle labelStyle) => labelStyle.FontAttributes, source: LabelStyle));
1134+
label.SetBinding(Label.FontFamilyProperty,
1135+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.FontFamily), getter: static (ChartLabelStyle labelStyle) => labelStyle.FontFamily, source: LabelStyle));
1136+
label.SetBinding(Label.FontSizeProperty,
1137+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.FontSize), getter: static (ChartLabelStyle labelStyle) => labelStyle.FontSize, source: LabelStyle));
1138+
label.SetBinding(Label.MarginProperty,
1139+
BindingHelper.CreateBinding(nameof(ChartLabelStyle.Margin), getter: static (ChartLabelStyle labelStyle) => labelStyle.Margin, source: LabelStyle));
1140+
return label;
11341141
});
11351142

11361143
BindableLayout.SetItemTemplate(layout, labelTemplate);

maui/src/Charts/Layouts/ChartSeriesView.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ internal SeriesView(ChartSeries chartSeries, IChartPlotArea plotArea)
2121
{
2222
_series = chartSeries;
2323
_chartPlotArea = plotArea;
24-
SetBinding(IsVisibleProperty, new Binding() { Source = chartSeries, Path = nameof(ChartSeries.IsVisible) });
24+
SetBinding(IsVisibleProperty,
25+
BindingHelper.CreateBinding(nameof(ChartSeries.IsVisible), getter: static(ChartSeries series) => series.IsVisible, source: chartSeries));
2526
}
2627

2728
#endregion

maui/src/Charts/Layouts/DataLabelLayout.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public DataLabelLayout(IDataTemplateDependent source)
2222
IsClippedToBounds = true;
2323
_source = source;
2424
BindableLayout.SetItemTemplate(this, GetItemTemplate());
25-
SetBinding(IsVisibleProperty, new Binding(nameof(IDataTemplateDependent.IsVisible)) { Source = _source });
25+
SetBinding(IsVisibleProperty,
26+
BindingHelper.CreateBinding(nameof(IDataTemplateDependent.IsVisible), getter: static(IDataTemplateDependent dataTemplateDependent) => dataTemplateDependent.IsVisible, source: _source));
2627
}
2728

2829
DataTemplate GetItemTemplate()
@@ -34,17 +35,20 @@ DataTemplate GetItemTemplate()
3435
{
3536
Bindings =
3637
[
37-
new Binding(nameof(ChartDataLabel.XPosition)),
38-
new Binding(nameof(ChartDataLabel.YPosition)),
39-
],
38+
BindingHelper.CreateBinding(nameof(ChartDataLabel.XPosition), getter: static (ChartDataLabel label) => label.XPosition),
39+
BindingHelper.CreateBinding(nameof(ChartDataLabel.YPosition), getter: static (ChartDataLabel label) => label.YPosition)
40+
],
4041
Converter = new TemplateVisibilityConverter(),
4142
};
4243

4344
item.SetBinding(DataLabelItemView.IsVisibleProperty, binding);
44-
item.SetBinding(DataLabelItemView.XPositionProperty, new Binding(nameof(ChartDataLabel.XPosition)));
45-
item.SetBinding(DataLabelItemView.YPositionProperty, new Binding(nameof(ChartDataLabel.YPosition)));
46-
item.SetBinding(SfTemplatedView.ItemTemplateProperty, new Binding(nameof(IDataTemplateDependent.LabelTemplate)) { Source = _source });
47-
return item;
45+
item.SetBinding(DataLabelItemView.XPositionProperty,
46+
BindingHelper.CreateBinding(nameof(ChartDataLabel.XPosition), getter: static (ChartDataLabel label) => label.XPosition));
47+
item.SetBinding(DataLabelItemView.YPositionProperty,
48+
BindingHelper.CreateBinding(nameof(ChartDataLabel.YPosition), getter: static (ChartDataLabel label) => label.YPosition));
49+
item.SetBinding(SfTemplatedView.ItemTemplateProperty,
50+
BindingHelper.CreateBinding(nameof(IDataTemplateDependent.LabelTemplate), getter: static (IDataTemplateDependent source) => source.LabelTemplate, source: _source));
51+
return item;
4852
});
4953
}
5054
}

0 commit comments

Comments
 (0)