I have a very simple TabBar with a few ShellContent elements such as this:
<ShellContent
Title="Staying at Home"
Icon="house_user_solid.png"
ContentTemplate="{DataTemplate views:PatientEdSelectionPage}">
<views:PatientEdSelectionPage />
</ShellContent>
The ViewModel looks like this:
namespace Lati2d.ViewMode
{
public partial class PatientEdViewModel : BaseViewModel
{
public ObservableCollection<PatientEd> PatientEds { get; } = new();
public ObservableCollection<Lati2d.Model.Condition> Conditions { get; } = new();
public string PatientEd { get; set; }
public string PatientEdTypeId { get; set; }
public Guid ConditionId { get; set; }
public string ConditionName { get; set; }
public Lati2d.Model.Condition SelectedCondition { get; set; }
PatientEdService _patientEdService;
ConditionService _conditionService;
public PatientEdViewModel(
PatientEdService patientEdService,
ConditionService conditionService)
{
_patientEdService = patientEdService;
_conditionService = conditionService;
Title = "Patient Education Titles";
}
}
}
This all works great. I'm now trying to get a value from the ShellContent to the page. I'm happy to pass some sort of parameter or use some sort of reference from the Page, back to the 'referrer'.
I've tried the QueryParameter attribute and a few other methods I've seen, most seem to use direct navigation, but can't seem to get it to work.
[Rewrote answer]
Instead of inlining
PatientEdSelectionPage
:Consider expanding it then, you can easily add properties:
Also, you may also like to use
StaticResource
[Old Answer]
This answer involves passing properties throughout your entire application via a
GlobalViewModel
singleton.As you're using Dependency Injection, set it up like this:
In your
AppShell.xaml.cs
, you can gain access to theGlobalViewModel
singleton. That way, the Shell can provide values via theGlobalViewModel
singleton.Because
GlobalViewModel
is a singleton, you can set properties fromShell
and you can consume those same properties inPatentEdViewModel
. For the latter, you can configurePatentEdViewModel
via dependency injection:The comment about declaring
GlobalViewModel
in theApp
BindingContext
should allow access to theGlobalViewModel
in your bindings, by settingSource={RelativeSource AncestorType={x:Type viewmodel:GlobalViewModel}}
, but, setting it as a dependency to your individual view models works too.