0

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.

Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
  • Try singleton lifetime? Not very clear without any code mind. – Chris Watts Aug 28 '17 at 03:50
  • I updated my question. Could you let me know more information about singleton lifetime? – Bui Quang Huy Aug 28 '17 at 03:54
  • It would only create one for the lifetime of the container/lifetime of the application. Its not quite what you want, but you are currently creating one each time you need the page and dont want that, so try singleton – Chris Watts Aug 28 '17 at 03:58
  • Could you give me the exact name of singleton lifetime? Do we register it like my lifetime above or just make it like singleton pattern? – Bui Quang Huy Aug 28 '17 at 04:00
  • I apologise, looks like you are already using the singleton lifetime according to this: https://stackoverflow.com/questions/16835728/unity-singleton-code seems like you have another problem at hand. Why do you think its being recreated multiple times? – Chris Watts Aug 28 '17 at 04:08
  • Just debug and I saw it jumps into constructor method multiple times :D I think we can do this with XAML page. it needs to recreate every time we display it. – Bui Quang Huy Aug 28 '17 at 04:14

1 Answers1

0

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

Jessica
  • 2,057
  • 1
  • 15
  • 24