How to add page into Navigation stack?( Windows Phone) - c#

when I navigate to Page1.xaml, I have an empty navidation stack, what I need to add into
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){}
to add Page2.xaml into Navigation stack (I need to navidate into Page2.xaml only when I press go back button)

If I understand correctly, you want to navigate to Page2.xaml when the user press the Back button, correct?
You'll have to use the BackKeyPressed event to make that work, like so:
public MainPage()
{
InitializeComponent();
this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
}
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
});
}
But please be advised that changing the default behavior of the Back button may lead to fail app certification!

Related

BackButtonPressed issues in Windows Phone 8.1?

I have a popup window in my windows phone 8.1 runtime application.
While back button pressed and popup is opened in a page, the app should stay in the page itself, else it should go back. This is my concept. So, I coded like below:
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
if (PopupWindow.IsOpen)
{
PopupWindow.IsOpen = false;
e.Handled = true;
}
}
Even if the popup windows is open in the page, the app goes to the previous page. I used the same logic in windows phone silverlight application and that worked.
NOTE: I'm using Basic Page.
What mistake actually I'm doing ?
Check two things:
by default in NavigationHelper, HardwareButtons_BackPressed lacks checking if the event was already handeled, try to improve it:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
// if (this.GoBackCommand.CanExecute(null)) // this is as a default
if (this.GoBackCommand.CanExecute(null) && !e.Handled) // add a check-up
// ... rest of the code
look at your App.xaml.cs file, and in App() there is HardwareButtons_BackPressed subscribed (check if subscribed method also navigates back):
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
// HardwareButtons.BackPressed += HardwareButtons_BackPressed; // this line also could fire Frame.GoBack() (as default project template)
// of course check what is in the above method
}
Also remeber that events are fired in the order you have subscribed them and for example Navigation helper subscribes in Loaded event. If you subscribe after then the navigation will be first. You may subscribe before or maybe use a flag.
I resolve in thi way
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
protected virtual void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
e.Handled = true;
}

Windows Phone: how to change the automated navigation path?

I use TimeSpanPicker in my app. When I pick my time from the timer, it will go back to the initial page automatically (lets say setTime.xaml page). However, I want to change the navigation to another page rather than the setTime.xaml page.
enter code here
>> setTime.Xaml.cs Page
TimeSpanPicker tsp = new TimeSpanPicker();
tsp.ValueChanged += change_Value();
>> Event Handler
private void change_Value(object sender, RoutedPropertyChanedEventArgs <TimeSPan> e)
{
//do something
}
How can I change its navigation to another page?
Add a event for ValueChanged. Write the event handler like this:
private void TimePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}

Show other view at application first launch

I have inherited some Windows Phone code that I must change. I need to show the user a tutorial about how to use the application when he first launches it. However, I cannot manage to change the current view...
Here is my code:
public LoginView()
{
InitializeComponent();
this.DataContext = new LoginViewModel();
if (ApplicationFirstLaunched() == true)
{
NavigationManager.Current.Navigate(ApplicationView.DemoView);
}
}
The ApplicationFirstLaunched function works fine (I use IsolatedStorageSettings to store a boolean value), but the view never changes.. I thought that maybe the Navigate call was wrong so I created a button in my view and assigned its Click property to this function:
private void demoBtn_Click(object sender, RoutedEventArgs e)
{
NavigationManager.Current.Navigate(ApplicationView.DemoView);
}
When I click the button, the view changes and the tutorial pops up. What to do to show another view at first launch? Thanks
Navigate in the OnNavigatedTo method.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (ApplicationFirstLaunched() == true)
{
NavigationManager.Current.Navigate(ApplicationView.DemoView);
}
}

Handling Delegates on Windows Phone

