Back button to go back in WebBrowser WP8 youtube site - windows-phone-8

I made a WebBrowser and it works except the back button in a youtube site. Youtube have a redirect to mobile version and this cause a loop
this code not works
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
I know if it is a redirect (302) in this event?
webBrowser_Navigated(object sender, NavigationEventArgs e)

In the WebBrowserControl documentation you can find all available methods and events.
It is not very pretty but you should try that:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.GoBack();
e.Cancel = true;
}
void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.ToString().Contains("YouTubeUriYouGetAlwaysRedirectedTo"))
{
// Do some stuff here
e.Cancel = true;
}
}

Related

MediaElement Windows phone 8.1

I have a weird bug here and i don't know how to call this,but here's the thing.. i have my MediaElement in my XAML ->> <MediaElement Height="10" Width="10" x:Name="Nomes"/> and i have a Button to call that Element which is mp3 audio, and works fine C# ->>
private async void AMN(object sender, RoutedEventArgs e)
{
Nomes.Source = new Uri("ms-appx:///Sounds/AMN.mp3", UriKind.RelativeOrAbsolute);
Nomes.Play();
await Task.Delay(TimeSpan.FromSeconds(1));
VibrationDevice vb = VibrationDevice.GetDefault();
vb.Vibrate(TimeSpan.FromMilliseconds(100));
await Task.Delay(TimeSpan.FromSeconds(1));
Frame.Navigate(typeof(AmericaDoNorte));
}
Here is my SecondPage Override Method
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if(rootFrame == null)
{
return;
}
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
e.Handled = true;
}
}
And when the Vibrate Method is call i navigate through a new page,and works fine,but when i comeback to that page, the audio which supposed be to play when i hit the button play by yourself without i click, how this is possible? Thanks!
I guess your MediaElement property AutoPlay is enabled. Go to properties and have a look.
Hope this helps.
Thanks!

Windows phone 8.1 backbutton not returning to last form

When I go to the login page with this code.
private void AppBarButton_Click_1(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(LoginPage));
}
But when I push the back button on the loginpage it does not go back to the first page. Did I do something wrong?
This is the code I use in my App.xaml.cs
void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
Fixed the issue by changing the blank pages to Basic pages. Now it will de all the backbutton navigation automatic.
Fixed the issue by changing the blank pages to Basic pages. Now it will de all the backbutton navigation automatic.

Check if the current page has been reached by pressing the back button

I'm developing a little app for Windows Phone 8 that has 2 pages (1 is of course the main page).
When page2 is reached I want to check if this page has been reached by pressing the back button on the phone. I want to do something like this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (backButton.isPressed()) {
// this page has been reached by pressing the back button on the phone
} else {
// this page has been reached by NavigationService.Navigate()
}
}
Is there a native API to do that?
Is this what you need?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
...
}
}

Best way to show a passcode screen everytime an app is launched/activated

