【问题标题】:Xamarin.Forms - Expander Command not being calledXamarin.Forms - 未调用扩展器命令
【发布时间】:2021-05-13 14:39:20
【问题描述】:

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

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");
    }

}

【问题讨论】:

  • 在“新命令”中删除obj 怎么样
  • 还是不行
  • 你是设置断点,还是看输出。像这样Debug.WriteLine("Here"); 在 Xamarin 中正常检查输出
  • GetMathSubCatgories 是在您的代码隐藏中还是在您的 VM 中定义的?命令通常位于 VM 中。

标签: c# xamarin.forms


【解决方案1】:

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

public AssignTaskPage()
{
    InitializeComponent();

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

    this.BindingContext = this;
}

【讨论】:

  • 我试过了,还是不行,我的 GetMathSubCatgories 方法中有一个断点,用于 Console.Write,它没有命中
  • @user979331 试试this.Content.BindingContext = this;
【解决方案2】:

您可以使用下面的代码。

 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"},
            new Catgory{ icon="cactus_24px.png", name="C", textColor="Red"},
            new Catgory{ icon="cactus_24px.png", name="D", textColor="Green"},
            new Catgory{ icon="cactus_24px.png", name="E", textColor="Red"},
            new Catgory{ icon="cactus_24px.png", name="F", textColor="Green"},
            new Catgory{ icon="cactus_24px.png", name="G", textColor="Red"},
            new Catgory{ icon="cactus_24px.png", name="H", textColor="Green"},
            new Catgory{ icon="cactus_24px.png", name="I", textColor="Red"},
        };
        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; }

}

【讨论】:

  • 这不起作用,我更新了我的问题以显示完整的代码
  • 我必须为此创建一个新文件吗?
  • 我在后面的代码中提供了有关如何执行此操作的代码。请检查一下。它将在输出中以相同的结果调用。
猜你喜欢
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
  • 2019-09-11
  • 2018-04-12
  • 2020-09-27
  • 2016-09-07
  • 2015-12-19
  • 1970-01-01
相关资源
最近更新 更多