Windows Phone 8 open new page programmatically - c#

I've started WP 8 development recently. I know C# a bit but not much. BTW, I'm trying to open a page pragmatically, but app is crushing.
My code is here
NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
But I'm being confused because it's working when I'm placing the code above within button click event code block.
ERROR Detail An exception of type 'System.NullReferenceException'
occurred in TestProgram.DLL but was not handled in user code
If there is a handler for this exception, the program may be safely
continued.
I need your advice.
EDIT: Code Added
Credens MyCred = new Credens();
// Constructor
public MainPage()
{
InitializeComponent();
if (MyCred.ifExists("api_key"))
{
NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
}

You cannot use the NavigationService in the constructor. Put your code to the OnNavigatedTo event and it will not crash
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (MyCred.ifExists("api_key"))
{
NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
}

Did you follow this tutorial step by step ?
You code seem right. As you said, you should have something like this :
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
}
Does you page are on the same folder ? Did you check the path ? Does you page exist ? Can you boot in it ? If you add a break point on the NavigationService, where failed it ?
I think this documentation is pretty helpful.

Try calling Navigate() on the PhoneApplicationPage Loaded or OnNavigatedTo() events.

Related

Action when website changes

how can i make action when link in WebBrowser1 changes?
I tried to do if(webbrowser1 == https://www.google.nl/intl/en/about/)but it doesn't work
I mean at startup in WebBrower1 is http://www.google.com and when i click something in this WebBrowser1 link changes, for example https://www.google.nl/intl/en/about/ and my question is, how can make an action (MessageBox.Show("you are here"); when i'm on main google page and i'm go to about page
This is "Windows Forms Application"
I guess you could do it using Navigated event.
You can also find an example and how to catch and redirection moment here: https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated(v=vs.110).aspx
I believe this example will help you:
// Shows ModalWindow upon navigation.
private void webBrowser1_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
{
MessageBox.Show("you are here:" + WebBrowserNavigatedEventArgs.Url);
}

Set windows phone store application start up page instead of MainPage

I am new to windows phone 8 development, I have MainPage.xaml which is start up page by Default, I have used this page as a login page, But every time I run my application it opens login page and need to put username and password, Instead of MainPage I want to set UserList.xanl as my start up page.
the next event is MainPage.xaml page_loaded event,
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/UserList.xaml", UriKind.Relative));
}
I dont want to use above code,
I have googled around but there was not any clue? Can anyone help me? Thanks
Did you try changing it in your WMAppManifest?
Reference: Setting The Start Page in Windows Phone 7 Application
You can change startup page in the app.xaml.cs file.
Find this method:
protected async override void OnLaunched(LaunchActivatedEventArgs e)
and change MainPage to UserList here:
if ( !rootFrame.Navigate( typeof(MainPage), e.Arguments ) )

which method is first called when a xaml page is loaded in windows phone by default?

I want to know which method is first called by default when a xaml page is loaded in windows phone app and how can I change the method which has to be called first on load?
To automatically perform an action on page load, use this in your page constructor:-
public MainPage()
{
InitializeComponent();
Loaded += (s, e) =>
{
//write logic here
}
}
You can also set the Loaded handler via xaml:
.xaml:
<Page
...
Loaded="OnPageLoaded">
.xaml.cs:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
...
}
To directly answer your question: Initialize is the event you are looking for.
For more detailed information, Google is your friend:
Application lifecycle - http://msdn.microsoft.com/en-us/windowsphonetrainingcourse_applicationlifetimewp7lab_topic2.aspx
Controls and other objects should follow the standard event lifecycle:
http://msdn.microsoft.com/en-us/library/ms754221.aspx

C#, Windows Phone 7, Using if-else statement to check which page is displayed

Now I'm developing a Windows Phone 7 app, my app has three buttons located at the applicaton bar. Here is the event handler for these three buttons:
//app bar page navigation
private void ApplicationBarIconButton_Click1(object sender, EventArgs e)
{
if(//check if the current displayed page is mainpage)
{
//do nothing
}
else
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
}
private void ApplicationBarIconButton_Click2(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Audios.xaml", UriKind.RelativeOrAbsolute));
}
private void ApplicationBarIconButton_Click3(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Statistics.xaml", UriKind.RelativeOrAbsolute));
}
The button navigation works well except the first one (Button_Click1) because when I first access the main page and click the first button the app will automatically go back to app list.
So I want to use if-else statement to check which page is currently displayed and then decide whether to navigate or stay in the current page.
It looks like you are using the ApplicationBar like you would use a TabBar within iPhone (or similar in Android, Bada, etc too)
In WP7, the Metro style is typpically to use a Pivot or Panorama rather than a "Tab Bar" for this type of navigation.
If you do want to use the ApplicationBar like this:
then you can (the WP7 Marketplace will allow it) but users might feel it's not very Metro.
then you might want to consider disabling the appropriate button rather than just stubbing out the action.
then you can detect the currently shown page using CurrentSource on the NavigationService
Also, please do note that if you try to navigate from MainPage.xaml to the same url MainPage.xaml then you will see an exception - as I recall, the navigation service complains about url fragments not being supported.
Button click1 should be removed from the main page. It makes no sense to have that button there.
Other pages should use the back button to return to the main page. Otherwise you mess up your back stack.

Transition between phone pages

I'm developing a Windows Phone application.
Is there anyway to animate transition between phone pages?
Now, I'm using this to navigate between pages:
NavigationService.Navigate(new Uri("/Views/SelectComponent.xaml", UriKind.Relative));
Thanks.
What you could do is have a "Page1Out" storyboard and "Page2In" storyboard. Then, assign a Completed event handler to the storyboard like this:
Page1Out.Completed += new EventHandler(Page1Out_Completed);
Then handle that event
void Page1Out_Completed(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/Views/SelectComponent.xaml", UriKind.Relative));
}
This will play your out transition and then load the new page. In the new page, you want to do something similar, but handle it in the OnNavigatedTo() event.
Hope that helps
The same question with answers can be found here

Categories

Resources