Xamarin forms navigate to modal page from navigation page - xamarin.forms

I'm having trouble with navigating to a modal page.
My MainPage in App() is set to a NavigationPage like so:
MainPage = new NavigationPage(new StartPage());
I'm trying to push a new page onto the stack from the StartPage like this:
public async void DisplayAdd()
{
await Navigation.PushModalAsync(new AddGameModalPage());
}
But I'm getting an error saying that it's not compatible with the Xamarin.Forms.Page.
I've also tried something like this, but the error still remains.
var page = new NavigationPage(new AddGameModalPage());
Navigation.PushModalAsync(page);

Related

removing root page from navigation stack?

my pages are
Page 2 (root page and using it as a splash screen progress bar) -> Page1
now what i want is when Page 2 finishes doing some logic it goes to page 1 and make Page1 my root page so that when user click back button it exits the application
i dont want when user press back button to navigate to Page 2 (root page and using it as a splash screen progress bar)
how can i implent that and here is my Code
App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Page2());
}
Page2.cs
public partial class Page2 : ContentPage
{
public Page2()
{
InitializeComponent();
.....some logic code ....
Navigation.PushAsync(new NavigationPage(new Page1()));
}
and in my page1.cs
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
App.Current.MainPage = new NavigationPage(new Page1());
Navigation.RemovePage(new Page2());
}
it appears black Screen , IDK how to solve this issue , thx :)
Page 2 finishes doing some logic it goes to page 1 and make Page1 my
root page
in Page2, whenever your code "finishes"
App.Current.MainPage = new NavigationPage(new Page1());

Using Absolute Navigation is not resetting the stack and is forcing modal navigation

I have an application with a Loginscreen, and a Mainpage
When I initialize my application I set the Navigation to "NavigationPage/LoginPage". When I log into the application I reset the stack with absolute navigation using the route "/NavigationPage/MainPage". All of my pages have a label bound to the current navigation Uri and when I reset the stack after logging in and navigate to the mainpage my Uri looks like this
/NavigationPage/LoginPage/MainPage?useModalNavigation=true
I've read that if you have duplicate pages on top of one another this can cause the navigation to become modal by default. But I'm resetting the stack and even resetting the NavigationPage. This doesn't cause any immediate issues, but I have found that in deeper parts of my application's navigation stack, some navigation isn't working and this seems to be the cause
Does anyone know why this is happening?
My code
App.xaml.cs
protected override async void OnInitialized()
{
InitializeComponent();
await NavigationService.NavigateAsync("NavigationPage/LoginPage");
}
LoginPageViewModel.cs
public ICommand LoginCommand { get; }
public LoginPageViewModel(INavigationService navigationService) : base(navigationService)
{
LoginCommand = new Command(Login);
NavUri = NavigationService.GetNavigationUriPath();
}
private async void Login()
{
await NavigationService.NavigateAsync("/NavigationPage/MainPage");
}
MainPageViewModel.cs
public MainPageViewModel(INavigationService navigationService) : base(navigationService)
{
Title = "Main Page";
NavUri = NavigationService.GetNavigationUriPath(); // current URI on the mainpage is /NavigationPage/LoginPage/MainPage?useModalNavigation=true
NavigateToPageACommand = new Command(NavigateToPageA);
}
I'm using;
Xamarin Forms 4.6.0.726
Prism.Dryloc 7.2.0.1422
Appears to be just a temporary value that occurs during OnNavigatingFrom. While the stack is being reset the Navigation appears to be Modal, however it is actually not. Discovered this by adding INavigationAware to my ViewModelBase and viewing the result of GetNavigationUriPath in the OnNavigatedTo and OnNavigatingFrom Methods.

Get toolbar in Xamarin.Forms if the first page is not a navigation page

