-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LayoutBindings] Fix '[Preserve]' is obsolete warnings (#8529)
Fixes: #7480 Context: e604833 When [Layout Bindings][0] are enabled, a set of `partial` classes will be generated which "mirrors" the `.axml` structure in C#. Consider: dotnet new android -n com.example.codebehind You enable Code-Behind by: 1. Setting the `$(AndroidGenerateLayoutBindings)` MSBuild property to `true`. 2. Using `android:id` on elements within `.axml` files. Consider this patch to the above `dotnet new`: diff --git a/Resources/layout/activity_main.xml b/Resources/layout/activity_main.xml index f949852..c74521c 100644 --- a/Resources/layout/activity_main.xml +++ b/Resources/layout/activity_main.xml @@ -8,6 +8,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" + android:id="@+id/text" android:text="@string/app_text" /> </RelativeLayout> diff --git a/com.example.codebehind.csproj b/com.example.codebehind.csproj index 3fdbcb5..8190f84 100644 --- a/com.example.codebehind.csproj +++ b/com.example.codebehind.csproj @@ -8,5 +8,6 @@ <ApplicationId>com.companyname.com.example.codebehind</ApplicationId> <ApplicationVersion>1</ApplicationVersion> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> + <AndroidGenerateLayoutBindings>true</AndroidGenerateLayoutBindings> </PropertyGroup> </Project> The resulting output contains `$(IntermediateOutputPath)codebehind\Binding.activity_main.g.cs`, which declares a type based on the `.axml` file name, and a mirror of the structure: namespace Binding { // typename based on `activity_main.xml` filename sealed partial class activity_main : global::Xamarin.Android.Design.LayoutBinding { [global::Android.Runtime.PreserveAttribute (Conditional=true)] public activity_main (Activity client, …); // … // `text` is via `android:id="@+id/text"` public TextView text => …; } } The problem is the use of `PreserveAttribute` in the generated code; it's been `[Obsolete]` since e604833 (over two years ago), which means all projects using Layout Bindings and Layout Code-Behind always have [CS0618][1] warnings. Remove the emission of `PreserveAttribute`, so that CS0618 is no longer generated. Additionally, within the generated files disable the [CS1591][2] and [CS8981][3] warnings: * CS1591 is around missing XML documentation comments. These code behind files do not have XML documentation comments, so if/when `$(DocumentationFile)` is set, these will be emitted for the generated code. We do not currently plan on emitting XML documentation comments, so disable this warning. * CS8981 is emitted when a type name consists solely of lowercase characters. As the generated Binding types are based on the filename -- which is frequently all lowercase -- this warning may be emitted. Finally, add `#nullable enable` to `LayoutBinding.cs` and fix the nullable reference type warnings. [0]: https://github.com/xamarin/xamarin-android/blob/2f4e01ec15102dd9cd922cbd833f6482d69512b5/Documentation/guides/LayoutCodeBehind.md [1]: https://learn.microsoft.com/dotnet/csharp/language-reference/compiler-messages/cs0618 [2]: https://learn.microsoft.com/dotnet/csharp/language-reference/compiler-messages/cs1591 [3]: https://learn.microsoft.com/dotnet/csharp/language-reference/compiler-messages/warning-waves#cs8981---the-type-name-only-contains-lower-cased-ascii-characters
- Loading branch information
1 parent
150e1d9
commit 751eb96
Showing
10 changed files
with
86 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.CodeBehindBuildTests"> | ||
<uses-sdk android:minSdkVersion="21" /> | ||
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name"> | ||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters