Xamarin Forms navigation issue

I am working on a Xamarin Forms Application and I am having a problem.

It looks like the navigation system is slightly off. I am using the root NavigationPage to host my pages and it works great all the time except ...

I have a subpage with a list of items. When listening, each element should display information about itself, on a separate page (whether it's modal or not).

However, it only works the first time. The second time I try to check the entry, it seems to load it twice (the font is thicker and a little ghostly) and when I click back one layer is left behind and the other moves with close animation (which is why I think it is was downloaded twice).

The code is as follows:

public partial class NotesPage : ContentPage {
protected override void OnAppearing()
    {
        base.OnAppearing();

        NotesList.ItemsSource = VM.Notes; // VM is my ViewModel, NotesList is a ListView defined in XAML
        NotesList.ItemSelected += async (sender, e) =>
        {
            if (e.SelectedItem == null) return;
            await Navigation.PushAsync(new NoteDetails((Note)e.Item));
            ((ListView)sender).SelectedItem = null;
        };
    }
}

      

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       x:Class="Views.Notes.NoteDetails">
  <StackLayout>
    <Label Text="{Binding Title}" />
    <Label Text="{Binding Date}" />
    <Label Text="{Binding Author}" />
    <Label Text="{Binding Content}" />
  </StackLayout>
</ContentPage>

      

The NoteDetails class only sets the BindingContext for the Note object passed. The Note class has four properties: String Title, String Date (its code field is a DateTime object, conversion is to a getter-setter), String Author, and String Content. All details are displayed correctly.

The only problem is retesting - it just refuses to work properly. What could be the problem?

+3


source to share


2 answers


OnAppearing gets called again when you close the modal.

This means that the next time you click ItemSelected, the code will run twice.



IOS only though.

+4


source


you can also set a variable to check if the page is loaded the first time.



protected override void OnAppearing()
{
   if (_isFirstLoad) {
   //-- create your list
   //-- _isFirstLoad = true;
   }
}

      

+2


source







All Articles