Xamarin:检索CardView的父项时出错

问题描述 投票:2回答:2

设置

我对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作为目标的调试模式时,出现了这三个错误:

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>

我是否无法正确链接这些资源?我在做什么错?

android xamarin xamarin.forms xamarin-studio
2个回答
2
投票

可能要引起问题的几件事:

1)如果使用的是AppCompat控件/活动,请使用Theme.AppCompat.Light代替Theme.Material.Light

2)您的活动是否继承自AppCompatActivity

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

希望有帮助!


0
投票

截至2019年,我收到了相同的错误消息;

error APT0000: Error retrieving parent for item: No resource found that matches the given name 'CardView'.
error APT0000: No resource found that matches the given name: attr 'cardBackgroundColor'.
error APT0000: No resource found that matches the given name: attr 'cardCornerRadius'.

解决方案:我只需要添加nuget包Xamarin.Android.Support.v7.CardView

© www.soinside.com 2019 - 2024. All rights reserved.