windows phone navigation immediately after loading page - c#

I have 2 pages.(MainPage.xaml,second.xaml)
MainPage.xaml is the Login page. In this page I send login and password, and receive result. I save them(result) in Isolate Storage and navigate to the second.xaml page;
When i start this application in the next time, i extract data from Isolate Storage and I want to navigate the second.xaml immidiately, but i don't know how
I try write
public MainPage()
{
InitializeComponent();
//function for Isolate storage
InitializeSettings();
NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
}
But it isn't work)
I understood that I could not use the navigation code associated with the MainPage() constructor. Of course, i may will do simple button, but i wish fast navigation
I think may be it connected with App.xaml method
private void Application_Launching(object sender, LaunchingEventArgs e)
for example, write my method
//function for Isolate storage
InitializeSettings();
with navigation there?(navigation not work in this example)
private void Application_Launching(object sender, LaunchingEventArgs e)
{
InitializeSettings();
NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
}
Where I can use navigation, so go straight to the second.xaml page, without fully loading the MainPage.xaml(may be without MainPage.xaml)

You can do as Rana Tallal said.
Or you can write it in code:
public MainPage()
{
InitializeComponent();
Loaded += (s, e) =>
{
InitializeSettings();
// Some login-password check condition
if (_login && _password)
NavigationService.Navigate(new Uri("/Conversation.xaml",
UriKind.Relative));
}
}

Well create a new function... and in that perform the checks at which you want it to be navigated and if the checks are ok right in it than call the navigation service navigationservice.navigate(....) code.
Now you need to tell the program to call this function when the mainpage is completely loaded. To do so in the xml of mainpage inside tags at the end of it write loaded="function_name"
Now when ever the page will be loaded this function will be called. If the login information is present in the isolated storage than the navigation sevices will be called otherwise the mainpage will be shown.
Make sure to put (object sender, RoutedEventArgs e) in the functions parameters(as it is a event handler).

Related

How do I use the event VisibilityChanged?

I need to re-run the code contained within dela method OnNavigatedTo () when the app resumes from background.
To do this I need the event VisibilityChanged:
Link MSDN
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//My code
}
With this event, each time the app is opened again from the background, the code contained within dell'OnNavigatedTo runs again. How can I use that event? I can not.
Visibility changed is only relevant if the page isn't in the background. Every time the app opens it goes to OnNavigatedTo(), you can make a bool or counter to check if it is the first time you entered the page and then decide what to do based on that inside OnNavigatedTo. for Example:
private override void OnNavigatedTo(NavigationEventArgs e)
{
if(hasBeenHere) Repeat_Visit(args);
else First_Visit(args);
}
The Application_Activated Event gets fired when the application is resumed from the background.
From the App.xaml.cs of the WP8.1 Template:
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// your code
}
Edit : to call methods on MainPage from app.xaml.cs has already been answered here : How to use a method in MainPage from app.xaml.cs with variable appbar pivot

Windows Phone: RemoveBackEntry after navigate failing

I've got an intermediate "Loading" page for my game: I send them there and it has "Loading..." text that displays while the rather hefty game page loads up:
private void OnLoaded(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
try
{
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
NavigationService.RemoveBackEntry();
}
catch (InvalidOperationException)
{
}
});
}
Then when you hit the back button you go to the main page rather than back to the loading screen. There's no other logic on the page.
However I just got a store submission declined: apparently on the Samsung Focus and Odyssey the navigation entry for the loading page didn't get removed, and the user is sent back to the loading page when they hit the back button, rather than back to the main menu. I'm guessing RemoveBackEntry failed.
This looks like it should work, I can't reproduce the error and I don't have a Focus or Odyssey to work with. Does anybody know what might be going wrong?
I think you should replace:
NavigationService.RemoveBackEntry();
with:
while (NavigationService.CanGoBack)
{
NavigationService.RemoveBackEntry();
}
You have to remember that NavigationService.Navigate will perform the navigation asynchronously. So when you call NavigationService.RemoveBackEntry(), the current page might not yet be on the BackStack.
To fix that, call RemoveBackEntry in OnNavigatedTo of GamePage.

Windows Store Frame Navigation: How to get the source page?

On windows store apps we usually use something like this:
this.Frame.Navigate( typeof(ItemDetailPage), itemId);
To navigate between the app's different pages.
After the page I want to navigate to is open, how can I know it's navigation source? How can I get the previous page type?
You are going to want to create a navigated event
void NavigationService_Navigated(object sender, NavigationEventArgs e)
{
//Your code here
}
(per http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.navigated.aspx)
Then once you have this event you can look at the NavigationEventArgs
This will allow you to access the Navigator that called the event.
From THERE you should be able to grab the Navigator's Uri

Code in Application_Activated not running when phone wakes up

I've done some reseach on the life cycle of Windows Phone apps and I've gathered that when the phone is locked whilst an app is still running, and you unlock the phone the 'Application_Activated' function is called in the App.xaml.cs file.
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
//Code to run
MessageBox.Show("Hello there!");
}
Now in the above example, the simple 'MessageBox' call doesn't get run. Like I said, if you have your app running and you lock the phone, and then unlock the phone the above code is expected to run, in this case display a MessageBox as soon as you unlock the phone.
Any help would really be appreciated! Thanks.
You can not do that
If you call Show(String) method from the app Activated and Launching event
handlers an InvalidOperationException is thrown with the message Error
Displaying MessageBox.
it is in msdn
if you wanna show same message my suggestion is to use OnNavigatedTo event
EDIT
if i understood correctly you wanna change default page navigation
1.One way to do this:
In WMAppManifest.xml replace the property of Navigation Page with your desire page
An alternative:
In WMAppManifest.xml remove the property of Navigation Page
private void Application_Launching(object sender, LaunchingEventArgs e)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
}
This way you can "play" with IsolatedStorageSettings for example
if (boolvariable)
{
RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative));
boolvariable = false;
}
else
{
RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
It just an idea, let me know how it goes (:

Call an event on Web Control Load

I am loading my with web controls and loading them based on the URL parameters.
I need to load a gridview if the user is in :&cat=8
My web control has a method which I need to call. One of its parameters is a session which is created only when the category is 8.
Technically I need a way of calling methods within my web control from my page. Placing it on page_load results in an error.
Thanks
You need to bind an event-handler on Load event of the Control itself.
private void Page_Load (object sender, System.EventArgs e)
{
// Add the following code:
yourControl.Load += new EventHandler(yourControl_Load);
}
private void yourControl_Load (object sender, System.EventArgs e)
{
//your code here.
}

Categories