Skip to content

XamlC should optimize OnPlatform #5583

Open
@eerhardt

Description

@eerhardt

Description

In the .NET Podcasts app, it has the following XAML:

https://github.com/microsoft/dotnet-podcasts/blob/00be1bb25b6940fee77f0e7b711a04dea2358ced/src/Mobile/Controls/HeaderControl.xaml#L9-L32

            <OnPlatform x:TypeArguments="View">
                <On Platform="UWP, macOS">
                    <Grid RowDefinitions="*, auto">
                        <SearchBar x:Name="searchBar"
                                   HorizontalOptions="Start"
                                   WidthRequest="460"
                                   Text="{Binding TextToSearch, Source={x:Reference selfMediaElementView}}"
                                   SearchCommand="{Binding SearchCommand, Source={x:Reference selfMediaElementView}}" />
                        <Label Text="See All Categories"
                               Style="{StaticResource BodyLLabelStyle}"
                               TextColor="{StaticResource Primary}"
                               Grid.Row="1"
                               HorizontalOptions="End"
                               Margin="0,0,30,0"
                               IsVisible="{Binding ShowSearchCategories, Source={x:Reference selfMediaElementView}}"
                               FontSize="16">
                            <Label.GestureRecognizers>
                                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                            </Label.GestureRecognizers>
                        </Label>
                    </Grid>
                </On>
                <On Platform="Android,iOS">
                    <Grid ColumnDefinitions="*,*"
                          Margin="16,12">
                        <Image Source="{AppThemeBinding Light=logo_header_horizontal.png,Dark=logo_color_horizontal_darkmode.png}"
                               HeightRequest="34"
                               WidthRequest="125"
                               VerticalOptions="Center"
                               HorizontalOptions="Start" />
                        <Image Source="search.png"
                               Grid.Column="1"
                               VerticalOptions="Center"
                               HorizontalOptions="End"
                               HeightRequest="20"
                               WidthRequest="20">
                            <Image.GestureRecognizers>
                                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                            </Image.GestureRecognizers>
                        </Image>
                    </Grid>
                </On>
            </OnPlatform>

When building the app for Android, I'm still seeing the controls inside the <On Platform="UWP, macOS"> clause get created and properties set on them. For example, the SearchBar is only used in UWP, macOS, but in my compiled Android app I see:

private void InitializeComponent()
{
	ReferenceExtension referenceExtension = new ReferenceExtension();
	BindingExtension bindingExtension = new BindingExtension();
	ReferenceExtension referenceExtension2 = new ReferenceExtension();
	BindingExtension bindingExtension2 = new BindingExtension();
	SearchBar searchBar = new SearchBar();

This pattern is used in 3 places in the .NET Podcasts app:

  1. The above
  2. The Player control
  3. The ShowDetailPage.

We should optimize this pattern so when building for Android, the other platforms' controls are not created.

cc @StephaneDelcroix @jonathanpeppers

Steps to Reproduce

  1. git clone https://github.com/microsoft/dotnet-podcasts.git
  2. cd dotnet-podcasts/src/Mobile
  3. dotnet build Microsoft.NetConf2021.Maui.csproj -t:Run -c Release -f net6.0-android -r android-arm64
  4. Inspect the IL of HeaderControl.InitializeComponent()

Version with bug

Preview 14 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

All

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions