Frage

My app has a singleton class called CycleManager. I have created a sealed class for this purpose like this

public sealed class CycleManager
{
    public static readonly CycleManager instance = new CycleManager();

    public CycleManager()
    {
        //ReadFromIsolatedStorage();
    }

    public static CycleManager Instance
    {
        get
        {
            return instance;
        }

    }
}

And the App.xaml.cs has the following code

    public App()
    {
        UnhandledException += Application_UnhandledException;

        InitializeComponent();

        InitializePhoneApplication();

        RootFrame.Navigating += new NavigatingCancelEventHandler(RootFrame_Navigating);            if (System.Diagnostics.Debugger.IsAttached)
        {
            Application.Current.Host.Settings.EnableFrameRateCounter = true;

            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }
    }

I have used the RootFrame_Navigating() to check if i need to go to the main page or the login page.

    void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        //throw new NotImplementedException();
        if (e.Uri.ToString().Contains("/RootPage.xaml") != true)
            return;

        CycleManager pCycMan = CycleManager.instance;

        e.Cancel = true;
        RootFrame.Dispatcher.BeginInvoke(delegate
        {
            if (pCycMan.GetPasswordEnabled())
                RootFrame.Navigate(new Uri("/PasswordPage.xaml", UriKind.Relative));
            else
                RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        });
    }

I had expected that the Cyclemanager instance will be created when an instance is called inside the RootFrame_Navigating()

But debugging showed me that jus after the App() constuctor , the control flow moves to the Cycle manager class and after the CycleManager() constructor it moves to the RootFrame_Navigating. !! Is there something wrong or I have understood it wrong?

Second thing is that on executing of CycleManager pCycMan = CycleManager.instance; I expected the following code in the CycleManager is being called but surprisingly it isnt. Then how does singleton property managed? or everytime a new obj is created?

    public static CycleManager Instance
    {
        get
        {
            return instance;
        }

    }

Alfah

War es hilfreich?

Lösung

If you look at the C# specification for static field initialization

[...] the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class

So the order that your breakpoints are hit is in-line with the specification, in other words static fields are initialized at some time determined by the runtime. However, the exact timing is not detailed. You should not write code that depends on the initialization order of static fields.

Your second problem is that you are accessing the field, rather than the property instance vs. Instance. You should make your static field private.

Andere Tipps

You're not actually accessing the get property in your RootFrame_Navigating method. You're directly hitting the static field (it is declared public)

public sealed class CycleManager
{
   public static readonly CycleManager instance = new CycleManager();

...

CycleManager pCycMan = CycleManager.instance;

I think you meant to type CycleManager.Instance (with a capital 'i')

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top