I am looking to disable gestures for a specific page. I've came across many post suggesting to set MasterDetailPage's IsGestureEnabled to false to disable it. This works but for some reason not all the time.
At the moment I only want to disable gestures at the signin page, so on authentication I enable gestures, OnAppearing I disable.
The issue I'm coming across is that if the user switchs apps and comes back and signs out, the gestures are still enabled. It makes me think a different MasterDetailePage context is being triggered.
Has anyone came across this issue or has any guidance for me. I possibly think I am not understanding the lifecycle of the MasterDetailPage
If you want to disable gesture on specific page for MasterDetailed Page, I Suggest you can try to use MassagingCenter to do this.
1.Usinf MassagingCenter from masterdetailed page constructor:
MessagingCenter.Subscribe<string>(this, "DisableGesture", (sender) =>
{
if (sender == "0")
{
IsGestureEnabled = false;
}
else
{
IsGestureEnabled = true;
}
});
2.When you navigate to specific page , and want to disable gesture, you can do this in content page OnAppearing method.
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send<string>("0", "DisableGesture");
}
Related
Is there any way to detect the press of the back button of the Navigation Page in Xamarin forms?
You can override your navigation page "OnBackButtonPressed" method:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Exit?", "Are you sure you want to exit from this page?", "Yes", "No"))
{
base.OnBackButtonPressed();
await App.Navigation.PopAsync();
}
});
return true;
}
If you are using the shell, you can override the Shell's OnNavigating event:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}
Update:
OnBackButtonPressed event will get fired ONLY on Android when user press the Hardware back button.
Seems like you are more interested to implement when any page get disappeared you want to do something!
In that case:
You have the page's two methods -
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Hey, Im coming to your screen");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Console.WriteLine("Hey, Im going from your screen");
}
You can override those 2 methods on any page to track when they appear and disappear.
Recent updates to Xamarin forms mean you can now do this in an application made with Shell Navigation for navigation back arrow on both platforms.
Use the Shell.SetBackButtonBehavior method, for example running this code in the constructor of your page object will allow the back navigation to take place only when the bound viewmodel is not busy:
Shell.SetBackButtonBehavior(this, new BackButtonBehavior
{
Command = new Command(async() =>
{
if (ViewModel.IsNotBusy)
{
await Shell.Current.Navigation.PopAsync();
}
})
});
In the body of the Command you can do whatever you need to do when you are intercepting the click of the back button.
Note that this will affect only the navigation back button, not the Android hardware back button - that will need handling separately as per the answers above. You could write a shared method called from both the back button pressed override and the command on shell back button behaviour places to share the logic.
You must override native navigationbar button behavior with custom renderer. OnBackButtonPressed triggers only physical device button. You can read good article how to achive this here
I've got this code for creating a WebView:
public class SessionsActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.discover);
//declare webview and tell our code where to find the XAML resource
WebView discoverWebView = FindViewById<WebView>(Resource.Id.webViewDiscover);
//set the webview client
discoverWebView.SetWebViewClient(new WebViewClient());
//load the subscription url
discoverWebView.LoadUrl("https://www.bitchute.com/");
//enable javascript in our webview
discoverWebView.Settings.JavaScriptEnabled = true;
//zoom control on? This should perhaps be disabled for consistency?
//we will leave it on for now
discoverWebView.Settings.BuiltInZoomControls = true;
discoverWebView.Settings.SetSupportZoom(true);
//scrollbarsdisabled
// subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
discoverWebView.ScrollbarFadingEnabled = false;
}
}
but when I click on links inside my app, the page loads at the same spot as the last page was, which makes navigation very difficult, because mobile pages are supposed to load from the top, not from the middle. So basically, if I am in my subscriptions feed, and then I scroll down to find more content, I click on a user, and their video page loads videos from weeks ago, because it loaded with the same view elevation as the root page.
The site I'm using WebView with is also designed for mobile users, if that matters. The weird thing is that this used to not happen until recently... don't know what changed. How can I have the WebView load at top of page on each click?
I'm using TabHost for this .. here's my full code for context
https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.68/MainActivity.cs
thanks in advance!
I'm having a problem with the backbutton, so I have a function that is only put on action like 5 pages after the MainPage. Than on that fifth page I want to touch the backButton and go to the MainPage(so 5 pages before that one), is there anyway to throw some code to the back button to do this? How can I do this?
EDIT1
"MainPage" is not my root page!!!!
Override the OnBackButtonPressed() method in the code-behind of your page.
If you want to navigate back to the root page, you can call Navigation.PopToRootAsync():
protected override bool OnBackButtonPressed()
{
Navigation.PopToRootAsync();
return true;
}
If you want to navigate back a single page, just Navigation.PopAsync(). If you want to navigate back for example three times, you can call Navigation.PopAsync() three times.
I think Dennis' answer is correct, and it seems you know how many steps you want to go back, so:
protected override bool OnBackButtonPressed()
{
// Go back 5 steps when the back button is pushed
for (var i=0; i<5; i++) {
Navigation.PopAsync();
}
return true;
}
You can get it overriding OnBackButtonPressed() but you have to take care because this override just fire when you press physical button then this solution does not work with iOS just android, for iOS you have to create a custom renderer of the navigation page and replace the default back button for a new one and add the necessary action.
other way to get this approach could be on the OnDisappearing() override of the page
protected override void OnDisappearing()
{
if (Navigation.NavigationStack.Last() == this)
{
Navigation.PopToRootAsync();
}
base.OnDisappearing();
}
I have 2 pages in my Xamarin Forms app. My first page has 4 icons in the toolbar. My second page is a login page and has a tick and a cross in the toolbar.
I can't get the login page to show any icons unless I make it a navigation page. I also have to clear ToolBarItems on the first page before calling PushAsync() otherwise it complains there are too many toolbar items.
If I call PopAsync() on the login page it does not return to the first page. I'm guessing this is due to their being 2 navigation pages. I also tried PopToRootAsync().The back button works however.
My question is - how do I show different toolbar icons on 2 different pages in a way that allows navigation to work?
I'm testing this on Windows Phone 8.0
Here is the code calling the login page:
private async void ShowLoginPage()
{
ToolbarItems.Clear();
var page = new NavigationPage(new LoginPage());
await Navigation.PushAsync(page);
}
and here is the code to return to the first page:
private void Cancel()
{
Navigation.PopToRootAsync();
}
I'm running Xamarin.Forms v1.2.2.6243
One thing you could try is to keep your Login Page inside of a NavigationPage, and then instead of running PopAsync() within the Login Page after they have logged in successfully, simply replace the MainPage with your old Navigation page:
In your App class:
public NavigationPage AppNavPage = new NavigationPage(new FirstPage());
public App() {
MainPage = AppNavPage;
}
In your FirstPage:
private async void ShowLoginPage() {
ToolbarItems.Clear();
var page = new NavigationPage(new LoginPage());
await Navigation.PushAsync(page);
}
In Login Page:
private async void OnCreateClicked(object sender, EventArgs e) {
bool loginInfoIsGood = CheckLoginInfo(); //Check their login info
if(loginInfoIsGood) {
Application.Current.MainPage = App.AppNavPage;
}
}
Otherwise, I have also done a custom renderer for the NavigationRenderer on iOS to insert toolbar items onto the right side of the Navigation Bar and have overridden some Menu related stuff on Android to change the icon text/colors.
One option that you have, and one that I implemented in my own app, is a custom renderer that removes the navigation header from the app and then you could build your own custom header. With this approach, you do lose some of the native feel of the app, and you have to implement much of the transitional functionality your self. However, it gives you alot more control over the look.
CustomRenderer that removes the navigationBar:
//add using statements
// add all view here that need this custom header, might be able to build a
//base page that others inherit from, so that this will work on all pages.
[assembly: ExportRenderer(typeof(yourView), typeof(HeaderRenderer))]
class HeaderRenderer : PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.NavigationController.SetNavigationBarHidden(true, true);
}
}
After this you can build a header view that can be placed on the top of every page (I am using xaml) so I don't know if it is relevant in you application.
Edit: You might need to change this renderer for differnt page types.
I have an application, the main page contains few functions. To explain in detail - I have save, color palette button in my main page. When any of these buttons are clicked, save pop up or color palette appears. How to handle back button of the device, when color palette or save pop up is opened. When back button is pressed at these scenario it should just make them invisible and stay on the main page. When nothing is being performed in the main page, then it should come out of the app. I tried to make their visibility collapsed on back button press. But it still is coming out of the application.
Please, guide me in this. Thanks in advance.
Override PhoneApplicationPage.OnBackKeyPress and then set CancelEventArgs.Cancel to true if you want to stop it from actually going back.
protected override void OnBackKeyPress(CancelEventArgs args)
{
if (PanelIsShowing)
{
HidePanel();
args.Cancel = true;
}
}
The back button behaves as is intended by Microsoft.
If you change its behavior you risk your application not being certified for the Marketplace.
If you want the back button to close the popups, turn the popups into pages so the back button navigates back to the main page..
You need to use
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (_popup.IsOpen)
{
_popup.IsOpen= false;
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
That should do the trick.