Win Phone 8 : Navigating Only One Time - c#

I'm on windows phone 8 project. Doing it with xaml in visual studio.
I have 4 pivot items in this project.
I want to navigate the home page (my first pivot item) with
menu on bottom of phone;
<shell:ApplicationBarIconButton IconUri="Images/appbar_home.png" Text="Home" Click="ApplicationBarIconButton_Click_2"/>
and on click event i wrote this;
private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml?PivotMain.SelectedIndex = 0", UriKind.Relative));
}
When i run the program, it works nice, when i go other pivot items, and click that home button, it goes, but if i walk around again and click button, it doesnt work. Why ?
I need to solve this.
Thanks for the answers.

You are having this problem because you are doing it wrong.
NavigationService.Navigate is meant to navigate between XAML Files, not reload them. The phone doesn't like to reload the same page over and over.
What you want to do instead is set the current selected index to the first page.
Reference:http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.pivot(v=vs.105).aspx
It will look something like this
private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
{
PivotControlName.SelectedIndex = 0;
}

Related

WPF Press any button to continue to next page

Got a simple 3 page program, but would like to make a welcome screen that users have to press a button to move to the next page (and start the workflow).
Users will mostly be using a barcode scanner (captures string & hits enter), so I'm trying to avoid buttons that need to be clicked with a mouse.
The setup is basically one MainWindow with a DockPanel in it with a Frame in it.
Pages get loaded to the frame on events(the enter being hit on a textbox). Pages have Grids in them with the standard controls.
I've got the frame in the window navigating to the welcome page, but can't seem to capture a keypress.
What would set I the event on? The Window, the frame, the dockpanel, the page, the grid?
Also, is there a way to specify "any key or click" event, maybe listen for any input action?
I've tried experimenting and looking at documentation but I can't seem to get it.
I created an empty WPF app with a frame and was able to catch a keypress event. Once the app starts I focus the frame. You can do the same once your page loads or when your workflow starts over.
<Frame Loaded="FrameworkElement_OnLoaded" KeyUp="UIElement_OnKeyUp">
<Frame.Content>
<Page>
<Label Content="Hello"></Label>
</Page>
</Frame.Content>
</Frame>
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
((Frame)sender).Focus();
}
private void UIElement_OnKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("Key pressed");
}
Add this to your designer:
this.myForm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.myForm_KeyDown);
And add this to your Form's code
private void myForm_KeyDown(object sender, KeyPressEventArgs e)
{
if(KeysDown().Any())
{
currentForm.Hide();
nextForm,Show();
}
}

How to add an event on a menu in C# project?

I have a C# Windows Forms App that contain a menu bar.
I want to display a Help Message when I press on the "HELP" menu button.
All that I can see when I press view code is this:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I think that I need to create inside the function a MessageBox or an event that will display the desired message.
Do you have any idea how should I do this, please?
Below should work for what your asking. If you are on your form you can double click the button you want to interact with, and Visual Studio should take you to the empty method.
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This is supposed to be helpful");
}

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 ) )

Windows 8 - cache paged button visual state not changing on navigate back

I have a small (but annoying) problem.
You can quickly replicate it by doing the following:
New Project > Windows Store > Blank App (XAML)
Add a button to the grid. This also works with the default style. (note: TextButtonStyle is defined in SimpleStyles.xaml)
<Button Click="Click" Style="{StaticResource TextButtonStyle}" Content="Page 2"/>
Add the function to the code behind file:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof (Page2));
}
Next create another page, add the button, and navigate back to the MainPage in the Click event.
Next set on both pages add NavigationCacheMode="Enabled". For convenience set one of the buttons to be Left aligned and the other one Right.
Run the app. Move the mouse over the button. The state changes to reflect this. Click the button. Again the color changes. On the second page, do the same. On returning to the first page, the button is still in it's "PointerOver" visual state, since there was no PointerExited event called on the button.
How can I fix this? VisualStateMananger.GoToState() doesn't work.
We encountered a similar issue. We noticed that the state 'resets' when you hide the control.
We solved it, the dirty way:
void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
var ctrl = this.ItemContainerGenerator.ContainerFromItem(e.ClickedItem);
((Control)ctrl).Visibility = Visibility.Collapsed;
((Control)ctrl).Visibility = Visibility.Visible;
}
Maybe you could try to do the following (didn't test this):
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
((Control)sender).Visibility = Visibility.Collapsed;
((Control)sender).Visibility = Visibility.Visible;
Frame.Navigate(typeof (Page2));
}

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.

Categories

Resources