Bui Quang Huy Bui Quang Huy - 2 months ago 15
C# Question

Initialize XAML page once when application is running UWP

I want to init XAML and its code behind page once when that page is open. I try to register it with

ContainerControlledLifetimeManager
but it still initializes every time I navigate to it. So do we have any way to keep it alive during the application lifetime?

UPDATE

I registered my XAML file and code behind with
ContainerControlledLifetimeManager
like this:

Container.RegisterType<MyPage>(new ContainerControlledLifetimeManager());


But it still run the constructor every time we navigate to that page.
In ContainerControlledLifetimeManager comment say:


A Microsoft.Practices.Unity.LifetimeManager that holds onto the
instance given to it. When the
Microsoft.Practices.Unity.ContainerControlledLifetimeManager is
disposed, the instance is disposed with it.

Answer Source

How about setting NavigationCacheMode of your page to either Required or Enabled instead?