Xamarin表单没有为'Sku'找到的属性,可绑定属性或事件,或者值和属性之间的不匹配类型

Nie*_*nen 6 xamarin xamarin.forms

在我的xamarin项目中,我有一个自定义按钮控制器:

[XamlCompilation(XamlCompilationOptions.Compile)]
    public class AddToCartButton : Button
    {
        public static readonly BindableProperty SkuPoperty = BindableProperty.Create(nameof(Sku), typeof(ApiModels.SkuDetailModel), typeof(AddToCartButton));

        public ApiModels.SkuDetailModel Sku
        {
            get { return (ApiModels.SkuDetailModel)GetValue(SkuPoperty); }
            set { SetValue(SkuPoperty, value); }
        }

        public AddToCartButton()
        {
            this.Clicked += AddToCartButton_Clicked;
        }

        public AddToCartButton(ApiModels.SkuDetailModel sku)
        {
            this.Sku = sku;
            this.Clicked += AddToCartButton_Clicked;
        }

        private async void AddToCartButton_Clicked(object sender, EventArgs e)
        {
            var response = await Helpers.ApiHelper.CurrentAccess.AddToCart(new List<ApiModels.CartItem>() {
                new ApiModels.CartItem() {
                    ItemCode = Sku.ItemCode,
                    Quantity = 1
                }
            });
            // handle add modal
        }
    }
Run Code Online (Sandbox Code Playgroud)

这与我为ContentViews创建BindableProperties的方式完全相同.

在我的xaml引用这个控制器,我有:

<local:AddToCartButton Text="Add to Cart" Style="{ DynamicResource SkuAddToCart }" Sku="{Binding Sku}" />
Run Code Online (Sandbox Code Playgroud)

这一行导致我的构建失败,错误: Severity Code Description Project File Line Suppression State Error Position 44:44. No property, bindable property, or event found for 'Sku', or mismatching type between value and property....

我似乎无法弄清楚为什么我会得到它.我的所有类型都是一致的.我试图绑定的对象是这样完成的:public partial class SkuView:ContentView {public ApiModels.SkuDetailModel Sku {get; 组; }

        public SkuView(ApiModels.SkuDetailModel sku, string baseUrl, ApiModels.SimpleUser user)
        {
            BindingContext = this;
            Sku = sku;
            InitializeComponent();
Run Code Online (Sandbox Code Playgroud)

根据xaml标头的请求,这是整个xaml文件.

<?xml version="1.0" encoding="UTF-8"?>

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:myApp.Controls"
         x:Class="myApp.Views.Products.SkuView">
<ContentView.Content>
    <StackLayout>
        <Grid Style="{ DynamicResource SkuGrid }">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="50" />
            </Grid.RowDefinitions>

            <StackLayout x:Name="ImageCell" Grid.Row="0" Grid.Column="0"></StackLayout>
            <StackLayout x:Name="ContentCell" Grid.Row="0" Grid.Column="1">
                <Label x:Name="DescriptionLabel" Style="{ DynamicResource SkuDescLabel }" />
                <Label x:Name="ItemCodeLabel" Style="{ DynamicResource SkuItemCode }" />
                <StackLayout x:Name="PricingLayout">
                    <!--<StackLayout Orientation="Horizontal">
                        <Label x:Name="PriceLabel" Style="{ DynamicResource SkuPricing }" />
                        <Label x:Name="PurchaseMultipleLabel" Style="{ DynamicResource SkuUom }" />
                    </StackLayout>-->
                </StackLayout>
            </StackLayout>
            <StackLayout Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
                <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                    <Button Text=" - " Style="{ DynamicResource SkuQtyStepper }" />
                    <Grid Margin="-10, 0">
                        <BoxView Color="DarkGray" Opacity=".6" Margin="0, 5"/>
                        <BoxView Color="White" Margin="2, 7"/>
                        <local:BorderlessEntry Style="{ DynamicResource SkuQtyEntry }" Keyboard="Numeric" Margin="2" HorizontalTextAlignment="Center" />
                    </Grid>
                    <Button Text=" + " Style="{ DynamicResource SkuQtyStepper }" />
                </StackLayout>
                <!--<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                    <Button Text=" - " Style="{ DynamicResource SkuQtyStepper }" />
                    <local:BorderedEntry Style="{ DynamicResource SkuQtyEntry }" Keyboard="Numeric" HeightRequest="20" />
                    <Button Text=" + " Style="{ DynamicResource SkuQtyStepper }" />
                </StackLayout>-->
                <local:AddToCartButton Text="Add to Cart" Style="{ DynamicResource SkuAddToCart }" Sku="{Binding Sku}" />
                <!--<Button Text="Add to Cart" Style="{ DynamicResource SkuAddToCart }" />-->
            </StackLayout>
        </Grid>

        <Grid HeightRequest="1" Style="{ DynamicResource BackgroundMediumGray }" />
    </StackLayout>
</ContentView.Content>
Run Code Online (Sandbox Code Playgroud)

Muh*_*awi 14

在属性名称中,您有一个拼写错误:

public static readonly BindableProperty SkuPoperty = BindableProperty.Create(nameof(Sku), typeof(ApiModels.SkuDetailModel), typeof(AddToCartButton));
Run Code Online (Sandbox Code Playgroud)

SkuPoperty => SkuProperty

编辑:详细说明,Xamarin要求具有PropertyName 属性命名约定: 创建可绑定属性

  • @Greggz实际上,我没有注意到他到处都使用了"Poperty",只是第一个:D和是的,[Xamarin需要一个属性命名约定](https://developer.xamarin.com/guides/xamarin-forms/XAML /可绑定的属性/#Creating_a_Property) (3认同)
  • 这实际上是正确的答案。显然 Xamarin 需要这些属性的特定命名约定。愚蠢的是,尽管我到处都有正确的参考资料。 (2认同)
  • 我将 BindableProperty 设置为私有。它需要公开。 (2认同)