Nathan Sokalski
Nathan Sokalski

Reputation: 103

Regenerating AndroidResource(s) For .NET for Android Using Visual Studio 2022

I am working on converting a Xamarin.Android app to .NET for Android. I have created an entirely new Solution & Project in Visual Studio 2022 & I used the Android Application Project Template. Visual Studio 2022 creates the following file:

_Microsoft.Android.Resource.Designer.cs

This file contains the Resource class defined as follows:

public partial class Resource : _Microsoft.Android.Resource.Designer.ResourceConstant{}

As you can see, this class is completely empty (it does not contain any of the constants for the Resources in my Project). The declaration is declared as partial, so I don't know if there is some other file that is also generated, but Visual Studio 2022 claims that Resource does not have definitions for Layout, Style, Id, etc. Sometimes (although I don't know what things are causing it) the Resources are regenerated, but once I do anything like Rebuild or close & reopen Visual Studio 2022, they get removed and then it takes a long time before it decides to regenerate them (whatever it is that causes it). I looked at the following page:

https://devblogs.microsoft.com/dotnet/android-resource-designer-dotnet-8/

But it still doesn't give a way to regenerate the Resources. What is the problem? How can I force Visual Studio 2022 to regenerate the Resources in .NET for Android?

Upvotes: 0

Views: 171

Answers (3)

user2153142
user2153142

Reputation: 804

Well, you could look at it this way. Using fragments when using fragments and multiple activities was a real pain. Think Fragment transactions - much pain. Therefore, the NavigationComponent was developed - no more pain. I hate even thinking back to those days. One other thing you've probably never used is a ConstraintLayout. It replaces the need for complex LinearLayouts. The Xamarin.Android Designer has not been updated in the last 4 or 5 years and will soon be removed from VS 2022. Therefore, it is probably time for you to install Android Studio because AS makes it very easy to set up a complex layout using a ConstraintLayout. Once done, you can copy/paste your new layout into a new empty Xamarin.Android XML file and build it. There are more sophisticated/efficient ways of getting it into VS, which I can expand on.

It might be an idea to take this stuff to email because I doubt that SO appreciates these types of answers.

Upvotes: 0

user2153142
user2153142

Reputation: 804

The NavigationComponent is considered the modern way to develop Android apps using Views, as compared to Compose. I started with it in late 2020 when I converted Google's Navigation Codelab to C# using Xamarin.Android. The codelab introduced the idea of a single Activity and Fragments for each screen, along with what they call a navigation graph or NavGraph. Building an app with multiple activities was already considered more complex than necessary. The idea of a small single activity with a separate fragment for each screen was much more straightforward, and all this is achieved without a fragment transaction in sight because the NavComponent does all that automatically. It is much more than a new way of moving between activities

You can start by following the original Google NavigationCode and using my Xamarin.Android codelab to see how I converted it. See the ReadMe for links, etc.

After converting their codelab, I started on the NavigationGraph series. The early ones included a Word document, and as I became more familiar with Github, I started to include READMEs instead of Word docs. With each NavigationGraph, I introduced new concepts and complexities and revised what I had coded previously.

It won't do any harm to go through each of them, NavigationGraph1 through NavigationGraph7. I think the first one I converted to .Net7 was NavigationGraph6Net7, and then it moved on to .Net8. I'm just about done with NavigationGraph9Net9, a new one for Android 15, concentrating on the new Edge-To-Edge features of Android 15, which means backward compatible Edge-To-Edge for Android devices 10 through 15. You'll especially like this one; I've included a new fragment with a RecyclerView. I can't believe I never included a RecyclerView previously, as my main app has roughly 34 fragments, and I think there are about 20 of those that have a RecyclerView.

I think it is fair to say that if you go through all of them, you'll find it to be a reasonable tutorial for the NavigationComponent.

You may find it easier if you stick with Xamarin.Android and Android 14 while you start converting your app to the NavigationComponent. When you are comfortable with it, you only then need to read up on how I converted, say, NavigationGraph6 to NavigationGraph6Net7 or NavigationGraph7 to NavigationGraph7Net7. If you follow those suggestions, you'll make the change from X.A. to Net9 more readily. My email is on https://github.com/gmck, so feel free to contact me if you need to.

Upvotes: 1

user2153142
user2153142

Reputation: 804

@Nathan Sokalski, you sure are running into problems. I presume you're trying to create a net8.0-android app. Have you successfully built/deployed and run the standard app built from the template Android Application? The main point of the standard app is that it shows you how the new proj files are created.

If you did that successfully, what were your next steps in bringing in your code, especially your resources? The easiest way to do it before you add any code is to delete the contents of each resource folder. Folder by folder, and then use add existing item, getting the items from your existing project folder into the same folder name in the new app. That way, you should be able to build the app without error.

If you need a new folder, create it first and then do the same thing.

Build it after adding all the contents of one of your resource folders, one at a time. If you get an error, it should be obvious what went wrong.

There are faster ways of doing it, but doing it one folder at a time is the safest way when doing it for the first time.

What version of VS are you using? - the latest is 17.11.5

Have you followed the instructions for dotnet workload install android. See the readme and the links in NavigationGraph8Net8

If your existing app is a multi-activity app, just forget it. Learn how to use the NavigationComponent before you even attempt to move it to .net.

Upvotes: 1

Related Questions