I'm having a problem with the QueryString in my application.I have two buttons in my mainpage, when the user clicks on a button, the application navigates to the second page and stores the button ID on the Querystring.
Now, the problem I'm having is that if a user clicks on a button the Querystring "ID" key is set to the corresponding ID value BUT that value of ID then becomes persistant.
ie
Click Button1 => NavigationService("/Page2.xaml?ID=1",UriKind.Relative) => Check incoming ID value in onNavigated event of page2 and youu find that Querystring successfully set to ID in the second page.
but if you click the back button then press button2
Click Button2 => NavigationService("/Page2.xaml?ID=2",UriKind.Relative) => Check QueryString value in onNavidated event on page2 and you find that the ID key still as a value of 1.
If I had started off by pressing button2 then the ID key would have a persisted value of 2.
I have absolutely NO idea what could be going on.I have animated page transition between pages but I don't know how that could affect things.Does anyone have any ideas of what could be happening? Is there a way of reseting the whole app if I navigate back to the mainpage?
In MainPage
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
animation1.Stop();
animation2.Stop();
animation1.Begin();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
animation2.Begin();
animation2.Completed += (x, y) => NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=1", UriKind.Relative));
}
private void button2_Click(object sender, RoutedEventArgs e)
{
animation2.Begin();
animation2.Completed += (x, y) => NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=2", UriKind.Relative));
}
private void button3_Click(object sender, RoutedEventArgs e)
{
animation2.Begin();
animation2.Completed += (x, y) => NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=3", UriKind.Relative));
}
In Page2
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo(e);
animation1.Stop();
animation2.Stop();
animation1.Begin();
}
By the way I have commented out the entire Page2 Page Loaded event.
UPDATE
One thing I have noticed is that the program doesn't even run the second Navigation call, it only runs the first, I know this because if I change the Uri source in button2 to an empty string and run the program with button1 being the button that is pressed first, I will get the querystring value stuck at 1 no matter which button I go back and press.When I look into the NavigationEventArgs e object in the OnNavigatedTo event in page2, it's URI source will always be the first uri source.
My guess right now is that the animation2.Completed event is acting as some sort of multicast delegate of sorts, Whatever gets put first into it is whatever will get run.Also, I removed the animations and just had the Navigation statement, the program worked fine but without the animation.Does anyone know how I can clear the animation2.Completed event? Or have any ideas on an alternate implementation.
Basically, you're adding a Completed handler to your storyboard. The Completed handler navigates to page 2. Then you go back, and press the second button. Since you went back to previous page, you're re-using the same page instance, and therefore the same Storyboard instance. Now you're adding a second Completed handler to your Storyboard. So when the animation finishes, the Storyboard has two Completed handlers, the first one gets executed first, thus navigating to Page2 with the first parameters.
Instead of adding a Completed handler every time, just do it once for all, and store the URL in a property:
In your XAML:
<Storyboard x:Key="Animation2" Completed="Storyboard_Completed">
<!-- whatever-->
</Storyboard>
Then in your C# code:
protected string Uri { get; set; }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
animation1.Stop();
animation2.Stop();
animation1.Begin();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Uri = "/fooApp;component/Page2.xaml?id=1";
animation2.Begin();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.Uri = "/fooApp;component/Page2.xaml?id=2";
animation2.Begin();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
this.Uri = "/fooApp;component/Page2.xaml?id=3";
animation2.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
this.NavigationService.Navigate(new Uri(this.Uri, UriKind.Relative));
}
Bonus:
Removing an event handler
If you want to remove an event handler, simply use the -= operator. But then you can't use a lambda, you have to use a 'classical' method:
// Good
this.animation2.Completed -= Storyboard_Completed;
// Wrong
this.animation2.Completed -= (sender, e) => NavigationService.Navigate(new Uri("RandomText_JustWantCompletedEventToBeHit", UriKind.Relative));
Alternative implementation
If all your buttons do is to trigger the animation and redirect to Page2, then you can handle it in a more generic way thanks to the Tag property of the controls. The Tag property allows you to to store any object you want to identify the control.
XAML:
<Button Click="Button_Click" Content="Button 1" Tag="1" />
<Button Click="Button_Click" Content="Button 2" Tag="2" />
<Button Click="Button_Click" Content="Button 3" Tag="3" />
C#:
private void Button_Click(object sender, RoutedEventArgs e)
{
var element = (FrameworkElement)sender;
this.Url = "/fooApp;component/Page2.xaml?id=" + element.Tag.ToString();
animation2.Begin();
}

Using Animations On Windows Phone

When I use a page animation to navigate from one page to another, I navigate fine to the second page but I can't navigate back to the main page (using the hardware back button) .When I try to navigate back I find all controls on the page gone.
Code to go the second page
private void Button1_Click(object sender, RoutedEventArgs e)
{
//Run animation then navigate to second page
myAnimation.Begin();
myAnimation.Completed += (s,ev)=>
{
NavigationService.Navigate(new Uri("/nextPage.xaml?id=Button1",UriKind.Relative));
};
}
Code on second page
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo(e);
myAnimation.Begin(); //Another Animation
}
My guess is that your storyboard moves the controls outside of the view. When the user presses the back button, the previous page is restored in the exact state you left it. So the controls are still be hidden from the view.
To solve the issue, simply reset the storyboard when the user navigates to the page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myAnimation.Stop();
}

Categories