2016-08-16 35 views
3

我想使用Xamarin和Visual Studio創建一個包含Material Design的應用程序。我想用v7 AppCompat庫來實現這個功能,所以我的應用程序在舊設備上運行良好。Xamarin - 檢索項目的父項時出錯:未找到與給定名稱相匹配的資源'Theme.AppCompat.Light.DarkActionBar'

我跟着這個教程:

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

我發現這個問題是與Visual Studio設計,因爲它可以正常運行我的手機上。於是我問了一個新問題:Xamarin.Android, Visual Studio - Designer doesn't work with v7 AppCompat library

在這裏,您可以下載我的項目:

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

(對不起,我英文不好,隨時糾正我)

+0

爲了更好的理解問題,粘貼代碼。 –

+1

你在哪裏應用主題?在你的'AndroidManifest.xml'中?嘗試核彈'bin/obj'文件夾,清理項目和重建。請確保你的'v7 AppCompat' NuGet包被適當引用。 –

+0

@JonDouglas我在'AndroidManifest.xml'中應用主題。我加入了'bin'和'obj'文件夾。我清理並重建了我的物體。我得到了和以前一樣的錯誤:'錯誤檢索項目的父項:找不到與給定名稱'Theme.AppCompat.Light.DarkActionBar'.'匹配的資源(在我的問題的頂部有更多細節)。當我重新編譯時,出現這個錯誤:'1> java.lang.UnsupportedClassVersionError:com/android/dx/command/Main:Unsupported major.minor version 52.0'(更多細節請參閱我的問題的編輯(2) 。所以不幸的是它不起作用:( – qwertytrewq

回答

相關問題