android - Xamarin:检索项目 CardView 的父级时出错

标签 android xamarin xamarin.forms xamarin-studio

设置

我对 Xamarin 非常陌生,我正在尝试遵循官方文档并实现 Lollipop Material Design Style在虚拟应用程序中。

我已经添加了该存储库中出现的资源:

enter image description here

(事实证明,values-v21 是正确的文件夹名称,与存储库中的名称不同)

然后我还从 SDK 管理器安装了整个 Android API 21 文件夹,并安装了 NuGet Xamarin.Android.Support.v7.AppCompat ,它正确显示在我的 packages.config AFAIK 中:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Xamarin.Android.Support.v4" version="23.0.1.1" targetFramework="MonoAndroid50" />
  <package id="Xamarin.Android.Support.v7.AppCompat" version="23.0.1.1" targetFramework="MonoAndroid50" />
</packages>

错误

但是当我点击播放(以 API 21 作为目标的 Debug模式)时,我收到以下三个错误:

Build FAILED.
Errors:

/Users/pegasus/Projects/DistraeMe/DistraeMe/DistraeMe.csproj (Build) ->
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets (_UpdateAndroidResgen target) ->

    Resources/values-v21/styles.xml(2): error APT0000: Error retrieving parent for item: No resource found that matches the given name 'CardView'.
    Resources/values-v21/styles.xml(2): error APT0000: No resource found that matches the given name: attr 'cardBackgroundColor'.
    Resources/values-v21/styles.xml(2): error APT0000: No resource found that matches the given name: attr 'cardCornerRadius'.

这是从存储库粘贴的 styles.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <!-- Define a custom theme that derives from the light Material Theme -->
  <style name="MyCustomTheme" parent="android:Theme.Material.Light">

    <!-- Override the app bar color -->
    <item name="android:colorPrimary">@color/my_blue</item>

    <!-- Override the color of UI controls -->
    <item name="android:colorAccent">@color/my_purple</item>

  </style>

  <!-- Theme an individual view: -->
  <style name="CardView.MyBlue">

    <!-- Change the background color to Xamarin blue: -->
    <item name="cardBackgroundColor">@color/my_blue</item>

    <!-- Make the corners very round: -->
    <item name="cardCornerRadius">18dp</item>
   </style>
</resources>

我没有正确链接这些资源吗?我做错了什么?

最佳答案

需要检查一些可能导致问题的事项:

1) 如果您使用 AppCompat 控件/Activity ,请使用 Theme.AppCompat.Light 而不是 Theme.Material.Light

2) 您的 Activity 是否继承自 AppCompatActivity

3) 设置CardView样式的parent为parent="CardView"

希望有帮助!

关于android - Xamarin:检索项目 CardView 的父级时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086379/

相关文章:

visual-studio - UWP 应用证书错误(错误代码 0x800B0109)

android - Xamarin.Forms Android AOT 支持

android - 如何在 android 中水平居中对齐 Activity 与对话框主题

java - 尝试在 Android 中发送电子邮件而无需用户干预时出现主线程网络异常

c# - Xamarin iOS - 模拟器 View 不刷新

ios - UILabel 剪切文本顶部

c# - Xamarin.Forms 中的多个阶梯 slider

xamarin - 仅创建一个新的 Xamarin Forms 内容页面 Xaml 会使项目无法编译,因为 ContentPageViewModel 未知

android - 进度条不动

java - Android 将 ImageView 拖放到另一个 ImageView(或布局)中