Xamarin.forms 添加自定义行为后,我的应用程序无法运行,无法理解原因

Xamarin.forms 添加自定义行为后,我的应用程序无法运行,无法理解原因,xamarin.forms,Xamarin.forms,我已经创建了一个自定义步进器行为,并将该行为添加到我的xaml中的步进器中,但由于某些原因,在添加该行为后,应用程序无法编译,因此出现以下错误: Position 82:87. No property, bindable property, or event found for 'ValueChangedCommand', or mismatching type between value and property. (ComanderoMovil) 以下是我的行为准则: using Syst

我已经创建了一个自定义步进器行为,并将该行为添加到我的xaml中的步进器中,但由于某些原因,在添加该行为后,应用程序无法编译,因此出现以下错误:

Position 82:87. No property, bindable property, or event found for 'ValueChangedCommand', or mismatching type between value and property. (ComanderoMovil)
以下是我的行为准则:

using System;
using System.Windows.Input;
using Xamarin.Forms;

namespace ComanderoMovil.Behaviors
{
    public class StepperQuantityChangedBehavior : Behavior<Stepper>
    {
        public static readonly BindableProperty StepperValueChangedProperty =
            BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);

        public ICommand ValueChangedCommand
        {
            get
            {
                return (ICommand)GetValue(StepperValueChangedProperty);
            }
            set
            {
                SetValue(StepperValueChangedProperty, value);
            }
        }

        protected override void OnAttachedTo(Stepper bindable)
        {
            base.OnAttachedTo(bindable);
            bindable.ValueChanged += Bindable_ValueChanged;
        }

        protected override void OnDetachingFrom(Stepper bindable)
        {
            base.OnDetachingFrom(bindable);
            bindable.ValueChanged -= Bindable_ValueChanged;
        }

        private void Bindable_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (ValueChangedCommand == null)
            {
                return;
            }
            var stepper = sender as Stepper;
            var prueba = e.NewValue;

            if (ValueChangedCommand.CanExecute(prueba))
            {
                ValueChangedCommand.Execute(prueba);
            }
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ComanderoMovil.Views.DishView"
             xmlns:converterPack="clr-namespace:Xamarin.Forms.ConvertersPack;assembly=Xamarin.Forms.ConvertersPack"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="true"
             xmlns:local="clr-namespace:ComanderoMovil.Behaviors"
             x:Name="DishSelectedPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="shopping_cart" Text="Search"  Command="{Binding ShowCartCommand}" />
    </ContentPage.ToolbarItems>

