How to detect back button click and make webview go back - c#

I am trying to build a webview app for one of my projects using Xamarin but I can't seem to figure out how to make the webview element go to previous page instead of closing the app.
So I have figured out how to detect the back button being pressed and prevent it from closing the app but I want to make the web page go back and if the web page can't go back then close the app.
Here's my code at the moment:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Application = Xamarin.Forms.Application;
namespace myNewApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class WebPage : ContentPage
{
public object _browser { get; private set; }
protected override bool OnBackButtonPressed()
{
base.OnBackButtonPressed();
return true;
}
public WebPage()
{
var browser = new Xamarin.Forms.WebView();
browser.Source = "https://myurl.com";
Content = browser;
}
}
}
I have tried a couple of answers and I found this bit of code but it doesn't work as the override can't access the public WebPage browser var:
if (browser.CanGoBack)
{
browser.GoBack();
return true;
}
else
{
base.OnBackButtonPressed();
return true;
}
Any help would be much appreciated.

you need to make browser a class level variable so you can access it anywhere in your page.
public class WebPage : ContentPage
{
Xamarin.Forms.Webview browser;
protected override bool OnBackButtonPressed()
{
base.OnBackButtonPressed();
if (browser.CanGoBack)
{
browser.GoBack();
return true;
}
else
{
base.OnBackButtonPressed();
return true;
}
}
public WebPage()
{
browser = new Xamarin.Forms.WebView();
browser.Source = "https://myurl.com";
Content = browser;
}
}

Related

How could I close my sidebar without closing my app in C# xamarin forms android?

I'm working with C# xamarin forms android and I'm trying to close my sidebar when the user touches de backbuttom but I'm not able to do it. Instead, my aplications closeses.
HomePage.xaml.cs code:
public partial class HomePage : ContentPage
{
string[] subs;
public HomePage()
{
//NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
my sidebar AppShell.xaml.cs code:
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
nameuser.Title = Preferences.Get("displayName", "Default");
versionadoAPP.Text = "v" + $"{VersionTracking.CurrentBuild}.{VersionTracking.CurrentVersion}";
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
Image of my app:
How could I resolve that?
Thank tou very much!
The easiest way that do this is to set the flyout presented property.
Try something like this:
Shell.Current.FlyoutIsPresented = false;
Good luck!

How to remove huge title bar from Xamarin.Forms WPF application?

I've got a Xamarin.Forms WPF application running, but there is a huge title bar.
I have tried the solution of adding NavigationPage.SetHasNavigationBar(this, false); inside my MainPage class's constructor
Here is the code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
}
Still I am getting huge title bar size in WPF application, on the other hand, while running on UWP, it is working fine.
You could try to add below codes in your MainWindow.xaml.cs in wpf project:
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (!topBarsRemoved) RemoveTopBars();
}
private bool topBarsRemoved = false;
private void RemoveTopBars()
{
var topAppBar = this.Template.FindName("PART_TopAppBar", this) as FormsAppBar;
if (topAppBar != null)
(topAppBar.Parent as System.Windows.Controls.Grid)?.Children.Remove(topAppBar);
topBarsRemoved = true;
}

Xamarin Forms Master Detail Keep Master on the left

