我有一个带有自定义视单元的ListView,我在其中放置了Label和GestureRecognizers。 如何使用带有TapGestureRecognizer的Label处理不同的项目。所以这是我的代码。
<ViewCell>
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="5">
<Label Text="{Binding Title, Converter={StaticResource LettersConverter}}" FontSize="30" TextColor="#edfff1"/>
<Label Text="" FontSize="40">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
<StackLayout Orientation="Horizontal" Padding="1">
<Label Text="Alarm" TextColor="#edfff1" FontSize="Micro"/>
<Label Text="{Binding AlarmTime, StringFormat='{0:dddd, d MMMM, yyyy, HH:mm}'}"/>
</StackLayout>
</StackLayout>
</ViewCell>
谢谢。
更新代码:
的Xaml:
<Label Text="">
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand}"
CommandParameter="{Binding .}" />
</Label.GestureRecognizers>
</Label>
代码:
ICommand tapCommand;
public MainPage()
{
tapCommand = new Command(OnTapped);
}
public ICommand TapCommand
{
get { return tapCommand; }
}
void OnTapped(object s)
{
test.Text = "Works";
}
最终工作守则。
的Xaml:
<Label Text="Test">
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Path = TapCommand, Source={x:Reference MainPage}}" CommandParameter="{Binding .}"
/>
</Label.GestureRecognizers>
</Label>
代码背后。
ICommand tapCommand;
//constructor
public MainPage()
{
tapCommand = new Command(OnClick);
}
public ICommand TapCommand
{
get { return tapCommand; }
}
void OnClick(object i)
{
var model = (Note)i;
st.Text = model.Title;
}
答案 0 :(得分:1)
这真的很容易。 3个特殊步骤
1像这样CommandObject =“ {Binding。}”
将该对象绑定到标签。2 create函数来调用Tapped =“ TapGestureRecognizer_Tapped”
<StackLayout Orientation="Horizontal" Padding="5">
<Label Text="{Binding Title, Converter={StaticResource LettersConverter}}" FontSize="30" TextColor="#edfff1"/>
<Label Text="" FontSize="40">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding .}" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
在您的代码后面的3处获得所选标签var lbl = sender作为Label; 将绑定模型作为选定对象获取var selectedItem = lbl.BindingContext作为YourModel;
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
var lbl = sender as Label;
var selectedItem = lbl.BindingContext as YourModel;
if (selectedItem == null) return;
// now you can access any property of your model
// for example var id = YourModel.id
// or you can pass your model to a function like
//HideorShowDetail(selectedItem);
}
请注意,您可以对任何对象使用此方法。 例如,如果您在listView中创建可点击的图像。
var lbl = sender as Label; will be var img = sender as Image;
button would be var btn = sender as Button;
等等
答案 1 :(得分:0)
如果我理解正确,您想知道点击了哪个标签。如果我是你,我会使用ICommand而不是事件处理程序和CommandParameter,它会为你提供项目ID。
MusicPlayManager.sharedInstance.player
在您的ViewModel或后面的代码中(取决于您的<TapGestureRecognizer Command="{Binding OnClickCommand}"
CommandParameter="{Binding ItemId}" />
是什么,您需要像这样实现ICommand:
BindingContext
Xamarin在官方文档here上有一个很好的教程如何做这样的事情。
编辑:所以,这似乎是您正在寻找的。 p>
ICommand _onClickCommand;
public ICommand OnClickCommand
{
get { return _onClickCommand; }
}
public YourViewModel() {
OnClickCommand = new Command (OnClick);
}
void OnClick(object i) {
var labelId = Convert.ToString(i);
// Now you know which label was clicked and you can do something with it
}
然后
<TapGestureRecognizer Command="{Binding OnClickCommand}"
CommandParameter="{Binding .}" />
编辑2:这里的问题是TapGestureRecognizer没有指向页面的BindingContext。你要做的是以下几点:
void OnClick(object i) {
var model = (YourViewModel)i;
// Now you can access model.AlarmTime, model.Title etc.
}
另外,将x:Name添加到您的页面,如下所示:
<TapGestureRecognizer Command="{Binding Path = BindingContext.OnClickCommand, Source={x:Reference MainPage}}" CommandParameter="{Binding .}" />
现在,当您点击标签时,OnClickCommand位于主BindingContext中,而不是ListView的ItemsSource中。