I have problem to pass parameter from page. Goal is that after login wanna show logged user info in tabbed page. Can you help me how to pass parameter from page to tabbed page.
Thank you
Solution is
Login Page
await Navigation.PushAsync(new TabbedPage1("Name"));
TabbedPage
<local:Page1 Title="Page1">
<Label x:Name="lblfirstname"/>
</local:Page1>
public partial class TabbedPage1 : TabbedPage
{
public TabbedPage1(string firstname) {
InitializeComponent();
lblfirstname.Text = firstname;
}
}
There are many solutions, but it's depend on your implementation.
The simplest is use MessagingCenter . Your tabbed page can subscribe and your page can publish all necessary parameters.
Related
I would like my app to show a Login page, then a MasterDetail page with a 'hamburger' menu. On the hamburger menu I want a Log Out entry that closes the Master Detail and shows the Login page again. I have all but the log out part working.
I have copied code from the MvvmCross Playgrounds sample, specifically the MixedNavFirstPage (which fakes a login procedure), MixedNavMasterDetailPage (the 'hamburger menu'), and MixedNavMasterRootContentPage.
If I try to close the MixedNavMasterDetailPage with await _navigationService.Close(this) then I get a null reference exception in MvxFormsPagePresenter.CloseMasterDetailPage()
This question How to Exit (or Navigate out of )a MasterDetail page to a simple ContentPage in Xamarin. Forms? covers what I want to do in straight Xamarin.Forms, I'm just not sure how to do the equivalent in MvvmCross.
In your LoginPage add the MvxContentPagePresentation attribute NoHistory = true
Then simply navigate to the login page, and when the user has logged in, navigate to your MasterDetail page also with NoHistory = true.
When the user logs out again, simply navigate to your LoginPage, and as the NoHistory = true the MasterDetail will be removed completely.
[MvxContentPagePresentation(WrapInNavigationPage = false, NoHistory = true)]
public partial class LoginPage : MvxContentPage<LoginViewModel>
{
public LoginPage()
{
InitializeComponent();
}
}
I am trying to implement a login scenario with Xamarin Forms and Prism.
My application root page is a MasterDetail page (HomeMasterDetailPage); this being used to provide the side menu.
What I am trying to do is to have the HomeMasterDetailPage page shown with Content set to an instance of LoginPage. When the user logs in, the Content should change to an instance of HomeDetailContentPage. It should not be possible to return to the LoginPage using the back button.
I am using the NavigationService from Prism Forms.
My start up is like this...
public partial class App : PrismApplication {
public App(IPlatformInitializer initializer = null)
: base(initializer) {
}
protected override void OnInitialized() {
InitializeComponent();
Uri uri = new Uri($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}/{nameof(HomeDetailContentPage)}", UriKind.Absolute);
var settings = Container.Resolve<SettingsService>();
if (!settings.DeviceUserID.HasValue)
uri = new Uri($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}/{nameof(LoginPage)}", UriKind.Absolute);
NavigationService.NavigateAsync(uri);
}
protected override void RegisterTypes() {
Container.RegisterTypeForNavigation<NavigationPage>();
Container.RegisterTypeForNavigation<LoginPage, LoginPageViewModel>();
Container.RegisterTypeForNavigation<HomeMasterDetailPage, HomePageViewModel>();
Container.RegisterTypeForNavigation<HomeMasterContentPage>();
Container.RegisterTypeForNavigation<HomeDetailContentPage>();
}
}
This correctly displays the LoginPage within the HomeMasterDetailPage if the user is not already logged on, and the HomeDetailContentPage within the same HomeMasterDetailPage if the user is logged on.
The problem comes when actually logging on; the following is the code within the view model for the LoginPage...
protected async Task ExecuteLoginCommand() {
Uri uri = new Uri($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}/{nameof(HomeDetailContentPage)}", UriKind.Absolute);
await this.navigationService.NavigateAsync(uri);
}
It is my understanding that by using an absolute Uri this should reset the navigation so that the new page arrangement (/NavigationPage/HomeMasterDetailPage/HomeDetailContentPage) is at the top and bottom of the stack, allowing me to then move forward from there. Instead, I am getting an unhandled exception (on Android 7.0).
What am I doing wrong?
Note: All of the examples that I see have the MasterDetailPage at the root with Content being set to NavigationPage/ContentPage; when I try this I do not get exceptions, however the side menu operates differently within the MasterDetailPage - when you tap on the "hamburger" the side menu slides in over everything (including the action/title bar) and the only way to clear it is to tap outside of the side menu that slides in whereas when I show the MasterDetailPage within a NavigationPage the side menu slides in below the action/title bar and the hamburger changes to an arrow that you can click to hide the side menu again.
I can't say I've tested this exact scenario. However, if your LoginPage is the Detail of a MasterDetailPage, you may not want to perform the navigation from the LoginPage. You could, instead create a SuccessfulLoginEvent, which you would then publish from the LoginPage using the IEventAggregator, and you would subscribe to on your MasterDetailPage's ViewModel. You could then perform the navigation as _navigationService.NavigateAsync("NavigationPage/ViewA") and it should reset the Detail such that the back button doesn't bring you to the LoginPage.
Alternatively, you can simply perform an absolute Navigation which has the effect of:
Application.Current.MainPage = new MyPage().
To do to this, you could do the navigation from anywhere as:
_navigationService.NavigateAsync("/MyMasterDetailPage/NavigationPage/ViewA")
I am creating an app using Xamarin Forms. I have a MainTab page that displays all the 4 tabs. There is one specific tab call Me tab.
As of now the Me Tab displays the user's profile. However, when the user is not logged in, it my UserLogin page should be displayed instead. How do I do that in Xamarin forms?
You can achieve this in various ways. If you're using a MVVM approach the most straight forward solution would be to set a binding in your viewmodel indicating user logging. Based on that you can set your
tabbedPage.CurrentPage = TheTabPageYouWant.
If you're using codebehind, just use in your ;
public bool UserLogged
{
get { return _userLogged; }
set
{
_userLogged = value;
if (_userLogged)
{
tabbedPage.CurrentPage = UserProfilePage.
return;
}
tabbedPage.CurrentPage = LogInPage.
}
I strongly recommend any Xamarin.Forms book and MVVM techniques for you.
Don't hesitate to ask any further questions.
I am using MasterDetail page in xamarin forms, my detail page is a tabbed page with three tabs, my problem is when I am writing below code in MasterDetail Page to remove NavigationBar from that page
protected async override void OnAppearing ()
{
base.OnAppearing ();
NavigationPage.SetHasNavigationBar (this,false);
}
Then my tab page is not working properly like all tabs got disabled.
But without using this code everything is working fine.
But I need to write this code as I don't want that navigation bar on my master detail page.
You can hide the navigation bar by creating a new NavigationPage(new YourTabbedPage) and then you can use the code to hide the NavigationBar
NavigationPage.SetHasNavigationBar (this,false);
You can have different NavigationPage MainPage of your App.cs you can have an instance of your App.cs like this:
public static App Instance { get; private set; }
So now you can set:
App.Instance.MainPage = new NavigationPage(new YoutTabbedPage());
Hope this helps.
I am using tweetsharp nuget package for adding tweets to my website.
I put my partialview on footer and it is visible on everpage of website.
here is my codes on Basecontroller.
public PartialViewResult _PartialView_twitter_feed()
{
var service = new TwitterService("key", "key");
service.AuthenticateWith("key", "key");
IEnumerable<TwitterStatus> tweets = service.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions { ScreenName="my_screen_name", Count=5 });
ViewBag.Tweets = tweets;
return PartialView();
}
and I call it like this on footer view.
#Html.Action("_PartialView_twitter_feed", "base")
Now is the question, How can I make output caching or some kind of caching for this implementation on asp net mvc 5?
I dont want to call twitter api in every page view again and again. Because partial view is on footer.
How can I make it only once and show it until user leaves my website?
What can be the best practise to achive this?
Thanks for any help.
You can use OutputCache attribute. I hope the following example will help you.
Controller
[OutputCache(Duration = 6000)]
public PartialViewResult Footer()
{
return PartialView("Footer");
}
public ActionResult MainPage1()
{
return View();
}
public ActionResult MainPage2()
{
return View();
}
View
MainPage1
<h2>MainPage1</h2>
#Html.Action("Footer")
MainPage2
<h2>MainPage2</h2>
#Html.Action("Footer")
When user access the MainPage1 first time, the Footer partial view will return from server. From second time onwards it will return from cache. Even when user access MainPage2 the footer partial view will return from cache. You can increase the duration seconds based on your requirement