Xamarin表单没有为' Sku'找到的属性,可绑定属性或事件,或者值和属性

时间:2018-01-24 22:35:49

标签: 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
        }
    }

这与我为ContentViews创建BindableProperties以及您拥有的内容完全相同。

在我的xaml引用此控制器时,我有:

<local:AddToCartButton Text="Add to Cart" Style="{ DynamicResource SkuAddToCart }" Sku="{Binding Sku}" />

这一行导致我的构建失败,错误如下: 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....

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

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

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

1 个答案:

答案 0 :(得分:7)

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

public static readonly BindableProperty SkuPoperty = BindableProperty.Create(nameof(Sku), typeof(ApiModels.SkuDetailModel), typeof(AddToCartButton));

SkuPoperty =&gt; SkuProperty

编辑: 详细说明,Xamarin需要具有PropertyName Property 命名约定: Creating a bindable property