How to close a content page on button click and remove that from stack in xamarin forms? - xamarin

want to close a content page on button click and remove it from stack,so how to achieve this.PopAsync is not working for me.

To remove some page of you navigation stack you can use.
this.Navigation.RemovePage (this.Navigation.NavigationStack [your page index]);
but if you just want to go back screen or go to main page of your navigation stack you can use this.
this.Navigation.PopAsync ();
this.Navigation.PopToRootAsync();

you need to call
Application.Current.MainPage.Navigation.PopAsync();
from async void.
for example if you have button click handler just add async keyword before void like this:
private async void Button_Clicked(object sender, EventArgs e)
{
await Application.Current.MainPage.Navigation.PopAsync();
}

Related

Navigate within modal dialog using shell

I'm using shell to navigate in my Xamarin Forms app. I want my settings to be in a Modal dialog which is navigated to by calling PushModalAsync. In that settings page I want to navigate to other pages, eg password change. But since I'm not using NavigationPage I cannot get it to work.
How do one use Shell with Modal and navigate within that Modal page?
I can get it to work by setting my SettingsPage as a new MainPage. But then I don't get the modal navigation animation.
using NavigationPage will probably solve the problem but then I will
no longer use Shell which I want to use.
I think wrap your Settings page in a NavigationPage would solve your problem. You can still use Shell after using NavigationPage.
For example, you can do some push actions in the SettingPage:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new BearDetailPage());
}
In the BearDetailPage, if you want to use Shell, you can dismiss the SettingPage by send a message usingMessagingCenter:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopToRootAsync(false);
MessagingCenter.Send<BearDetailPage>(this, "BackToShell");
}
And in the SettingPage, subscribe that message:
public SettingPage()
{
InitializeComponent();
MessagingCenter.Subscribe<BearDetailPage>(this, "BackToShell", (sender) =>
{
// Do something whenever the "BackToShell" message is received
this.Navigation.PopModalAsync();
});
}
If you don't want the navigationbar in the SettingPage, you can just hide it.

Button x:Name="btnBack" not navigating to previous page

I am developing Xamarin Forms application for IOS and Android.I have the list of 3 xamarin form pages say X.xaml, Y.xaml, Z.xaml. Page X.xaml, Y.xaml is having 1 and click on that button it opens the next-next Xamarin form. And last on Z.xaml having 2 buttons btnBack & btnConfirm
<Button x:Name="btnBack"></Button>
<Button x:Name="btnConfirm"></Button>
when I click on that back button I want to navigate it to my previous page, but I don't want it like Navigation Back Button or Hardware Back Button click.
for this I have tried this code this will not work.
Z.xaml.cs
public partial class Z : ContentPage
{
public Z()
{
InitializeComponent();
btnBack.Clicked += btnBack_Clicked;
}
private void btnBack_Clicked(object sender, EventArgs e)
{
var existingPages = Navigation.NavigationStack.ToList();
foreach (var page in existingPages)
{
if(page == this)
Navigation.RemovePage(page);
}
}
}
In this code when I click on Back Button it Navigate me to prevoius page ie Y.xaml once I click on button which is on Y.xaml and open the Z.xaml page it shows me blank.
Y.xaml.cs
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
#Kowalski had the right answer however it is incomplete.
You have to await Navigation.PopAsync() otherwise you may mess up the navigation stack. So always await it. Example:
async void btnBack_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
Beside that be aware that there are bugs in Xamarin.Forms related to navigation, here is one that might be interesting for you as well:
https://bugzilla.xamarin.com/show_bug.cgi?id=59172
P.S.: Here you can find the official guide on Popping Pages from the Navigation Stack.
you should invoke Navigation.PopAsync() to go back

Xaml Button is "clicked" if it's pressed-and-hold on a touch screen

I am developing a Windows UWP App with C++. I want to add a button that can either be left-clicked or right-clicked and do corresponding task.
The button works perfectly on normal devices with mouse. Unfortunately on touch screen, if users press-and-hold the button, both left-click and right-click events are triggered.
I am using "Click" to handle the left click event and "RightTapped" to handle the right click event. Any suggestion on how to disable the left-click event when users press-and-hold the button? Thank you!
The XAML code is short:
<Button x:Name="m_NewPageButton"
Content="New Page"
Height="48"
Width="199"
Click="OnClick"
RightTapped="OnRightClick"/>
And here's the cpp code:
void MainPage::OnClick()
{
// Left click task
}
void MainPage::OnRightClick()
{
// Right click task
}
Edit:
Thanks guys, the solution is to change the left click handler to , which will not be triggered by press-and-hold.
Hacky but works...
private bool rightClicked;
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (rightClicked)
return;
await new MessageDialog("left").ShowAsync();
}
private async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
rightClicked = true;
await new MessageDialog("right").ShowAsync();
rightClicked = false;
}
I'm guessing this is a flaw in the design.. :(
Actually... If you use the button's Tapped and RightTapped events it works as it should. So in this case just replace Clicked with Tapped and all is well... Then you can ignore the hack I put above.
You can use the Holding event to detect a tap and hold. Use the Tapped event for a single tap (or click). The Tapped event should only fire for touch on a quick tap and release. The mouse specific click will raise Tapped but a touch tap-and-hold will not.

How to go to an xaml page by clicking on the button in WebBrowser control in Windows Phone 7 or 8

I have a XAML Page with a WebBrowser tag. WebBrowser has few text boxes and a button, here my question is after clicking on that button i have to go to a XAML page. How can i do this?
By clicking the button in WebBrowser, i want to go to a XAML Page which is in solution file.
Please help me on this.
I believe you can solve this by listening to the WebBrowser.Navigating event. First do:
myWebBrowser.Navigating += myWebBrowser_Navigating;
Then in the handler method check if the uri navigated to is the pointed to by that button:
private void myWebBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if(e.Uri.AbsolutePath == URI_OF_BUTTON) {
NavigationService.Navigate(new Uri("/otherPage.xaml", UriKind.Relative));
}
}
It sound like Page Navigation.
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Pasta.xaml", UriKind.Relative));
}

Windows Phone back button to terminate the app

I have a main page with some options. One of them is to navigate to page 1 where there are two arrows. One that navigates to page2, page3, and the other arrow page3, page2, page1 like a loop. There is also an arrow that navigates to main page.
I want to ask if there is a way when the user presses the back button to terminate the app from whatever page the user is currently at and not to navigate through all pages.
edit
if i want when i press the back to always navigate to the mainpage what i have to do ?
clear the back stack inside the onbackkeypress function where you want to exit the app. And it will exit the app normally.
[Updated]
1) after clearing back stack. Use NavigationService.Navigate(new Uri("MainPage.xaml",UriKind.Relative)); to traverse to mainpage and do e.Cancel = true in the next statement.
2) Or clear the back stack upto the mainpage. and automatically the back press will take you to the mainpage. inside the mainpage clear the back stack fully inside the OnNavigatedTo function so that the first item is always your mainpage and user can exit easily.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
while (NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
e.Cancel = true;
return;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
while (NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
}
Throw an exception in OnBackKeyPress which terminates the app.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
throw an exception();
}
You can also do like this,
check
e.NavigationMode == System.Windows.Navigation.NavigationMode.Back
in OnNavigatedTo event (you need to override this every page) and call
NavigationService.GoBack();
there is no direct way of exiting an app.. see here
hope this will work. If its XNA
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

Resources