2016-01-22 45 views
0

我是Xamarin.forms的新手,我的客戶想要一個像Gmail這樣的功能,用戶可以點擊並按住其中一個列表項並獲得多選項目的選項。Xamarin.Forms是否支持輕擊和按住手勢?

該應用程序將與可用的像刪除不同的選擇,視圖中的項目的列表,上傳等SO基本上它有5個選項,並按照窗口移動限制該應用不能有超過4的菜單選項(ToolbarItem)。因此需要點擊並保持手勢。一旦用戶點擊並保存其中一個項目,ToolbarItem sholud就會更改並且僅替換爲刪除選項。通過這樣做,我們可以將ToolbarItem減少到四個。

任何參考將有很大的幫助! :-)

也想知道如果點擊和按住是可能的那麼不同的平臺(iOS,Windows,Android)將如何渲染它?它會由Xamarin.forms處理嗎,還是代碼中有些東西需要照顧到不同的操作系統平臺?

回答

1

您是否考慮過使用Context Options而不是替換工具欄中的選項?

如果你可以使用右鍵選項,而不是工具欄,你不需要第三方組件,如Xamarin.Forms允許你定義爲每個輕鬆ListView項這樣的選項:

要實例化您的ListView

var listView = new ListView(); 
listView.ItemTemplate = new DataTemplate(typeof(MyListItemCell)); 

而且數據模板應該是這樣的:

public class MyListItemCell : ViewCell 
{ 
    // To register the LongTap/Tap-and-hold gestures once the item model has been assigned 
    protected override void OnBindingContextChanged() 
    { 
     base.OnBindingContextChanged(); 
     RegisterGestures(); 
    } 

    private void RegisterGestures() 
    { 
     var deleteOption = new MenuItem() 
     { 
      Text = "Delete", 
      Icon = "deleteIcon.png", //Android uses this, for example 
      CommandParameter = ((ListItemModel) BindingContext).Id 
     }; 
     deleteOption.Clicked += deleteOption_Clicked; 
     ContextActions.Add(deleteOption); 

     //Repeat for the other 4 options 

    } 
    void deleteOption_Clicked(object sender, EventArgs e) 
    { 
     //To retrieve the parameters (if is more than one, you should use an object, which could be the same ItemModel 
     int idToDelete = (int)((MenuItem)sender).CommandParameter; 
     //your delete actions 
    } 
    //Write the eventHandlers for the other 4 options 
} 
0

不幸的是,事件不在默認情況下。然而,有一個便宜的第三方組件可以爲您處理:使用LongPressing或LongPressed的http://www.mrgestures.com/#events

或者你可以選擇在每個平臺上本地實現它。

+0

如果我必須這樣做,每個平臺上,那麼這將意味着在Xamar 3個平臺上創建3個不同的項目在或修改xamarin.forms項目來實現它? – Arti

+0

你能提供任何相同的例子或參考.. – Arti