Xamarin Forms 找不到“Sku"的属性、可绑定属性或事件,或者值和属性之间的类型不匹配 [英] Xamarin Forms No property, bindable property, or event found for 'Sku', or mismatching type between value and property

查看:22
本文介绍了Xamarin Forms 找不到“Sku"的属性、可绑定属性或事件,或者值和属性之间的类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my xamarin project I have a custom button controller:

[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 的方式完全相同.

This is the exact same way I've created BindableProperties for ContentViews and what have you.

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

In my xaml referencing this controller, I have:

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

这一行导致我的构建失败,并出现错误:Severity Code 描述 Project File Line Suppression State错误位置 44:44.没有找到Sku"的属性、可绑定属性或事件,或者值和属性之间的类型不匹配......

This line is causing my builds to fail, with the error: 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{公共 ApiModels.SkuDetailModel Sku { get;放;}

I can't seem to figure out why I'm getting it. All my types are consistent. The object I'm trying to bind is done like this: public partial class SkuView : ContentView { public ApiModels.SkuDetailModel Sku { get; set; }

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

对于 xaml 标头的每个请求,这里是整个 xaml 文件.

Per request for xaml headers, here is the entire xaml file.

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

推荐答案

在属性名称中,您有一个错字:

In the property name, you have a typo:

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

SkuPoperty => SkuProperty

SkuPoperty => SkuProperty

详细说明,Xamarin 需要具有 PropertyNameProperty 命名约定:创建可绑定属性

To elaborate, Xamarin requires to have PropertyNameProperty naming convention: Creating a bindable property

这篇关于Xamarin Forms 找不到“Sku"的属性、可绑定属性或事件,或者值和属性之间的类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