Replace Xamarin forms page without pushasync - xamarin

I've searched through numerous online posts and can't find a suitable answer for this scenario. It's a pretty common requirement. I have a Xamarin.Forms app with a login page and listing page. When the app is started, the login page is displayed. When they login successfully, I want to display the listing page and prevent them from backing up by clicking on the toolbar back button or the hardware back button.

Change the MainPage
when you start the app you should have
Application.Current.MainPage = new LoginPage();
after login, something like
Application.Current.MainPage = new MyFirstPageAfterLogin();

Related

Xamarin Forms - Navigation.PopAsync() not working

I have a tab page i've set to the main page in xamarin forms:
App.Current.MainPage = new MainPage();
MainPage is a tabbed page:
public partial class MainPage : TabbedPage
{....
Within the tabbedPage there is a content page, which has a button click to load a MasterDetail View:
await Navigation.PushAsync(new AttendMasterPage(new AttendanceViewModel(item)));
This works ok, but the masterdetail view is loaded within the tabbed page. so when i use Navigation.PopAsync(); on the Detail page once finished, nothing happens.
I need the page to go back to the original content page on the tabbed control using a button idealy.
Hope this makes sense, any more info needed please let me know
A MasterDetailPage is designed to be a root page, and using it as a
child page in other page types could result in unexpected and
inconsistent behavior. In addition, it's recommended that the master
page of a MasterDetailPage should always be a ContentPage instance,
and that the detail page should only be populated with TabbedPage,
NavigationPage, and ContentPage instances. This will help to ensure a
consistent user experience across all platforms.
Source: official doc
Please get familiar with the official documentation in order to prevent such problems.

Xamarin Page Navigation (again)

We're starting on a Xamarin.Forms app, and there are going to be quite a few pages and the navigation between pages will be handled completely by the app - specifically there is no back button, which shouldn't be a problem since we are only planning on releasing for iOS.
The first page the user encounters is the Login page, once logged in they go to the Home page. To perform this transition I just call
LoginPage.Navigation.PushModalAsync(HomePage)
and that's fine.
Now if, on the Home page, they press the Logout button, I could call PopModalAsync(), the problem is that the Logout button exists on all the pages, so the user could have followed a path like this:
Login -> Home -> Create -> Format -> Print -> Logout
and I need to immediately jump to the Login screen.
So on the Home page, if the user presses the Logout button, I tried calling
ApplicationHomePage.Navigation.PushModalAsync(LoginPage);
but got an exception:
System.InvalidOperationException: Page must not already have a parent.
So just for fun I thought I'd try the easy solution:
LoginPage.Parent = null;
ApplicationHomePage.Navigation.PushModalAsync(LoginPage);
I'm never going to have a back button, and the iPad doesn't have one, so the contents of the navigation stacks aren't really important (right?)
Is this method of navigating "legal"? Is it going to cause me some problem I'm not seeing right now?
I think you can take a look to this
You should don't add your login page to a NavigationStack. Change MainPage property is a good solution...

Absolute URI won't work

Visual Studio 2017, Latest Xamarin.Forms, Latest Prism.
No matter what I try, I can't get my absolute URI to work. I have my app launch from app.xaml to a login page. In the loginpageviewmodel, I do some checks to see if the user is authenticated and if so, then I want to navigate to the mainpage.
I can navigate to the mainpage when not using absolute uri and it works - but the back button goes back to the login page - not wanted.
Code:
if (user.isAuthenticated)
{
navigationService.NavigateAsync("/AppMasterDetailPage/NavigationPage/MainPage"));
}
I've added all the http:// and the myapp.com, etc. ... nothing I try works.
How do I change the uri so absolute will navigate to proper main page and disallow the back button to the login page?
You can check out the following sample project which uses a "Login Screen" and a Hamburger Menu.
I haven't tested an attempted Navigation from OnNavigatingTo, but I wouldn't expect that to work properly as the page hasn't actually been pushed to the stack yet. That said if you did your check to see if the user is authenticated in OnNavigatedTo you could perform the navigation without a problem. If you do not want your user to see the LoginPage then the best I could tell you would be to have a SplashScreen that you first navigate to, and then do your logic from there to decide whether to go to your LoginPage or MasterDetailPage
I have a solution ...
I have app.xaml navigate to the loginpage. In the OnNavigatedTo method, I check for user authentication. If authenticated, then I call
NavigatgeAsync("/masterpage/navpage/mainpage");
and this gets me to the proper mainpage of the app and the back button DOES NOT go back to the login page - it exits app like expected. So the "/masterpage..." is the absolute uri I was looking for.
Next time you should do this instead _navigationService.NavigateAsync( new Uri("http://www.xxxx.com/Index/Navigation/Home", UriKind.Absolute)); this is after you have authenticated and have injected the NavigationService.

Xamarin Forms navigating to a MasterDetail Page after a Content Page

I'm creating an app using Xamarin Forms where when a user launches an app for the first time, they are sent to a login page. Once they log in, they're redirected to the MasterPage, which is a MasterDetail Page. However, when I try to navigate to the new MasterDetail Page, I only get the Detail Page, and the button to show the Master Portion is missing. The only thing appearing on the app bar is the title I set. I'm currently going to the MasterPage like this:
App.Current.MainPage = new NavigationPage(new MasterPage())
{
Title = "Welcome " + user.firstName
};
Every other run, this isn't an issue, since when a user opens the app it automatically logs them in and goes right to the MasterPage, except during their first use. However, this is a very important issue for me since it happens the first time a user uses my app and it will be part of their first impression.
When you use a MasterDetailpage, you should not wrap it into a Navigationpage (like your code-example). Because the MasterDetailPage should everytime be on top and not inside a NavigationPage.
To navigate between different pages you should wrap your DetailPage from the MasterDetailPage inside a NavigationPage.
Here some code for clarification:
var view = new MyFirstPage();
Application.Current.MainPage = new MasterPage(view);
And inside the MasterPage constructor, do the following:
public MasterPage(Page detailpage)
{
// Set the master-part
Master = new MasterContentPage();
// Set detail-part (with a navigation page)
Detail = new NavigationPage(detailpage);
}
And after that, you can navigate inside your app with this navigation:
var mdp = Application.Current.MainPage as MasterDetailPage;
await mdp.Detail.Navigation.PushAsync(myNextPage);
I usually create a static helper class AppNavigator to do this stuff.
For further information, look at the MasterDetailPage instructions on the Xamarin developer guides.

How can I set up login using Fancybox?

I am trying to open a control panel in FANCYBOX.
I have a form in my page which its action and field names are same as another website and once user submit it they will redirect to their control panel in that website in a new window.
What I want is to open it in fancybox. I do not want to user leave my page, I just want them to access to that panel inside my website.
And also I want to all links of the opened frame target be _parent. I mean if they go to different parts of their panel it shows in the same fancybox not open a new window.
You might wanna look at iframe i.e. Add class='iframe' to a link check it on this page.

Resources