removing root page from navigation stack? - xamarin.forms

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());

Related

How can I detect focus on child view inside a Tabbed Page in Xamarin.Forms?

I have a TabbedPage, which includes 3 pages (A,B,C). I need to detect (using some override method) when a page is active/selected from tabbed control. I tried some override methods such us OnAppearing with no result, because during debugging process I noticed OnAppearing was fired when TabbedPage was firstly initialized, so when I selected for example the second page (page B) this method was not fired. Is there any optimal way to detect switching between tabs inside child view?
Just like
ToolmakerSteve sayed, the tabbedpage has the CurrentPageChanged event, so you can override the OnCurrentPageChanged() method of the TabbedPage. Such as:
public partial class TabbedPage1 : TabbedPage
{
public TabbedPage1()
{
InitializeComponent();
}
protected override void OnCurrentPageChanged()
{
if(CurrentPage == Children[0])//Children[0] is the page A
{
....
}else if(CurrentPage == Children[1])//Children[1] is the page B
{
.....
}
else
{
......
}
base.OnCurrentPageChanged();
}
}

On XF Prims how do I pass the Master Page Viewmodel to the MasterDetailPage's ViewModel so that I can swap out the details page

My Master page is defined in a separate ContentPage with it's own ViewModel which handles commands for navigating and swapping out the detail page, but it's not working; navigating to a Detail Page from the Master Page VM causes the Master page to disappear. As I understanding you need to navigate from the MasterDetailPage VM. Is there any way to pass the Master Page's VM to the MasterDetailPage VM, or should I be doing something else?
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
x:Class="NGT.Views.MyMasterDetailPage"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:pages="clr-namespace:NGT.Views"
MasterBehavior="Split">
<MasterDetailPage.Master>
<pages:MyMenuPage/>
</MasterDetailPage.Master>
</MasterDetailPage>
and in the Master's Page ViewModel (not the MasterDetailPage ViewModel):
public class MyMenuPageViewModel : ChildBaseViewModel, INavigationAware
{
public MyMenuPageViewModel (INavigationService navigationService)
{
this.navigationService = navigationService;
this.ChangeDetailPage = new DelegateCommand(this.OpenDetailPage)
}
public async void OpenDetailPage()
{
await this.NavigationService.NavigateAsync("FAQPage", animated: false);
}
}
For now I have created an event for every time I want to swap out my detail page and invoke the event from MyMenuPageVM like so OnPageChanges?.Invoke("Page1"); I then subscribe to the event on my MasterDetailPage VM to handle swapping out the detail page.
On MyMenuPageVM (Master Page):
public delegate void MyEventHandler(string page);
public static event MyEventHandler OnPageChanges;
Subscribe to event on MasterDetailPageVM and handle.
public void OnNavigatedTo(INavigationParameters parameters)
{
MyMenuPageViewModel.OnPageChanges += this.ChangePage;
}
public async void ChangePage(string page)
{
switch (page)
{
case "Page1":
await this.navigationService.NavigateAsync("Page1");
break;
case "Page2":
await this.navigationService.NavigateAsync("Page2");
break;
}
}
Not sure this is the right thing, but it works. :)

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();
}

Xamarin forms navigate to modal page from navigation page

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);

From App.cs I am going to my landing page(MainPage.xaml) and then want to navigate to other page?

I am navigation to MainPage.xaml from my App.cs following is my code for App.cs
public class App : BaseApplication
{
public App()
{
MainPage = new NavigationPage(new MainPage());
}
}
And in my MainPage.xaml and code behind I am doing following
public partial class MainPage:ContentPage
{
public MainPage()
{
InitializeComponent();
SetPage();
}
async public void SetPage()
{
//await Navigation.PushModalAsync(new Login());
await Navigation.PushAsync(new Login());
Navigation.RemovePage(this);
}
}
This code gives me the login page but doesn't remove MainPage from stack and keeps my Android back button enable and clicking on that icon navigates me back. I really want to set Login page as a starting page in some situation. I have also tried App.Current.MainPage = new NavigationPage(new Login()); but that didn't work also?
Any suggestion how do I set the Starting page dynamically from my mainpage.cs, note I don't want to do that in App.cs as I want to keep that as clean as possible?
For more details, on error, please click on this link
I currently have a solution with the same approach you want to do.
What I do there is something like this:
public async void LoginClick(object sender, EventArgs e){
try
{
//Some logic code here ...
App.Current.MainPage = new NavigationPage(new HomeView());
}
catch (Exception exc)
{
DisplayAlert("Error",exc.Message,"OK");
}
}
}

Resources