I am working on a Windows Phone 8 App which should be protected with a passcode. What is the best way to show the passcode screen everytime the app is lauchend or activated?
I think the central point of action shoule be the App.xaml.cs with its Launch and Activation event handlers. But how exactly can I show the passcode screen?
The problem is, that one never know which pages will be displayed when the app launches or is reactivated. It is either the main page or any other page which was last displayed when the app was deactivated.
I tried to intercept the navigation to the first page, cancel it and show the passcode page instead:
// App.xaml.cs
private void InitializePhoneApplication() {
...
RootFrame.Navigating += HandleFirstNavigation;
...
}
private void HandleFirstNavigation(object sender, NavigatingCancelEventArgs e) {
RootFrame.Navigating -= HandleFirstNavigation;
e.Cancel = true;
RootFrame.Dispatcher.BeginInvoke(new Action(this.OpenPasscodePage));
}
private void OpenPasscodePage() {
RootFrame.Navigate(PasscodePageUri);
}
This works, but only when the app lauchend. When the app reactivated (dormant or tombstoned) the e.Cancel is irgnored. Although the navigation to the passcode page is called the original page is shown.
Moving the navigation the the passcode page from Navigating to Navigated does not worth either:
private void InitializePhoneApplication() {
...
RootFrame.Navigated += PasscodePageAfterFirstNavigation;
...
}
private void PasscodePageAfterFirstNavigation(object sender, EventArgs e) {
RootFrame.Navigated-= PasscodePageAfterFirstNavigation;
RootFrame.Navigate(PasscodePageUri);
}
This seems to be some kind of race condition: Sometimes the passcode page is shown, sometimes the original page. Even if the passcode pages comes up this looks bad because one first see the original page for the fraction of a second before the app navigates further to the passcode page.
Both solution do not work. Any idea what is the right way to implement this?
EDIT: Meanwhile I tried a third solution which does not work either. This solution uses the Uri Mapper:
App.xaml.cs
public bool PasscodeWasConfirmed; private void Application_Launching(object sender, LaunchingEventArgs e) {
...
PasscodeWasConfirmed = false;
...
}
private void Application_Activated(object sender, ActivatedEventArgs e) {
...
PasscodeWasConfirmed = false;
...
}
public Uri InitialPageUri;
public bool ShouldRedirectToPasscodePage(Uri uri) {
if (PasswordWasConfirmend == false) {
InitialPageUri = uri;
return true;
}
return false;
}
UriMapper
public class AppUriMapper : UriMapperBase {
public override Uri MapUri(Uri uri) {
App app = (Application.Current as App);
if (app != null) {
if (app.ShouldRedirectToPasscodePage(uri))
return PasscodeQueryPage.PageUri;
}
// default
return uri;
}
}
PasscodePage
public partial class PasscodePage : PhoneApplicationPage {
...
private void PasscodeConfirmed() {
App app = (Application.Current as App);
app.PasscodeWasConfirmed = true;
NavigationService.Navigate(app.InitialPageUri);
}
}
The Logic is working without any problem, but the app does not navigate to InitialPageUri after the passcode was confirmed. The Uri Mapper is called and correctly and returns the InitialPageUri (no redirect any more). But no navigation happens...
There are no errors, exceptions or debug output. simply nothing happes...
Biggest problem when using Uri Mapper:
When the app is reactivated from Dormant state there is no navigation which could be mapped or redirected...
(I've edited previous answer instead of adding a new one)
I've spend a little time trying to find a solution, and I don't see why your code doesn't run.
In my case it's enough if I do such a change in App.xaml:
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
App.RootFrame.Navigate(new Uri("/passPage.xaml", UriKind.RelativeOrAbsolute));
}
This works on my example which is under the link http://sdrv.ms/1ajH40E
But - I cannot prevent user from seeing last screen when he holds back buton and is chosing to which app return, and then for a blink he can see the last page before leaving the app. I don't know if it is possible to change this behaviour after clicking MS Button:
windows phone change deactivated app image
Second edit
Ok - maybe I've found solution why it sometiems work and sometimes not in your code. After pressing the Start or Search buton the App can go to two cases: Tombstone and non-tombsone. After return different events happen. Code above works with Tombstone case but not with non-tombstone. To work it with the second you need to add (because page is not initialized again) - (of course it can be different solution):
bool afterActivation = false;
private void Application_Activated(object sender, ActivatedEventArgs e)
{
afterActivation = true;
}
private void CheckForResetNavigation(object sender, NavigationEventArgs e)
{
// If the app has received a 'reset' navigation, then we need to check
// on the next navigation to see if the page stack should be reset
if (e.NavigationMode == NavigationMode.Reset)
RootFrame.Navigated += ClearBackStackAfterReset;
if (afterActivation)
{
afterActivation = false;
App.RootFrame.Navigate(new Uri("/passPage.xaml", UriKind.RelativeOrAbsolute));
}
}
Please also ensure of your debug properties in VS: Project->Properties->Debug->Tombstone upon deactiovation checkbox.
You can also find some information here (if you haven't seen it before):
http://blogs.msdn.com/b/ptorr/archive/2010/12/11/how-to-correctly-handle-application-deactivation-and-reactivation.aspx

Click a button on a website using C#

I'm developing an windowsphone 8 application.
My goal is to click on a certain button on a website from my code behind (C#).
Is there anyway to achieve this?
Example:
I have a button named 'MyButton" on Mainpage. Then in the event handler:
private void BtnMyButton_Click(object sender, EventArgs e)
{
//click button 'Search' on Google
}
You could register a Javascript function to call the click event.
In codebehind:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> document.getElementById('yourButtonID').onclick(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}
Or if you're using jQuery:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> $('yourButtonID').Click(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}