1

In my prism application I want to make a single shared instance of a view. When I try to navigate the first time it works fine, but when I try to second time it's not working. If I change the PartCreationPolicy from Shared to NonShared it works but it's give me a new instance. Are there any options for another way to do this?

[Export(ViewNames.AppView)]
[PartCreationPolicy(CreationPolicy.Shared)] 
public partial class AppMain : UserControl
{
    public AppMain()
    {
        InitializeComponent();
    }
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
Sanjay Patel
  • 955
  • 1
  • 8
  • 22
  • 1
    Could you be more specific on what you mean by "it's not working"? You might also want to explain why you want this to be a singleton. – Matt May 24 '13 at 19:12
  • U may found actual problem herehttp://stackoverflow.com/questions/16741667/exception-region-with-the-given-name-is-already-registered – Sanjay Patel May 24 '13 at 19:35

1 Answers1

0

You might want to play around with Prism's KeepAlive value for your view. This value determines whether the view should be removed from the region when you navigate away from it. You have two ways of doing this:

  1. Using the RegionMemberLifetime attribute

    [RegionMemberLifetime(KeepAlive = false)]
    [Export(ViewNames.AppView)]
    [PartCreationPolicy(CreationPolicy.Shared)] 
    public partial class AppMain : UserControl
    {
        public AppMain()
        {
            InitializeComponent();
        }
    }
    
  2. Implementing the IRegionMemberLifetime interface

    [Export(ViewNames.AppView)]
    [PartCreationPolicy(CreationPolicy.Shared)] 
    public partial class AppMain : UserControl, IRegionMemberLifetime
    {
        public AppMain()
        {
            InitializeComponent();
        }
    
        public bool KeepAlive
        {
            get { return false; }
        }
    }
    

You can read some more about the KeepAlive property here.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110