in .net anroid there is "The style on this component requires your app theme to be Theme.AppCompat"?

mc 5,186 Reputation points
2024-02-03T01:56:16.2966667+00:00
  1. create simple empty Android Application
  2. add the code var view=new NavigationRailView(this);setContentView(view);
  3. run it
  4. you will get the error: the style on this component requires your app theme to be Theme.AppCompat

how to add the view?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,013 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 49,386 Reputation points Microsoft External Staff
    2024-02-05T02:57:32.8066667+00:00

    Hello,

    The style on this component requires your app theme to be Theme.AppCompat

    NavigationRailView is a kind of Material control. The Material control is used in the xml layout, so the android:theme style of the Application or activity must be Theme.AppCompat or its sub-styles.

    Solution:

    Apply the theme in your Android project, or you could implement your own theme as follows:

    <style name="MyTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
     ... <!--Add the colors of the following three names (the color value is arbitrary), and the style of MyTheme satisfies Theme.AppCompa, and you could solve the above problem--> 
        <item name="colorPrimary">#ffff00</item>
        <item name="colorPrimaryDark">#ff00ff</item> 
        <item name="colorAccent">#ff0000</item> 
    </style>
    

    After that you need to add the style to the AndroidManifest.xml.

    As a complement, NavigationRailView often needs to be used in combination with other components, so it is not recommended to create objects directly in C#. Please refer to NavigationRailView for more details.

    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.

    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.