0

I have a simple Windows Phone 8 application. When App starts, I need to authenticate user, doing a server call. In InitializePhoneApplication(), my WP8 application checks if this data was already obtained in a previous call and stored in IsolatedStorage. If no data is in the IsolatedStorage, a server call is performed. Code:

if ((IsolatedStorageSettings.ApplicationSettings.Contains("loggedin")) && 
    (Convert.ToString(IsolatedStorageSettings.ApplicationSettings["loggedin"]).ToLower() == "y"))
{
    RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
else
{
    SC = new ServiceClient();
    SC.CheckDeviceCompleted += SC_CheckDeviceCompleted;
    SC.CheckDeviceAsync(Utility.GetStatus());
}

Here is the code of SC_CheckDeviceCompleted(). This event is raised when the CheckDeviceAsync() returns (on service):

private void SC_CheckDeviceCompleted(object sender, CheckDeviceCompletedEventArgs e)
{
    string res = e.Result;
    if (!res.Equals(""))
    {
        IsolatedStorageSettings.ApplicationSettings["loggedin"] = "y";
        IsolatedStorageSettings.ApplicationSettings["id"] = res;
        RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }
    else
    {
        RootFrame.Navigate(new Uri("/Authentication.xaml", UriKind.Relative));
    }
}

Data received from server are used to select the start page of the application. If an empty string is received, no data is registered on server: so, user must login. Otherwise, user can immediately access the main page of the App.

The problem is that this code does not work. Specifically, server call is not received from service (service does not receive anything), so the SC_CheckDeviceCompleted() method is never executed (because nothing is received from service) and no page is showed.

SC instance is defined as a public static property in App.xaml.cs, App class:

public static ServiceClient SC { get; set; }

My service works perfectly when called from any other page (where I use local ServiceClient variables and not the ServiceClient property defined in App.xaml.cs) of my WP8 application. I can't do service calls from App.xaml.cs file using the ServiceClient property defined in it. Why? What is the problem?

smn.tino
  • 2,272
  • 4
  • 32
  • 41

1 Answers1

0

The main purpose for App.xaml is for holding resources and some lifetime events of the pages such as Application start and exit. You can't make a service call inside it. Just do that in the main page with same logic.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks Sajeetharan for your answer. I need service's response to select the right page (main page or authentication page). Is there any way to do that? – smn.tino Jul 06 '14 at 17:34
  • create a page say Landing.xaml, in the Landing.xaml.cs -> make a call to the service, get the response and based on the reponse navigate the user to the desired page, hope it helps :) – Rahul Reddy Jul 07 '14 at 16:21