Hi. This may seem like a very easy question, but I'm having a hard time getting it to work. I'm using the Mr. Gestures library for a Xamarin project. First I will be developing on Android, and will later hope it works on the iOS.
So basically I have a ListView control with multiple rows. Now I'm subscribing to the LongPressed event. When that happens I need to determine the bindableobject associated with that row. Unfortunately, the LongPressed event seems to be called too soon before the sender (ListView)'s SelectedItem property got set.
Can anyone review the code and make recommendations?
Thank you.
View File:
CustomListView _myListView = null;
protected CustomListView MyListView
{
get
{
if (_myListView == null)
{
_myListView = new CustomListView()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
ItemTemplate = new DataTemplate(typeof(MyViewCell)),
};
_myListView.ItemTapped += (s, e) =>
{
var list = s as ListView;
if (list == null)
return;
list.SelectedItem = null;
};
_myListView.ItemSelected += OnItemSelected;
_myListView.LongPressed += myListView_LongPressed; // What I'm after.
_myListView.SetBinding(ListView.SelectedItemProperty, "SelectedOne"); // ????
}
return _myListView;
}
}
private void _myListView_LongPressed(object sender, MR.Gestures.LongPressEventArgs e)
{
// FIRST: this gets called with a null value for the listview's SelectedItem property, and a null value for the ViewModel 'SelectedOne' dependency property.
System.Diagnostics.Debug.WriteLine("_myListView_LongPressing called.");
}
ViewModel File:
public GalaSoft.MvvmLight.ObservableObject _selectedOne;
public GalaSoft.MvvmLight.ObservableObject SelectedOne
{
get { return _selectedOne; }
set
{
// THEN: this gets called later. The VM dependency property gets set, but the 'SelectedItem' property of the listview is still null.
if (_selectedOne != value)
_selectedOne = value;
//OnPropertyChanged("SelectedOne"); // This does not compile. Not sure if it's important.
}
}