Create a new instance every time you use Navigationpage in Xamarin Forms?

Asked

Viewed 433 times

1

Let’s assume that I have 3 screens in Xaml and I use the SterDetailPage and Navigationpage to navigate between these screens back and forth. Page A -> Page B -> Page C -> ...

I use Pushasync to do the navigation. So far so good...

I implemented it in two ways:

1 - I create a new instance of each screen every time in the navigations:

await Navigation.PushAsync(new Views.PageA());

2 - I create a static property (in the App View) for each screen and use this property to navigate:

public static Views.PageA PageA 
    {
        get
        {
            if (_pageA == null)
            {
                _pageA = new Views.PageA();
            }

            return _pageA =;
        }
    }


await Navigation.PushAsync(App.PageA);

Is there another way to do this? Static use or create a new instance every time?

My concern is about performance and memory usage.

Please leave your answer with your opinion and if you can leave some code I appreciate.

1 answer

4

You should always navigate to Detail. Something like:

var masterDetail = new MasterDetail()
{
    Master = new MasterPage(),
    Detail = new NavigationPage(new DetailPage())
};

MainPage = masterDetail;

To navigate, an exit would be:

var md = App.Current.MainPage as MasterDetailPage;
md.Detail.Navigation.PushAsync(new NovaPagina());

I didn’t test this code, I wrote it in my head :p

However, one tip I give is .. use Prism: https://github.com/angelobelchior/prism-xamarin-forms Much easier to solve these issues with it. This link has example navigation with Masterdetail :)

  • Angelo, it just worked this way: Md. Detail = new Navigationpage(new Novapagina());

  • 1

    Show! , the secret is in Detail with Navigationpage

Browser other questions tagged

You are not signed in. Login or sign up in order to post.