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

Keine korrekte Lösung

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