2017-01-30 196 views

回答

2

EventToCommandBehavior目前不存在於NuGet上可用的pre1包中。這在pre2發佈時應該可用。

我的建議是,你要麼EventToCommandBehavior複製到你的項目,現在,或者你可以添加一個,我使用:在XAML中

/// <summary> 
/// ListView Item Tapped Behavior. 
/// </summary> 
public class ItemTappedBehavior : BehaviorBase<ListView> 
{ 
    /// <summary> 
    /// Gets or sets the command. 
    /// </summary> 
    /// <value>The command.</value> 
    public ICommand Command { get; set; } 

    /// <inheritDoc /> 
    protected override void OnAttachedTo(ListView bindable) 
    { 
     base.OnAttachedTo(bindable); 
     AssociatedObject.ItemTapped += OnItemTapped; 
    } 

    /// <inheritDoc /> 
    protected override void OnDetachingFrom(ListView bindable) 
    { 
     base.OnDetachingFrom(bindable); 
     AssociatedObject.ItemTapped -= OnItemTapped; 
    } 

    void OnItemTapped(object sender, ItemTappedEventArgs e) 
    { 
     if (Command == null || e.Item == null) return; 

     if (Command.CanExecute(e.Item)) 
      Command.Execute(e.Item); 
    } 
} 

然後

<ListView.Behaviors> 
    <behaviors:ItemTappedBehavior Command="{Binding SelectedItemCommand}" /> 
</ListView.Behaviors> 
2

我會建議一種不同的方法。

public class AppListView: ListView{ 

    public AppListView(): base(ListViewCachingStrategy.RecycleElement){ 
     this.ItemSelected += (s,e)=>{ 
      this.TapCommand?.Invoke(e.Item); 
     } 
    } 

    #region Property TapCommand 

    /// <summary> 
    /// Bindable Property TapCommand 
    /// </summary> 
    public static readonly BindableProperty TapCommandProperty = BindableProperty.Create(
     nameof(TapCommand), 
     typeof(System.Windows.Input.ICommand), 
     typeof(AppListView), 
     null, 
     BindingMode.OneWay, 
     null, 
     null, 
     null, 
     null, 
     null 
    ); 

    /// <summary> 
    /// Property TapCommand 
    /// </summary> 
    public System.Windows.Input.ICommand TapCommand 
    { 
     get 
     { 
      return (System.Windows.Input.ICommand)GetValue(TapCommandProperty); 
     } 
     set 
     { 
      SetValue(TapCommandProperty, value); 
     } 
    } 
    #endregion 

} 

現在使用AppListView代替ListView和使用TapCommand="{Binding ...}"。爲了使intellisense正常工作,我建議將這個類保存在一個單獨的庫項目中(一個用於android,一個用於iOS,並將該文件保存在所有庫項目之間的共享項目中)。

相關問題