Skip to content

Commit

Permalink
[templates] fix broken/wrong item template contents (#7366)
Browse files Browse the repository at this point in the history
After working on #7349, I found some really odd behavior if you name
an Android layout, `my_cool_layout.xml`.

VS appears to generate:

    <?xml version="1.0" encoding="utf-8"?>
    <Linearmy_cool_layout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:my_cool_layout_width="match_parent"
        android:my_cool_layout_height="match_parent">

    </Linearmy_cool_layout>

This is completely broken! It appears that anywhere the word `Layout`
was replaced with `my_cool_layout`. I also get the same behavior with
`dotnet new android-layout -n my_cool_layout`.

To solve this issue, I made the default name `Layout1`, so that
`<LinearLayout/>` and `android:layout_width`, are not replaced.

Now I get a `my_cool_layout.xml` with the contents:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>

Secondly, the `android-activity` template if you run:

    dotnet new android-activity -n MyActivity

It will generate:

    [Activity(Label = "@string/app_name", MainLauncher = true)]
    public class MyActivity : Activity

Let's replace the label with the name passed in, and remove
`MainLauncher=true`, so we instead get:

    [Activity(Label = "MyActivity")]
    public class MyActivity : Activity

I think this will prevent confusion with multiple app launchers, etc.
  • Loading branch information
jonathanpeppers authored Sep 12, 2022
1 parent 34986b9 commit 93411cf
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AndroidApp1;

[Activity(Label = "@string/app_name", MainLauncher = true)]
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
},
"identity": "Microsoft.Android.AndroidLayout",
"shortName": "android-layout",
"sourceName": "Layout",
"sourceName": "Layout1",
"primaryOutputs": [
{ "path": "Layout.xml" }
{ "path": "Layout1.xml" }
],
"defaultName": "Layout"
"defaultName": "Layout1"
}

0 comments on commit 93411cf

Please sign in to comment.