[英]Xamarin.Forms - Expander Command not being called
I really don't understand what I am doing wrong, I have using toolkits expander and I am trying to call a method when the expander header is being tapped.我真的不明白我做错了什么,我使用了工具包扩展器,并且我试图在扩展器 header 被点击时调用一个方法。 According to their docs:
根据他们的文档:
Command, of type ICommand, which is executed when the Expander header is tapped.命令,类型为 ICommand,在点击扩展器 header 时执行。
so i tried this:所以我尝试了这个:
<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>
with this in my code behind:在我的代码后面:
public Command GetMathSubCatgories
{
get
{
return new Command((obj) =>
{
Console.Write("Here");
});
}
}
But its not being called, what am I doing wrong?但是它没有被调用,我做错了什么?
Here is my full code behind:这是我背后的完整代码:
public partial class AssignTaskPage : ContentPage
{
public AssignTaskPage()
{
InitializeComponent();
GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
}
public ICommand GetMathSubCatgories { get; private set; }
void MathSubCatgoriesCommand()
{
Console.Write("Here");
}
}
You could use the code below.您可以使用下面的代码。
public class CatgoryViewModel : INotifyPropertyChanged
{
public CatgoryViewModel()
{
GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
}
public ICommand GetMathSubCatgories { get; private set; }
void MathSubCatgoriesCommand()
{
Console.Write("Here");
}
}
OutPut: https://imgur.com/Vda9p2p OutPut: https://imgur.com/Vda9p2p
Update:更新:
If you do not do that in viewmodel, you could try the code below for code behind.如果您不在 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; }
}
you need to assign a BindingContext
in order for binding to work您需要分配一个
BindingContext
才能使绑定工作
public AssignTaskPage()
{
InitializeComponent();
GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
this.BindingContext = this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.