Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[workload] setup C# implicit global usings from MSBuild #3267

Merged
merged 1 commit into from
Nov 11, 2021

Commits on Nov 11, 2021

  1. [workload] setup C# implicit global usings from MSBuild

    Context: dotnet#3018
    
    PR dotnet#3108 was a good start in enabling implicit C# global usings.
    However, there were a couple of issues to work through:
    
    1. There is a `GlobalUsings.cs` file. Can we do this in MSBuild, so
       it's just there by default? I added:
    
        <ItemGroup Condition=" '$(UseMaui)' == 'true' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') ">
          <!-- %(Sdk) is only here if something later needs to identify these -->
          <Using Include="Microsoft.Extensions.DependencyInjection" Sdk="Maui" />
          <Using Include="Microsoft.Maui" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Controls" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Controls.Hosting" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Controls.Xaml" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Graphics" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Essentials" Sdk="Maui" />
          <Using Include="Microsoft.Maui.Hosting" Sdk="Maui" />
        </ItemGroup>
    
    2. The platform-specific usings were not cleared. So types like
       `Microsoft.Maui.Controls.Application` could conflict with
       `Android.App.Application`.
    
    To solve this, I added a `%(Platform)` metadata in the Android, iOS,
    macOS, MacCatalyst, and tvOS workloads:
    
        <Using Include="Android.App" Platform="Android" />
        <Using Include="Android.Widget" Platform="Android" />
        <Using Include="Android.OS.Bundle" Alias="Bundle" Platform="Android" />
    
    Then in .NET MAUI's MSBuild targets we can do:
    
        <ItemGroup Condition=" '$(UseMaui)' == 'true' and '$(MauiEnablePlatformUsings)' != 'true' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') ">
          <Using Remove="@(Using->HasMetadata('Platform'))" />
        </ItemGroup>
    
    I created `$(MauiEnablePlatformUsings)`, just in case someone ever
    needs to turn this off.
    
    I updated the templates to specify `ImplicitUsings=enable`.
    
    I updated `Controls.Sample.Tests.csproj`, as it was a .NET 6 project.
    We probably can't update the other samples yet, because there are
    Xamarin projects that would not support C# 10 language features.
    jonathanpeppers committed Nov 11, 2021
    Configuration menu
    Copy the full SHA
    c29655b View commit details
    Browse the repository at this point in the history