locked
Call fucntion on App load only once

    Question

  • Hi

    I call a Windows live SDK logon function, GreetUser(); when the apps loads on start-up as below. If I navigate to another page then back again the logon calls again.

    How can I make it call the function only once?

     public sealed partial class GroupedPicturesPage : LayoutAwarePage
        {
         
    public GroupedPicturesPage()
            {
               
                this.InitializeComponent();
               
              
                    GreetUser();
            }




    • Edited by Andy Sid Monday, January 5, 2015 1:18 AM
    Monday, January 5, 2015 1:16 AM

Answers

  • I'm sure a real programmer would use something more sophisticated. I am a fake programmer. So, I use very simplistic solutions for this kind of thing.

    In the App.xaml.cs page, create a static bool variable.  This will be your app global variable.  Initialize it to true. 

    static public bool firsttime = true;

    Now, in the page that you posted the code from, modify it to this.

     public sealed partial class GroupedPicturesPage : LayoutAwarePage
        {
         
    public GroupedPicturesPage()
            {
               
                this.InitializeComponent();
               
                if(firsttime==true) {
                    GreetUser();
                    App.firsttime = false;
                }
            }

    And voila.  GreetUser() will only fire the first time around.  After that, it won't fire again.
    Monday, January 5, 2015 5:28 AM

All replies

  • I'm sure a real programmer would use something more sophisticated. I am a fake programmer. So, I use very simplistic solutions for this kind of thing.

    In the App.xaml.cs page, create a static bool variable.  This will be your app global variable.  Initialize it to true. 

    static public bool firsttime = true;

    Now, in the page that you posted the code from, modify it to this.

     public sealed partial class GroupedPicturesPage : LayoutAwarePage
        {
         
    public GroupedPicturesPage()
            {
               
                this.InitializeComponent();
               
                if(firsttime==true) {
                    GreetUser();
                    App.firsttime = false;
                }
            }

    And voila.  GreetUser() will only fire the first time around.  After that, it won't fire again.
    Monday, January 5, 2015 5:28 AM
  • Hi

    Works perfect and using this approach. I am sure there are other ways also.

    Thank you

    Monday, January 5, 2015 7:42 AM