I have a listview which on any item selected navigates to another XAML page. When I press the back button I return back to the original listview page.
Now if I select the same list view item, it does not navigate to the intended XAML page. If I select another item in listview it navigates.
So the issue I am facing is that tapping/selecting the same item does not navigate to the intended page the second time after the back button is pressed, but any other item in the list does navigate. Also the break point is not hit in VS (onitemtapped event args), so can't debug the issue too.
Any help/suggestions on the same?.
Thanks In Advanvce!!..
Setting a variable to null won't do anything, you need to set the ListView's SelectedItem property to null.
((ListView)sender).SelectedItem = null;
Answers
https://docs.microsoft.com/it-it/xamarin/xamarin-forms/user-interface/listview/interactivity#selectiontaps
I am using "ItemTapped" property of listview on XAML and implementing the event in code behind.
Also in the end, after page navigated I make the selected item null again.
Here is my code:
Setting a variable to null won't do anything, you need to set the ListView's SelectedItem property to null.
((ListView)sender).SelectedItem = null;
I would think so but I don't ever use ItemTapped, and the code @Siddy posted is an ItemSelected handler.
Using the following line help resolve it:
The only catch here is that, the OnItemTap event, gets called twice, due to the above mentioned line, but now as the selected item is null, it returns or comes out of the event method.
As you have found, clearing the SelectedItem solves this.
However, this clears the indication of the selected item on the UI. So ItemTapped may be a better option.
Also you should really be using MVVM and not code behind.
I agree with Alessandro. It is a "Mode=TwoWay" binding issue. I set "selectedItem = null" and forced Initialize component OnAppearing() method:
protected override void OnAppearing()
{
// Fix error Grid Mode=TwoWay
InitializeComponent();
}
It solve the issue of Binding two way mode!
Sure this will work, but completely unnecessary. You should (probably?) never reinitialize your component everytime the page appears. It's better the reset the SelectedItem in OnAppearing.
e.g. this.ListViewName.SelectedItem = null;
Just Set SelectionMode = "None"
call the ItemTapped Event..
Thats it...
I tried and worked