I tried to intercept the software back button in Android. I follow several samples that adds these:`
protected override void OnPostCreate(Bundle savedInstanceState)
{
var toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolBar);
base.OnPostCreate(savedInstanceState);
}
protected override void OnResume()
{
Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
SetSupportActionBar(toolbar);
base.OnResume();
}
Then use the OnOptionItemSelected to get the software back button event.
But this didn't work because, for me the first page of the app is login page that does not belong to NavigationPage. After the successful login, I change the Application.Current.MainPage to the root application page that is a NavigationPage. But it is too late, the OnPostCreate or OnResume already been called and the FindViewById(Resource.Id.toolbar) return null.
Any suggestion?
I dont understand whether you are asking xamarin.forms or xamarin.android.
For xamarin.forms
You can navigate from Login Page to Mainpage like this
Navigation.InsertPageBefore(new MainPage(), this);
Navigation.PopAsync();
And In your App.xaml.cs
MainPage = new NavigationPage(new Login());

Xamarin forms navigation backpress prevent on logout

I am creating a xamarin forms app.My app flow is LOGIN-->MAINPAGE. The Mainpage is a Bottombarpage contains three options. 1.DashBoard, 2.Settings, 3.User .
I added a logout icon on Dashboard class toolbar. My problem is whenever user click on logout, screen will navigate to login page. But if user press backbutton it will go to previous mainpage. I want to disable that.
Iam navigating from loginpage to Main page is like this.
Navigation.InsertPageBefore(new MainPage(), this);
await Navigation.PopAsync();
My App.xaml.cs - I using a validation for navigation to main page
if (Settings.Generalsettings != string.Empty)
{
MainPage = new NavigationPage(new MainPage());
}
else {
MainPage = new NavigationPage(new Login());
}
My logout button click on Dashboard.cs
private void logout_Clicked(object sender,EventArgs e)
{
Settings.ClearAllData();
Navigation.PushAsync(new Login());
}
There are two ways one if you are using a view model then you can simply navigate like this
await _navigationService.NavigateAsync("app:///NavigationPage/Splash");
Second is if you are using forms only then you can set the main page with a login page
MainPage = new LoginPage();
This will clear your back stack and navigate to the particular page.
Here you pushing a login page again, because the login page is already in navigation stack. So when you hit back button it show the login page.You can do like this MainPage = new NavigationPage(new MainPage());
From App.Xaml.cs page call
public App()
{
InitializeComponent(); MainPage = new LoginPage();
}
// and when login button pressed from login page do like this
private void loginButton_Clicked(object sender,EventArgs e)
{
NavigationPage navigationRootPage = new NavigationPage(new MainPage());
navigationRootPage.BarTextColor = Color.White;
MainPage = navigationRootPage;
}
// when you hit logout button
private void logout_Clicked(object sender,EventArgs e)
{
Settings.ClearAllData();
MainPage = new LoginPage();
}

FreshMvvm - Closing the child page of a page in FreshTabbedNavigationContainer

I have set up a FreshTabbedNavigationContainer with 5 pages.
Within the 5th tabbed page which is a SettingsPage where there is a way to display and AboutPage that is displayed via the SettingsPageModel like this
public Command AboutCommand
{
get
{
return new Command(() =>
{
CoreMethods.PushPageModel<AboutPageModel>();
});
}
}
When the user navigates away by selecting another tab, this other tab is displayed correctly.
If the user selects the SettingsPage via the tab, then the child AboutPage is automatically displayed.
I want to remove AboutPage from the navigation stack when another tab page is selected.
I have tried this in the AboutPage.xaml.cs
protected override void OnDisappearing()
{
base.OnDisappearing();
((AboutPageModel)BindingContext).CoreMethods.RemoveFromNavigation();
}
This works BUT if the back button is pressed on the app when in the AboutPage then it has already been removed from the navigation stack, and the app crashes.
How can I check if a PageModel is still in the navigation stack?
I have managed to resolve the problem :)
In the App.xaml.cs file in the App constructor where I create the FreshTabbedNavigationContainer, after setting the MainPage
MainPage = tabbedNavigation;
I then add the following code
tabbedNavigation.CurrentPageChanged += (sender, e) => {
tabbedNavigation.PopToRoot();
};
It works perfectly.

Resources