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



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

命令,类型为ICommand,在点击扩展器标头时执行。

所以我尝试了这个:

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

您需要分配一个BindingContext,以便绑定到工作

public AssignTaskPage()
{
InitializeComponent();
GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
this.BindingContext = this;
}

您可以使用下面的代码。

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

输出:https://i.stack.imgur.com/uT8ot.jpg

更新:

如果您在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; }
}

最新更新