Using Animations On Windows Phone - c#

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();
}

Related

Ignore tap until page fully loaded

I have page1 with a button that opens page2. Page2 takes approx 1 second to open and contains a list of options (user controls) that when tapped, saves the selection and returns the user to page1.
When the user taps the button on page1, if they become impatient, they will tap it again before page2 has loaded/displayed. the problem is, that the tap is recognised and the first option on page2 is tapped, even though it's not yet visible. This then saves the selection and returns the user to page1.
Is there anyway I can stop the tap/click event being recognised until the page2 has fully loaded?
One idea may be to subscribe to the Tap/Click event in OnNaviagatedTo on first page, then inside Tap/Click event unsubscribe it, so that user won't be able to click it again. Then when he navigates back, OnNavigatedTo subscribes again. You may also think about flag that will be set up upon Tap/Click and dismissed in Loaded event of second page. Sample:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedTo");
base.OnNavigatedTo(e);
this.button.Click += Button_Click;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Click -= Button_Click;
await Task.Delay(1000); // simulate heavy load of second page
this.Frame.Navigate(typeof(SecondPage));
}
}

Detect the bottom of a WebBrowser Page in Windows Phone 8

I have a xaml page in which I have used a webbrowser control.I need to scroll down to the web page and once the bottom is reached,I have to enable a button.
Thanks in advance!
There's two part to this. The first is to detect, in Javascript, the moment when the browser reaches the bottom of the page. The second is to forward the event to the C# code.
Say you ask your WebBrowser control to navigate to a given page. First, in the C# code, subscribe to the Navigated event to inject the proper Javascript code:
private void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
const string Script = #"window.onscroll = function() {
if ((window.innerHeight + document.documentElement.scrollTop) >= document.body.offsetHeight) {
window.external.notify('bottom');
}
};";
this.WebBrowser.InvokeScript("eval", Script);
}
This Javascript code uses the window.external.notify method to notify the C# code. To receive the notification, you need to subscribe to the ScriptNotify event of the WebBrowser:
private void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "bottom")
{
// Reached the bottom of the page
}
}

How to get info about previous page on Frame.GoBack()

Say we have some Page PageA and I have a button that, when clicked, does the following:
Frame.NavigateTo(typeof(PageB));
After the user is done doing stuff, he navigates back from PageB to PageA calling Frame.GoBack()
I want be able to determine that I'm navigating back from PageB
I could use:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
e.NavigationMode
}
But this only tells me that I'm navigating back, not that I'm navigating back from PageB.
Is this even a good windows-phone-guidelines approach (did not find this particular case in the docs)?
I think you should be able to do it by using Frame.ForwardStack property which holds forward navigation history.
A short sample which should work:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var lastPage = Frame.ForwardStack.LastOrDefault();
if (lastPage != null && lastPage.SourcePageType.Equals(typeof(desiredPage)))
{ /* do something */ }
}

Is there some way to go on the previous page get the pressed button text?

I'm working on Visual Studio, in C# language
I would like to ask if there's someway to go back to get the ButtonX.Text I pressed on the previous page, some sort of Login without a password.
Basically I need a worker to specify which person he is by clicking on their name (button) then it goes foward to the next page I have a label on the MasterPage but it resets everytime it goes on a next page what I would like to do is keep the info there
If you need some code tell me.
Thanks
You could use session variables?
On the button click handler on the first page...
protected void button_Click(object sender, EventArgs e)
{
Session["Worker"] = button.Text;
}
Then on the second page...
Label.Text = Session["Worker"];
Based-off your reply to Zollistic's answer, you could do this...
Apply this event to all your all your worker buttons...
protected void button_Click(object sender, EventArgs e)
{
if (Session["Worker"] == null) Session["Worker"] = "";
Session["Worker"] += button.Text + ",";
}
Now Session["Worker"] has a character-delimited list of all the clicked buttons. The character in this example is a comma; but you can change it to whatever you want (i.e. a pipe "|").

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

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!

Categories