微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.Forms - 扩展器命令未被调用

如何解决Xamarin.Forms - 扩展器命令未被调用

我真的不明白我做错了什么,我使用了工具包扩展器,我试图在扩展器标头被点击时调用一个方法。根据他们的文档:

ICommand 类型的命令,在点击 Expander 标头时执行。

所以我试过这个:

 <xct:Expander Command="{Binding GetMathSubCatgories}">
                            <xct:Expander.Header>
                                <Frame Padding="10" Margin="10" HasShadow="False" BorderColor="LightGray" VerticalOptions="CenterandExpand">
                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="{Binding icon}" WidthRequest="25" HeightRequest="25"></Image>
                                        <Label Text="{Binding name}" TextColor="{Binding textColor}" FontSize="Large" FontAttributes="Bold" HeightRequest="35" VerticalOptions="CenterandExpand"></Label>
                                    </StackLayout>
                                </Frame>
                            </xct:Expander.Header>
                            <Grid Padding="10">
                                <Grid.ColumnDeFinitions>
                                    <ColumnDeFinition Width="Auto" />
                                </Grid.ColumnDeFinitions>
                                <ListView x:Name="SubCategories" ItemsSource="{Binding subCategories}" ItemSelected="SubCategories_ItemSelected">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <StackLayout>
                                                    <Label Text="{Binding name}" TextColor="#02cc9d" FontAttributes="Bold" HeightRequest="35" VerticalOptions="CenterandExpand"></Label>
                                                </StackLayout>
                                            </ViewCell>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </Grid>
                        </xct:Expander>

在我的代码后面:

public Command GetMathSubCatgories
        {
            get
            {
                return new Command((obj) =>
                {
                    Console.Write("Here");
                });
            }
        }

但它没有被调用,我做错了什么?

这是我的完整代码

public partial class AssignTaskPage : ContentPage
{

    

    public AssignTaskPage()
    {
        InitializeComponent();

        GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());

    }

    public ICommand GetMathSubCatgories { get; private set; }
    void MathSubCatgoriesCommand()
    {
        Console.Write("Here");
    }

}

解决方法

您可以使用下面的代码。

 public class CatgoryViewModel : INotifyPropertyChanged
 {
    public CatgoryViewModel()
    {           
        GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());            
     }
      public ICommand GetMathSubCatgories { get; private set; }   
      void MathSubCatgoriesCommand()
      {
        Console.Write("Here");
      }
 }

输出: https://imgur.com/Vda9p2p

更新:

如果你没有在viewmodel中这样做,你可以试试下面的代码背后的代码。

 public partial class Page3 : ContentPage
{        
    public ObservableCollection<Catgory> catgories { get; set; }
    public Page3()
    {
        InitializeComponent();
        catgories = new ObservableCollection<Catgory>()
        {
            new Catgory{ icon="cactus_24px.png",name="A",textColor="Red"},new Catgory{ icon="cactus_24px.png",name="B",textColor="Green"},name="C",name="D",name="E",name="F",name="G",name="H",name="I",};
        GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
        this.BindingContext = this;
    }
    public ICommand GetMathSubCatgories { get; private set; }
    void MathSubCatgoriesCommand()
    {
        Console.Write("Here");
    }

    private void SubCategories_ItemSelected(object sender,SelectedItemChangedEventArgs e)
    {

    }
}
public class Catgory
{
    public string icon { get; set; }
    public string name { get; set; }
    public string textColor { get; set; }

}
,

您需要分配一个 BindingContext 才能使绑定生效

public AssignTaskPage()
{
    InitializeComponent();

    GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());

    this.BindingContext = this;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。