我想在xaml按钮中传递一个参数



你好,我想传递标签与按钮在xaml这是一个列表视图,每一行都有自己的标签,我想通过标签

更新每一行
<Label Grid.Column="2"
Grid.Row="1"
Text="{Binding Label}" 
FontSize="20"
TextColor="Black"
Margin="0,10,0,0"
FontFamily="{StaticResource font}"
HorizontalOptions="Start"
VerticalOptions="Center"/>
<ImageButton Grid.Column="2"
Grid.Row="0"
HorizontalOptions="End"
VerticalOptions="End"
Source="plus.png"
Margin="9"
clicked ="update"
BackgroundColor="Transparent"/>

和类

中的函数public void update (object sender, EventArgs e){按标签更新}

我知道有一些命令,但我是初学者,我不知道如何使用它,我试过这个,我没有工作。

Command="{Binding Path=BindingContext.UpdateCommand, Source={x:Reference Name=listViewEvent}}"  
CommandParameter="{Binding Label}"

函数为

UpdateCommand = new Command<string>(async (args) => {
String Label = args as string;
DisplayAlert("update", "update", "OK");
_ = Navigation.PushAsync(new AddReminder(Label));
});

和警报没有显示我需要帮助

你不需要"pass"Label

<ImageButton Grid.Column="2" Cilcked="update" ... />
protected void update(object sender, EventArgs a)
{
var btn = (ImageButton)sender;
// MyClassName should be the name of your model class
// this will get you the object referenced by the selected row
var item = (MyClassName)btn.BindingContext;
// then you can reference any property of item
// ie, item.Label, etc
}
<Label x:Name="myLabel"
Grid.Column="2"
Grid.Row="1"
Text="{Binding Label}" 
FontSize="20"
TextColor="Black"
Margin="0,10,0,0"
FontFamily="{StaticResource font}"
HorizontalOptions="Start"
VerticalOptions="Center"/>
<ImageButton Grid.Column="2"
Grid.Row="0"
HorizontalOptions="End"
VerticalOptions="End"
Source="plus.png"
Margin="9"
clicked ="update"
BackgroundColor="Transparent"
CommandParameter={x:Reference myLabel}/>
在后面的代码中你可以得到标签
protected void update(object sender, EventArgs a)
{
var btn = (ImageButton)sender;
var label = btn.CommandParameter as Xamarin.Forms.Label;
}

最新更新