I am trying to keep the Master page (menu) to the left of My MasterDetailPage at all times, I do not want the navigation to take the user away from the MasterDetailPage and make the user press a back button to get back.
I am looking a traditional burger bar menu to the left that simply changes the Detail page and keeps the master in place.
What I have now:
public partial class App : Application
{
public static NavigationPage NavPage { get; private set; }
private static RootPage RootPage;
public static bool MenuIsPresented
{
get
{
return RootPage.IsPresented;
}
set
{
RootPage.IsPresented = value;
}
}
public App()
{
InitializeComponent();
MenuPage menuPage = new MenuPage();
NavPage = new NavigationPage(new FirstPage());
RootPage = new RootPage();
RootPage.Master = menuPage;
RootPage.Detail = NavPage;
MainPage = RootPage;
}
...
}
And my ViewModel Command
async void GoSecondPageAsync(object obj)
{
await App.NavPage.PushAsync(new SecondPage());
App.MenuIsPresented = false;
}
But this just creates a new page on top of the stack with a back button back to the MasterDetailPage, while all I want is to stay within the MasterDetailPage.
You can push the new page and just remove the first one from the NavigationStack For example:
async void GoSecondPageAsync(object obj)
{
await App.NavPage.PushAsync(new SecondPage());
var removePage = App.NavPage.Navigation.NavigationStack[0];
App.NavPage.Navigation.RemovePage(removePage);
App.MenuIsPresented = false;
}
Although, if you just always want to replace the page do you really even need the NavigationPage and can't you just set the RootPage.Detail=new SecondPage();
You just need to update the Detail property to point to the new page. Like this:
async void GoSecondPageAsync(object obj)
{
RootPage.Detail = new NavigationPage(new SecondPage());
App.MenuIsPresented = false;
}

OnBackButtonPressed Not Firing Xamarin Forms

I am trying to prevent the user of my app from pressing the hardware back button.
I have found this code snippet that is in the code behind the xaml file:
protected override bool OnBackButtonPressed()
{
return true;
}
I have tried variations of this including using Boolean instead of Bool and returning base.functionname nothing seems to fire this method.
Here is the bigger context:
Code behind:
namespace Watson.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StartScan : ContentPage
{
public StartScan()
{
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
}
This is the second page in the stack and disabling the button only needs to happen on this page only, no where else.
Any help would be appreciated.
This is working for me, I tried in Android and iOS platforms using Xamarin.Forms.
Hope you can resolve with this piece of code.
namespace Test
{
public partial class TestPage2 : ContentPage
{
public TestPage2()
{
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
//return base.OnBackButtonPressed();
return true;
}
}
}
Thanks,
You can also add NavigationPage.SetHasBackButton property in the XAML
NavigationPage.HasBackButton="True"
In the Content Page
You can do this way:
namespace Watson.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StartScan : ContentPage
{
public StartScan()
{
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
// you can put some actions here (for example, an dialog prompt, ...)
return true;
}
}
}
For me it is working with the following code (Samsung Android)
protected override bool OnBackButtonPressed()
{
//base.OnBackButtonPressed();
browser.GoBack();
return true;
}
OnBackButtonPressed firing when you click the back button on your device, not from your navigation page, do your logic in OnDisappearing!

Is there a way to clear the Navigation Service or not register a frame?

I'm developing an aplication for Windows 7, and I need to block a page.. my solutions are not register that frame or clear all the frames in back.
Guessing that you mean you have a page on the Back-stack that you want to remove --
In the new Mango SDK, there's a Method you can try NavigationService.RemoveBackEntry
However it might be easier to just use a boolean and check in OnNavigatedTo:
At page where you need to go back > 1 pages:
in App.xaml.cs:
public static bool IsBackwardNavigation = false;
public static string PageContext = string.Empty;
Page2.xaml.cs :
public void YourFunction()
{
App.PageContext = "MainPage";
App.IsBackwardNavigation = true;
if (NavigationService.CanGoBack)
NavigationService.GoBack();
}
And in each page's OnNavigatedTo:
Page1.xaml.cs:
string Page1 = "Page1";
protected override void OnNavigatedTo(object sender, NavigationEventArgs e)
{
if (App.IsBackwardNavigation)
{
if (!Page1.Equals(App.NavigationContext)
{
//since this page's name is not the correct page, the page will go back again.
if (NavigationService.CanGoBack)
NavigationService.GoBack();
}
else
{
//this is the page we're trying to get to
App.IsBackwardNavigation = false;
App.NavigationContext = string.Empty;
}
}
}
}

Categories