how to make master detail page as root page? - xamarin.forms

i have master detail page,on click in page A(master detail page) navigate to -> page B then to ->Page C .from page c ,i have to go to page A,i used PopAsyncRootpage() for achieving this .
my issue is when the user is using application for the first time my rootpage will be login page.so,PopAsyncRootpage() will make nagivation to login page not to Page A from Page C.
but,once logged and the user closed application without logout,and he again open the application then the rootpage will be pageA,then PopAsyncRootpage() will navigate to pageA from Page C.
the problem canbe solved if i can set PageA as rootpage once logged..how will do this?
or please help .. how can i navigate to page A from page C..(pageA is master detail page),
also once logged i have popout login page from stack and push masterdetailpage(pageA)
await Navigation.PopToRootAsync();

You can just set the MainPage to your MasterDetail.
Example, in App.xaml.cs:
MainPage = new YourMasterDetail();
Or from another Page
Application.Current.MainPage = new YourMasterDetail();

Related

Simple page container

Is it possible to have a simple ContentPage or something similar that hosts another page without any of the frills that come with tab page or navigation page? I want this host page to be essentially invisible.
The idea is to pop up a login modal on the root page and then, when the user has logged in, switch the contents in the background so that when the login page is popped off, it reveals new content.
If you want the NavigationStack available so you can push & pop pages, you need to use NavigationPage. You have a couple of reasonable options though:
Reassign Application.Current.MainPage whenever you want a page transition. This won't create a page stack, but will show the new content when reassigned without animation. So rather than thinking about a login modal then popping it off, just set the current page to the login page, then on login, set the current page to the content you want the user to see.
Use a NavigationPage as the Application.Current.MainPage, but then call SetHasNavigationBar(page, false) to avoid the extra elements that come with NavigationPage. You may need to call NavigationPage.SetHasBackButton(page, false); for every page you push on the stack to avoid the back button.

How to insert a modal Login form in the navigation bar

I'm building a new website and one of the requirements is to have the login form be a modal window.
I'm trying to include it in the top navigation bar and it's only being rendered if the user is not logged in.
If I delete the model and let an empty modal everything works perfectly but when I add it again it doesn't work, because the model of the page (in this case the index page) is a different one then the one from the modal login.
How can I add this modal window with it's on model inside the top navbar? Are there any alternatives?
P.S. I'm using Razor Pages and ASP.NET Core 2.2.
Thanks!
You need to use a view component:
public class LoginViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
var model = await GetModelAsync();
return View(model);
}
public Task<LoginViewModel> GetModelAsync() =>
Task.FromResult(new LoginViewModel());
}
Then, create the view Views\Shared\Components\Login\Default.cshtml and add your login form code there. Finally, in your layout where you want the login form to be:
#await Component.InvokeAsync("Login")
You'll need a page or handler that can respond to the form post. You cannot post to a view component; that's just about rendering a partial view with it's own model in a self-contained way that can be dropped in. You should have a hidden input in your login form for a "return URL" which you'll fill with Request.Path. Then, on successful login, you redirect the user back to this URL. That way, the login will be seamless, as if they never let the page they were on.
On failure, you should simply redisplay the login form like you would do normally. It's easier and more seamless to just return this as the view. As such, you can piggy-back on the the login page's built-in post handler. Again, just add your return URL field and fill it with the return URL from the request, so that the user will eventually make it back to the page they were initially on when starting the login process.

Xamarin Form : Error on Login page when load

I have a login page where I have a check if user is logged in, then navigate to other page or stay on the login page. I don't want to put this code on App.cs because login page will not open on 1st load of app. Now when I navigate to login page my app crash and throws the error:
GC_BRIDGE: Complete, was running for 0.09ms 11-16 12:31:55.111 D/Mono
(15701): GC_MINOR: (Nursery full) time 35.82ms, stw 36.83ms promoted
1009K major size: 531904K in use: 471305K los size: 2048K in use: 49K
Here is my code
public partial class Logins : ContentPage
{
public Logins()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
if (!string.IsNullOrEmpty(Setings.AccessToken))
{
//
Navigation.PushAsync(new CartDetail());
}
else
{
Navigation.PushAsync(new Logins());
}
}
You have a recursive navigation inside your constructor.
When the property Setings.AccessToken is empty you navigate to a new Logins page. Because you create a new Logins page, you also check again for the AccessToken. And it is still empty, so you create again a new Logins page and again and again and again...
Quote from your question
[...] or stay on the login page.
To stay on the page, just don't navigate to it in the constructor. Leave the else part empty will show the Logins page.
Hint: It is not recommended to navigate inside a constructor, because of two reasons:
The navigation method PushAsync is asynchronous. So you can wait on it until the navigation finishes (that is not possible in the constructor)
You should check for navigation outside the page itself (in fact: before you create the Logins page first). Or: You have to load the page, and check the AccessToken afterwards. Why? You create a page to show it, and not to check if another one needs to be shown...

How to Exit (or Navigate out of )a MasterDetail page to a simple ContentPage in Xamarin. Forms?

I have a Xamarin.Forms app which has the following:
LoginPage - a ContentPage type
RegisterPage - a ContentPage type
parentMasterDetail - a MasterDetailPage type
masterPage - Master page (ContentPage type)
AppointmentPage - Detail page (ContentPage type)
NewAppointment - Detail page (ContentPage type)
I was able to create the pages all the pages as listed above, BUT
when I click on Logout button from the Detail page,
the LoginPage displays but to my surprise it displayed just as a detail page,
as if it is one of the detail pages and the menu page(i.e masterPage) is still
accessible from my login page. This is not what I intended.
I want to logout of (i.e close) the MasterDetailPage, named parentMasterDetail,
and navigate to the LoginPage which is not part of the MasterDetailPage such that
the masterPage (i.e the menu) will not be accessible until user logs-in.
Please, anyone help me.
You can do something like this:
Application.Current.MainPage = new NavigationPage(new LoginPage());
If you need without top navigation bar (Header of Page), try this.
Application.Current.MainPage = (new UserLoginPage());

Going back to previous page after page redirection

Hi I am tring to send a user to the login page if he is not authetificated and after he logs in I want to send him back to the page he was the problem is I am getting only half way to the page I want the user to be.Here is my code:
When I user clicks a button in my application I check if the user is logged in if he is not.I refirect him to the loggin page with this protion of code:
return RedirectToAction("LogIn" , "Account" , new { returnUrl = "Products" , subCat = Request.QueryString["subcat"]});
What I would expect to get out of this code is:
LogIn?returnUrl="Products"&subCat="Notebooks"
What I get is this:
Products?subcat=Notebooks
This is the link from the page I cliked the button from.Now this behavior may be because the links are all created using ajax.ActionLink but if that is true how can I get back to my previous page?
I have not modified the default routing of the MVC application.
Set the returnUrl like this returnUrl = ViewContext.HttpContext.Request.Url.PathAndQuery and leave the subcat alone.

Resources