     <ContentPage.Resources>
        <ResourceDictionary>
            <converterPack:CurrencyConverter x:Key="CurrencyConverter"></converterPack:CurrencyConverter>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label Text="{Binding Dish.Name}"
                       FontSize="Title"
                       HorizontalOptions="Center"
                       FontAttributes="Bold"></Label>
                <Label Text="Precio"
                       FontSize="Subtitle"
                       HorizontalOptions="Center"
                       FontAttributes="Bold"></Label>
                <Label Text="{Binding Dish.Price1, Converter={StaticResource CurrencyConverter}}"
                       FontSize="Subtitle"
                       HorizontalOptions="Center"></Label>
                <Label Text="Modificadores"
                       FontAttributes="Bold"
                       FontSize="Large"
                       HorizontalOptions="Center"></Label>
                <ListView ItemsSource="{Binding DishesMods}"
                          x:Name="ModsListView"
                          HasUnevenRows="True"
                          SeparatorVisibility="Default"
                          SeparatorColor="Black"
                          IsGroupingEnabled="True"
                          HeightRequest="{Binding ListHeight}">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell Height="30">
                                <StackLayout VerticalOptions="FillAndExpand"
                                             Padding="10"
                                             BackgroundColor="DimGray">
                                    <Label Text="{Binding Key}"
                                           TextColor="White"
                                           VerticalOptions="Center"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Padding="20">
                                    <StackLayout Orientation="Horizontal">
                                        <CheckBox Color="#102536">
                                            <CheckBox.Behaviors>
                                                <local:CheckBoxModChangedState ItemCheckedCommand="{Binding BindingContext.SelectedModCommand, Source={Reference DishSelectedPage}}"></local:CheckBoxModChangedState>
                                            </CheckBox.Behaviors>
                                        </CheckBox>
                                        <Label Text="{Binding Name}"
                                               VerticalOptions="Center"></Label>
                                        <Label Text="Precio:"
                                               VerticalOptions="Center"></Label>
                                        <Label Text="{Binding Price}"
                                               VerticalOptions="Center"></Label>
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal">
                                        <Label Text="Cantidad: "></Label>
                                        <Label Text="1"></Label>
                                    </StackLayout>
                                    <StackLayout>
                                        <Stepper HeightRequest="40"
                                                 WidthRequest="40">
                                            <Stepper.Behaviors>
                                                <local:StepperQuantityChangedBehavior ValueChangedCommand="{Binding BindingContext.ModQuantityCommand, Source={Reference DishSelectedPage}}"></local:StepperQuantityChangedBehavior>
                                            </Stepper.Behaviors>
                                        </Stepper>
                                    </StackLayout>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.Footer>
                        <ContentView>
                            <Frame HasShadow="False"
                                   Padding="50">
                                <Button Padding="20"
                                        Text="Agregar Orden"
                                        TextColor="White"
                                        BackgroundColor="#102536"
                                        Command="{Binding BindingContext.AddOrderCommand, Source={Reference DishSelectedPage}}"></Button>
                            </Frame>
                        </ContentView>
                    </ListView.Footer>
                </ListView>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

使用系统;
使用System.Windows.Input;
使用Xamarin.Forms;
名称空间comanderomovill.Behaviors
{
公共类StepperQuantityChangedBehavior:行为
{
公共静态只读BindableProperty StepperValueChangedProperty=
Create(“ValueChangedCommand”、typeof(ICommand)、typeof(StepperQuantityChangedBehavior)、null);
公共ICommand值更改命令
{
得到
{
返回(ICommand)GetValue(StepperValueChangedProperty);
}
设置
{
设置值(StepperValueChangedProperty,值);
}
}
受保护的覆盖无效数据传输(步进可绑定)
{
碱。可粘合的DTO(可粘合);
bindable.ValueChanged+=bindable\u ValueChanged;
}
受保护的覆盖无效OnDetachingFrom(步进机可绑定)
{
基础。从(可装订)开始连接;
bindable.ValueChanged-=bindable\u ValueChanged;
}
private void Bindable_ValueChanged(对象发送方,valuechangedventargs e)
{
if(ValueChangedCommand==null)
{
返回;
}
var步进器=发送器作为步进器;
var prueba=e.NewValue;
if(值更改命令可执行(prueba))
{
ValueChangedCommand.Execute(prueba);
}
}
}
}
这是我的xaml代码,我在其中添加了行为:

using System;
using System.Windows.Input;
using Xamarin.Forms;

namespace ComanderoMovil.Behaviors
{
    public class StepperQuantityChangedBehavior : Behavior<Stepper>
    {
        public static readonly BindableProperty StepperValueChangedProperty =
            BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);

        public ICommand ValueChangedCommand
        {
            get
            {
                return (ICommand)GetValue(StepperValueChangedProperty);
            }
            set
            {
                SetValue(StepperValueChangedProperty, value);
            }
        }

        protected override void OnAttachedTo(Stepper bindable)
        {
            base.OnAttachedTo(bindable);
            bindable.ValueChanged += Bindable_ValueChanged;
        }

        protected override void OnDetachingFrom(Stepper bindable)
        {
            base.OnDetachingFrom(bindable);
            bindable.ValueChanged -= Bindable_ValueChanged;
        }

        private void Bindable_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (ValueChangedCommand == null)
            {
                return;
            }
            var stepper = sender as Stepper;
            var prueba = e.NewValue;

            if (ValueChangedCommand.CanExecute(prueba))
            {
                ValueChangedCommand.Execute(prueba);
            }
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ComanderoMovil.Views.DishView"
             xmlns:converterPack="clr-namespace:Xamarin.Forms.ConvertersPack;assembly=Xamarin.Forms.ConvertersPack"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="true"
             xmlns:local="clr-namespace:ComanderoMovil.Behaviors"
             x:Name="DishSelectedPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="shopping_cart" Text="Search"  Command="{Binding ShowCartCommand}" />
    </ContentPage.ToolbarItems>

