我试图在点击 ListView 中的项目后打开另一个 View 。
我试过添加 TapGestureRegonizer
甚至添加带有网格的 ViewCell
等。这些似乎都不起作用。我在标签上添加了一个点击手势,这似乎有效,但对于 ListView 项目却不起作用。对于 ListView 之类的东西,这似乎是一个简单的问题,但似乎没有为此内置的功能。
Xaml:
<ListView x:Name="dataList"
ItemsSource="{Binding routeLabels}"
HasUnevenRows="True"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3">
</ListView>
背后的代码:
var listviewgesture = new TapGestureRecognizer();
listviewgesture.SetBinding(TapGestureRecognizer.CommandProperty,"LoadRoutePage");
dataList.GestureRecognizers.Add(listviewgesture);
View 模型:
public ICommand LoadRoutePage { get; protected set; }
public DriverDashboardViewModel(INavigation navigation,MessagDatabase database)
{
this._database = database;
this.Navigation = navigation;
this.LoadNotifications = new Command(async () => await OpenNotificationsPage());
this.LoadRoutePage = new Command(async () => await OpenRoutePage());
}
public async Task OpenRoutePage()
{
await Navigation.PushAsync(new RoutePageView());
}
需要说明的是,LoadNotifications
方法在打开页面时有效,但 LoadRoutePage
无效。所以我知道 View 和 View 模型之间存在某种程度的通信。
最佳答案
您不应将 TapGestureRecognizer
添加到 ListView
。每个单元格都已经有处理点击它们的事件,而 GestureRecognizer 可能只会混淆 ListView
关于点击应该做什么。有几种方法可以解决这个问题。
<强>1。 SelectedItem 绑定(bind)
将 SelectedItem
属性绑定(bind)到 ListView
并在该属性的 setter 中处理您的方法调用。
<ListView x:Name="dataList" ItemsSource="{Binding routeLabels}"
HasUnevenRows="True" Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="3" SelectedItem="{Binding SelectedItem}">
</ListView>
在你的 View 模型中:
string _selectedItem;
public string SelectedItem {
get {return _selectedItem; }
set
{
_selectedItem = value;
// Additional code
}
}
<强>2。使用内置事件 ItemSelected 或 ItemTapped
ListView
有一些您可以连接的事件,名为 ItemSelected
和 ItemTapped
。这些可以在代码隐藏中捕获,并且可以处理您要实现的目标。
<ListView x:Name="dataList" ItemsSource="{Binding routeLabels}"
HasUnevenRows="True" Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="3" ItemSelected="Handle_ItemSelected" ItemTapped="Handle_ItemTapped">
</ListView>
<强>3。使用事件命令绑定(bind)行为
由于您使用 View 模型,因此理想情况下您不需要这些事件,因为它们是在 UI 端处理的。那里有 NuGet 包,可以将事件转换为可以在 View 模型中处理的命令。看看Corcav.Behaviors例如。
<强>4。创建您自己的行为
我有一个我经常使用的,看起来像这样:
public class ListViewSelectedItemBehavior : Behavior<ListView>
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ListViewSelectedItemBehavior));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public ListView AssociatedObject { get; private set; }
protected override void OnAttachedTo(ListView bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
bindable.BindingContextChanged += OnBindingContextChanged;
bindable.ItemSelected += OnListViewItemSelected;
}
protected override void OnDetachingFrom(ListView bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
bindable.ItemSelected -= OnListViewItemSelected;
AssociatedObject = null;
}
private void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
private void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (Command == null)
return;
if (Command.CanExecute(e.SelectedItem))
Command.Execute(e.SelectedItem);
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
要将其添加到您的ListView
,您只需向其添加一个行为:
<ListView x:Name="dataList" ItemsSource="{Binding routeLabels}"
HasUnevenRows="True" Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="3">
<ListView.Behaviors>
<behaviors:ListViewSelectedItemBehavior Command="{Binding ItemSelectedCommand}" />
</ListView.Behaviors>
</ListView>
在这种情况下,ItemSelectedCommand
是 ViewModel 中的一个 Command 对象。
关于c# - 点击 ListView 项目上的手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946780/