I am working on Xamarin.Forms using PRISM library for MVVM architecture.
So, the problem is whenever I navigating between pages using INavigationService always the class/ViewModel is instantiating newly, so already assigned strings are becoming empty/null. I am registering page and ViewModel in App.Xaml.cs as below:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<LoginPage, LoginViewModel>();
}
How to handle not to instantiate newly always or need to Instantiate only one time throughout the app working.
What you're trying to do is not supported for a variety of reasons. Suffice it to say a Singleton ViewModel is a very bad practice and causes a lot of problems. While we cannot stop you from registering the ViewModel as a singleton with the container this WILL introduce bugs into your app.
Appropriate Ways
You haven't really provided any details of what it is you're trying to instantiate but one of these methods should work for you.
Use IInitialize or INavigationAware.OnNavigatedTo
Use a Singleton Service
I should also point out that if you are looking for something to last from one session to the next of your app running then you could use the built in
IApplicationStore
which exposes the Properties Dictionary and SavePropertiesAsync from the Application in an interface that keeps your code testable.