Xamarin - 获取父项错误:找不到与给定名称“Theme.AppCompat.Light.DarkActionBar”匹配的资源。

5
我希望使用Xamarin和Visual Studio创建一个具有Material Design的应用程序。我想使用v7 AppCompat库来实现这一点,以便我的应用程序可以在旧设备上良好运行。
我按照以下教程的步骤进行:https://blog.xamarin.com/android-tips-hello-material-design-v7-appcompat/。当我打开Main.axml文件(在Resources/layout文件夹中)时,会出现一个下拉菜单,您可以在其中选择主题(如下图所示)。然而,当我打开下拉菜单时,我的主题没有显示出来。因此,我认为清理并重建我的项目是个好主意。但是,当我清理项目时,出现了以下错误:
Error retrieving parent for item: No resource found that matches the given
name 'Theme.AppCompat.Light.DarkActionBar'.

No resource found that matches the given name: attr 'colorAccent'.
No resource found that matches the given name: attr 'colorPrimary'.
No resource found that matches the given name: attr 'colorPrimaryDark'.
No resource found that matches the given name: attr 'windowActionBar'.
No resource found that matches the given name: attr 'windowNoTitle'.

我该如何修复它并使我的主题出现在下拉菜单中?
编辑:
以下是代码:
Main.axml
<?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">
<Button
    android:id="@+id/MyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Hello" />
</LinearLayout>

values/styles.xml

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

values-v21

<resources>
  <style name="MyTheme" parent="MyTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace MaterialDesign
{
    [Activity(Label = "MaterialDesign", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        }
    }
}

注意:

当我使用android.support.v7.widget.Toolbar元素时,出现以下错误:android.support.v7.widget.Toolbar元素未声明

编辑(2):

我尝试使用组件管理器下载,使用NuGet命令(Package Manager Console)下载并手动下载dll文件并引用它们,但仍然无法正常工作。 我还删除了\AppData\Local\Xamarin\Android.Support.v7.AppCompat\23.0.1.3文件夹并清除了缓存,但没有成功。

在设计师选项卡中的Main.axml文件中出现警告:

This project contains resources that were not compiled successfully,
rendering might be affected

我发现问题出在Visual Studio Designer上,因为它可以在我的手机上正常运行。所以我提了一个新的问题:Xamarin.Android,Visual Studio - 设计器与v7 AppCompat库不兼容 你可以在这里下载我的项目: https://drive.google.com/file/d/0BxiDy9-wQHvzNFRhaTMzelg1WlU/view?usp=sharing (抱歉英语不好,请随意纠正)

2
你将主题应用在哪里?是在你的 AndroidManifest.xml 文件中吗?试着删除 bin/obj 文件夹,清理你的项目并重新构建。请确保你的 v7 AppCompat NuGet 包被正确引用。 - Jon Douglas
针对“Unsupported major.minor version 52.0”错误,请尝试卸载当前的JDK版本并安装1.8版本。如果您仍然遇到错误,请上传一个示例项目,并在您的帖子中编辑链接。 - Jon Douglas
@JonDouglas,当我重新构建项目时不再出现错误,但是当我清理项目时仍然出现相同的错误。 - qwertytrewq
@JonDouglas 我已经将我的项目上传到Google Drive上了:(问题中的网址) - qwertytrewq
你的项目在我的机器上构建和部署都很好。请确保你只安装了JDK 1.8。删除bin/obj文件夹,然后重新部署你的应用程序。你也可以尝试删除AppCompat库的本地缓存。同时,尝试删除这些文件夹中找到的相应支持库:https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/#automatic - Jon Douglas
显示剩余9条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接