I have a question on Xamarin.Forms, I'm trying to navigate back page from detail page when UIAlertView pressed OK. But I couldn't do it. Here is my code;
{
UIAlertView alert = new UIAlertView();
alert.Title = "Warning";
alert.AddButton("OK");
alert.Message = "Payment not found!";
alert.Clicked += ButtonClicked;
alert.Show();
return paymentList;
}
private void ButtonClicked(object sender, UIButtonEventArgs e)
{
//???
}
Thanks.
First of all, Xamarin.Forms doesn't have UIAlertView class (Inside the PCL project).
You can display alert there by calling asynchronous DisplayAlert method inside the Page instance. For example:
public class PaymentPage
{
...
async void OnNoPaymentMethodDetected(object sender, EventArgs e)
{
await DisplayAlert("Error", "You have no payment method registered", "Okay");
await Navigation.PopAsync(true);
}
...
}
Note: your Pop Navigation method depends on your Push Navigation method. For instance, if you've used PushModalAsync(page2), then you have to use PopModalAsync() in page2 to move back, otherwise PopAsync() will cause an error.
If you want to have similar alert in iOS-specific project, you have to use the next code:
var controller = UIAlertController.Create("Error", "No payment method detected", UIAlertControllerStyle.Alert);
controller.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, (sender) =>
{
NavigationController.PopViewController(true);
}));
ShowDetailViewController(controller, null);
UIAleartView does not have specific events for Buttons.
But, you can access the index of the button pressed from the UIButtonEventArgs (ex: e.ButtonIndex) verify what button is pressed
Related
I had build a Multipager Xamarin App, but after second page Android back button and Navigation back button is not working.
I had tried Shell Backbuttonbehavior , but with this only Top Navigation back button is working
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding BackCommand}" IconOverride="back.png" />
</Shell.BackButtonBehavior>
I had tried overriding OnAppearing and OnDisappearing to set Current and pervious navigation but that too is not triggering on thrid page
protected override void OnAppearing()
{
base.OnAppearing();
Shell.Current.Navigating += Current_Navigating;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Shell.Current.Navigating -= Current_Navigating;
}
private async void Current_Navigating(object sender, ShellNavigatingEventArgs e)
{
if (Models.PatientPlannerData.moveNext == false)
{
Shell.Current.Navigating -= Current_Navigating;
await Shell.Current.GoToAsync("..", true);
}
}
For Navigation from one page to another I am using
await Shell.Current.GoToAsync(nameof(Page2));
Everything is working fine till second page, from third page back button is not triggering
For the Android back button, after compilation the Navigation Bar that we call in Xamarin Forms, turns into the Action Bar for Android during runtime.
Set the toolbar after LoadApplication(new App()); in OnCreate of MainActivity.
AndroidX.AppCompat.Widget.Toolbar toolbar
= this.FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
And then overrode OnOptionsItemSelected(). When you press the backbutton on navigation bar, this event would be triggered.
public override bool OnOptionsItemSelected(IMenuItem item)
{
}
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
I am working on a xamarin project for ios aswel as android where we are showing custom markers on the Xamarin.Forms maps, we want the user to navigate to another view when he clicks on a custom marker.
We are using Navigation.PushAsync to navigate through the views. this is done from the viewmodels from the non platform specific code, and navigation.PushAsync can only be used there and not from the platformspecific code. Wich is where the customMapRenderers are and where the onlclick for the markers is handled.
So my question is how can i navigate to another view from these onClick Events.
underneath are the methods that catch the onclick.
Android:
private void OnMarkerClick(object sender, GoogleMap.MarkerClickEventArgs e)
{
Toast.MakeText(MainApplication.Context, "Button Pressed", ToastLength.Long).Show();
}
iOS:
private void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
UIAlertView alert = new UIAlertView()
{
Title = "Event",
Message = "Button Clicked"
};
alert.AddButton("Oke");
alert.Show();
}
As Yuri said, you can implement this by using Messenger, here is a sample about how to use Messenger. You can use it in custom renderer onClick Events, When send a message in renderer, the Page will received Message. So you can navigate to another view in your Page's MessagingCenter.Subscribe method.
Page:
MessagingCenter.Subscribe<MainPage>(this, "Navigation", async (sender) =>
{
var page1 = new Page1();
await Navigation.PushModalAsync(page1);
});
Custom Renderer:
MessagingCenter.Send<MainPage>(MainPage.getInstance(), "Navigation");
just wondering basically i have an Azure authentication system that opens when clicking on a facebook button or twitter button it then asks to authenticate the app and once logged in displays a UIalertview with the options to click "OK" or "Cancel".
I was wondering how once they clicked ok i could get it to display the next View?
I know my uialertview is called alert - so was thinking it would alert.Clicked (); then something in there but not sure what.
Here is the method that is processing the login and the alertview if someone can get back to me fast.
private void DoLogin(MobileServiceAuthenticationProvider provider)
{
var task = this.client.LoginAsync(this, provider).ContinueWith(t => {
MobileServiceUser user = t.Result;
this.BeginInvokeOnMainThread(() => {
UIAlertView alert = new UIAlertView("Logged In!", string.Format ("Hello user {0}", user.UserId),
null, "OK", new string[] {"Cancel"});
alert.Clicked();
alert.Show ();
});
});
}
Thanks
For example you could do this:
alert.Clicked += (sender, args) =>
{
// check if the user NOT pressed the cancel button
if (args.ButtonIndex != alert.CancelButtonIndex)
{
// present your next UIViewController, something like this
NavigationController.PushViewController(new YourNextViewController(), true);
}
};
For more information about UIAlertView check out it's documentation:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html
I am working on an app on wp7.
I hope to prompt a confirmation dialog when user exit app (press back button).
Is it possible?
Welcome any comment
Please handle the BackKeyPress button in the Application page to handle the back key press.
In Page.xaml file in the element add this code
BackKeyPress="PhoneApplicationPage_BackKeyPress"
it should look like
<phone:PhoneApplicationPage BackKeyPress="PhoneApplicationPage_BackKeyPress"
..//other attributes .. >
in event handler you write the code as follows
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mb = MessageBox.Show("You want exit the page", "Alert", MessageBoxButton.OKCancel);
if( mb != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
It's possible to catch when the user exits pressing the Back button, but it is not possible to stop the application from being made "dormant" when the user presses the hardware Start button or Search buttons.
You can stop back navigation by set e.Cancel in back key press event.
In MainPage.xaml.cs constructor:
OnBackKeyPress += (s, e) =>
{
if (MessageBox.Show("", "", MessageBoxButtons.OkCancel) == MessageBoxButtons.Cancel)
{
e.Cancel = true;
};
};