     <ContentPage.Resources>
        <ResourceDictionary>
            <converterPack:CurrencyConverter x:Key="CurrencyConverter"></converterPack:CurrencyConverter>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label Text="{Binding Dish.Name}"
                       FontSize="Title"
                       HorizontalOptions="Center"
                       FontAttributes="Bold"></Label>
                <Label Text="Precio"
                       FontSize="Subtitle"
                       HorizontalOptions="Center"
                       FontAttributes="Bold"></Label>
                <Label Text="{Binding Dish.Price1, Converter={StaticResource CurrencyConverter}}"
                       FontSize="Subtitle"
                       HorizontalOptions="Center"></Label>
                <Label Text="Modificadores"
                       FontAttributes="Bold"
                       FontSize="Large"
                       HorizontalOptions="Center"></Label>
                <ListView ItemsSource="{Binding DishesMods}"
                          x:Name="ModsListView"
                          HasUnevenRows="True"
                          SeparatorVisibility="Default"
                          SeparatorColor="Black"
                          IsGroupingEnabled="True"
                          HeightRequest="{Binding ListHeight}">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell Height="30">
                                <StackLayout VerticalOptions="FillAndExpand"
                                             Padding="10"
                                             BackgroundColor="DimGray">
                                    <Label Text="{Binding Key}"
                                           TextColor="White"
                                           VerticalOptions="Center"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Padding="20">
                                    <StackLayout Orientation="Horizontal">
                                        <CheckBox Color="#102536">
                                            <CheckBox.Behaviors>
                                                <local:CheckBoxModChangedState ItemCheckedCommand="{Binding BindingContext.SelectedModCommand, Source={Reference DishSelectedPage}}"></local:CheckBoxModChangedState>
                                            </CheckBox.Behaviors>
                                        </CheckBox>
                                        <Label Text="{Binding Name}"
                                               VerticalOptions="Center"></Label>
                                        <Label Text="Precio:"
                                               VerticalOptions="Center"></Label>
                                        <Label Text="{Binding Price}"
                                               VerticalOptions="Center"></Label>
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal">
                                        <Label Text="Cantidad: "></Label>
                                        <Label Text="1"></Label>
                                    </StackLayout>
                                    <StackLayout>
                                        <Stepper HeightRequest="40"
                                                 WidthRequest="40">
                                            <Stepper.Behaviors>
                                                <local:StepperQuantityChangedBehavior ValueChangedCommand="{Binding BindingContext.ModQuantityCommand, Source={Reference DishSelectedPage}}"></local:StepperQuantityChangedBehavior>
                                            </Stepper.Behaviors>
                                        </Stepper>
                                    </StackLayout>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.Footer>
                        <ContentView>
                            <Frame HasShadow="False"
                                   Padding="50">
                                <Button Padding="20"
                                        Text="Agregar Orden"
                                        TextColor="White"
                                        BackgroundColor="#102536"
                                        Command="{Binding BindingContext.AddOrderCommand, Source={Reference DishSelectedPage}}"></Button>
                            </Frame>
                        </ContentView>
                    </ListView.Footer>
                </ListView>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>


有趣的是,我为其他控件添加了自定义行为,如复选框,它可以正常工作,但只有在使用这种新行为时我才会遇到麻烦

有人知道发生了什么吗

public static readonly BindableProperty StepperValueChangedProperty
public ICommand ValueChangedCommand
正如错误消息所说,问题就在线路上

找到“ValueChangedCommand”的事件,或之间的类型不匹配 价值和财产。(科曼德罗莫维尔)

您应该将
StepperValueChangedProperty
更改为
ValueChangedCommand属性
,以保持名称与
ValueChangedCommand

改变

public static readonly BindableProperty StepperValueChangedProperty =
        BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);

    public ICommand ValueChangedCommand
    {
        get
        {
            return (ICommand)GetValue(StepperValueChangedProperty);
        }
        set
        {
            SetValue(StepperValueChangedProperty, value);
        }
    }


这就是解决办法!!,因此,在这种情况下,命名很重要,非常感谢