Xamarin Forms 找不到 'Sku' 的属性、可绑定(bind)属性或事件,或者值和属性之间的类型不匹配

标签 xamarin xamarin.forms

在我的 xamarin 项目中,我有一个自定义按钮 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 的方式完全相同,你有什么。

在我引用此 Controller 的 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....
我似乎无法弄清楚为什么我会得到它。我所有的类型都是一致的。我试图绑定(bind)的对象是这样完成的:

公共(public)部分类 SkuView : ContentView
{
公共(public) ApiModels.SkuDetailModel Sku { 获取;放; }

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

每个对 xaml header 的请求,这里是整个 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>

最佳答案

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

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

SkuPoperty => SkuProperty

编辑:
详细地说,Xamarin 需要有 PropertyName 房产 命名约定:
Creating a bindable property

关于Xamarin Forms 找不到 'Sku' 的属性、可绑定(bind)属性或事件,或者值和属性之间的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48432840/

相关文章:

xamarin - 从条目中删除填充

xamarin - DisplayAlert 导致应用程序在 Xamarin.Forms 中崩溃

c# - Xamarin Forms : can Prism and ReactiveUI be used in the same project ? 如果是这样,这是个好主意吗?

c# - 从 iCloud 打开自定义文件到 Xamarin.iOS 应用程序不起作用

mvvm - SharedTextSource 的 MvxLang 绑定(bind)语法

c# - 无法在 visual studio 2015 中运行 android ddms

android - 在上下文操作中使用导航会引发异常

无法为某些用户安装/更新 Android 应用程序 (Xamarin)

android - Xamarin 应用程序在 Debug模式下构建,但不是在 Android 的 Release模式下构建

xaml - Xamarin 将父 BindingContext 值传递给转换器