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

Nathan Sokalski 4,111 Reputation points
2024-10-29T22:43:06.9933333+00:00

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, 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?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,142 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,542 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-10-30T01:41:05.6233333+00:00

    Hello,

    The Resource file is described in detail in the AboutResource.txt file of the .Net For Android template program. You can refer to it.

    
    Images, layout descriptions, binary blobs and string dictionaries can be included 
    
    in your application as resource files.  Various Android APIs are designed to 
    
    operate on the resource IDs instead of dealing with images, strings or binary blobs 
    
    directly.
    
     
    
    For example, a sample Android app that contains a user interface layout (main.xml),
    
    an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) 
    
    would keep its resources in the "Resources" directory of the application:
    
     
    
    Resources/
    
        drawable/
    
            icon.png
    
     
    
        layout/
    
            main.xml
    
     
    
        values/
    
            strings.xml
    
     
    
    In order to get the build system to recognize Android resources, set the build action to
    
    "AndroidResource".  The native Android APIs do not operate directly with filenames, but 
    
    instead operate on resource IDs.  When you compile an Android application that uses resources, 
    
    the build system will package the resources for distribution and generate a class called "Resource" 
    
    (this is an Android convention) that contains the tokens for each one of the resources 
    
    included. For example, for the above Resources layout, this is what the Resource class would expose:
    
     
    
    public class Resource {
    
        public class Drawable {
    
            public const int icon = 0x123;
    
        }
    
     
    
        public class Layout {
    
            public const int main = 0x456;
    
        }
    
     
    
        public class Strings {
    
            public const int first_string = 0xabc;
    
            public const int second_string = 0xbcd;
    
        }
    
    }
    
     
    
    You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or 
    
    Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string 
    
    to reference the first string in the dictionary file values/strings.xml.
    
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.