Create new instance every time you use NavigationPage in Xamarin Forms?

1

Suppose I have 3 screens in Xaml and I use MasterDetailPage and NavigationPage to navigate between these screens back and forth. Page A - > Page B - > Page C - > ...

I use PushAsync to do the browsing. So good ...

I've implemented this in two ways:

1 - I create a new instance of each screen every time I navigate:

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

2 - I create a static property (in 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? Do I use static 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 thank you.

    
asked by anonymous 02.12.2016 / 10:55

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 did not test this code, I wrote it headlong: p

However, one tip I give is .. use Prism: link Much easier to resolve these issues with him. In this link you have an example of navigation with MasterDetail:)

    
13.03.2017 / 18:39