I am developing a multi-platform app using Xamarin Forms, and I need to pass a parameter with the user informations from my LoginPage to my HarvestPage (it is an app for farmers, btw). I'm using MVVM architecture, but no MVVM framework.
So, the user performs authentication in LoginPage
and then the app navigates to my AppShell
where I have 4 bottom tabs.
In LoginPage.xaml.cs
I navigate to the first tab of my ShellPage (this page does not need parameters):
private async void OnLoginClicked(LoginViewModel sender, Usuario args)
{
Application.Current.MainPage = new AppShell(args);
await Shell.Current.GoToAsync("//market");
}
Then in my AppShell.xaml
page I declare the contents of my tabs:
<TabBar>
<ShellContent Title="Market" Icon="exchange.png" ContentTemplate="{DataTemplate local:MarketPage}" Route="market" />
<ShellContent Title="Harvest" Icon="sugarcane.png" ContentTemplate="{DataTemplate local:HarvestPage}" Route="harvest" />
...
</TabBar>
And in AppShell.xaml.cs
I configure the routes:
private Usuario _user;
public AppShell(Usuario user)
{
InitializeComponent();
_user = user;
Routing.RegisterRoute(nameof(HarvestPage), typeof(HarvestPage));
Routing.RegisterRoute(nameof(MarketPage), typeof(MarketPage));
...
}
The user infos are sent to AppShell.xaml.cs
but how do I pass the user parameter to my HarvestPage
?
I can't use GoToAsync
like it is suggested in other questions, because there is no button to navigate, the navigation from MarketPage to HarvestPage is through the bottom tabs.
CodePudding user response:
You may define as public:
public Usuario User;
and then access it from your HarvestPage
using:
(Shell.Current as AppShell).User