I want to make an authentication in the App.xaml.cs class that if user exists in the database I want to direct user to member page otherwise want to direct user public page. I know how to use rootframe navigating but the problem is even though my webervice async method is written before RootFrameNavigating its not executed before it. my code is here
in App constructor
dclient.GetUserByPhoneIdCompleted += new EventHandler<GetUserByPhoneIdCompletedEventArgs> (dclient_GetUserByPhoneIdCompleted);
dclient.GetUserByPhoneIdAsync(GetDeviceUniqueID());
if (getUserbyPhoneIdCompleted)
RootFrame.Navigating += new NavigatingCancelEventHandler(RootFrame_Navigating);
and in my method
if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
return;
e.Cancel = true;
RootFrame.Dispatcher.BeginInvoke(delegate
{
if (userId == -1)
RootFrame.Navigate(new Uri(string.Format("/View/PublicPage.xaml"), UriKind.Relative));
else
RootFrame.Navigate(new Uri(string.Format("/View/WelcomePage.xaml"), UriKind.Relative));
});
and in my method in webservice returns integer and I get the user Id there
like this
void dclient_GetUserByPhoneIdCompleted(object sender, GetUserByPhoneIdCompletedEventArgs e)
{
userId = e.Result;
getUserbyPhoneIdCompleted = true;
}
Your application starts, you send an asynchronous call to the webservice. Then the navigating event is triggered (why wouldn't it be? You're not blocking the thread), and your event handler is called. There, you're canceling the navigation, and checking the value returned by the webservice. But since there isn't any kind of waiting, you can reach that point before the webservice call has ended. There's many ways to fix that, but the one I would recommend is:
Don't forget to handle the case when the webservice call returns an error.