Xamarin - 检索项目的父项时出错:找不到与给定名称"Theme.AppCompat.Light.DarkActionBar"匹配的资源

qwe*_*ewq 5 c# xml android xamarin.android xamarin

我想使用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'.
Run Code Online (Sandbox Code Playgroud)

如何修复它并让我的主题出现在下拉菜单中?

编辑:

这是代码:

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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++); };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:

此外,当我使用该android.support.v7.widget.Toolbar元素时,我收到以下错误:android.support.v7.widget.Toolbar element is not declared.

EDIT(2):

我尝试使用组件管理器下载它,使用NuGet命令(程序包管理器控制台)并手动下载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
Run Code Online (Sandbox Code Playgroud)

我发现问题出在Visual Studio Designer上,因为它在我的手机上正常运行.所以我问了一个新问题:Xamarin.Android,Visual Studio - Designer不能与v7 AppCompat库一起使用

在这里你可以下载我的项目:

https://drive.google.com/file/d/0BxiDy9-wQHvzNFRhaTMzelg1WlU/view?usp=sharing

(抱歉英文不好,随时纠正我)

qwe*_*ewq 0

它与代码无关。只是 Visual Studio 在撰写本文时不支持自定义视图。

只需使用 Xamarin Studio 或此插件https://visualstudiogallery.msdn.microsoft.com/9f5a516a-f4d0-4228-9d25-d0273abebf33这样您就可以在 Android Studio 中编辑应用程序。

有关更详细的答案,请参阅Xamarin.Android,Visual Studio - 设计器无法使用 v7 AppCompat 库