If simple navigation parameters at MvvmCross is not enough. And we need to share some data after collecting from the database or after some user modifications. We should just add behavior for our root tabbed page. 😉
Here we have true freedom about the way. But as the example I will show you two ways:
- More Generic with Interfaces
- Strongly Typed for special solution.
Here we will pass parameters by IParametrizedViewModel
interface where have only one field:
public interface IParametrizedViewModel
{
ObservableCollection<User> UserCollection { get; set; }
}
All our view models for tabs are implement it.
- Creating new class at your Xamarin.Forms project inherited from
Behavior<MvxTabbedPage>
- Create two local fields: one for our bindable object(
AssociatedObject
) and second for last view model(_currentViewModel
) - Override
OnAttachedTo
andOnDetachingFrom
- There add subscription to
CurrentPageChanged
event onbindable
- Assign
bindable
into ourAssociatedObject
field. - At our function assigned to
CurrentPageChanged
event we should:- Check if
AssociatedObject.CurrentPage
isIMvxPage
(base interface for all MvvmCross pages)(p
) - Check if
p.ViewModel
isIParametrizedViewModel
(view model that we need)(newViewModel
) - Assign data from
_currentViewModel
intonewViewModel
- Rewrite reference of
_currentViewModel
intonewViewModel
- Check if
- Attach new behavior at
HomePage.xaml
:
<views:MvxTabbedPage ...>
<TabbedPage.Behaviors>
<behavior:ParametrizedTabBehavior/>
</TabbedPage.Behaviors>
</views:MvxTabbedPage>
Here we want to cover only one specific scenario - passing Secret User.
Property SecretUser
is only at DetailsViewModel
and at ThirdDetailsViewModel
, it's missed at SecondDetailsViewModel
:
private User _secretUser;
public User SecretUser
{
get => _secretUser;
set => SetProperty(ref _secretUser, value);
}
Here we also can add one more interface and passing values only for view models that implement it. But here we want to pass that property from DetailsViewModel
to ThirdViewModel
, because the last one can't generate or modify it.
- Creating new class at your Xamarin.Forms project inherited from
Behavior<MvxTabbedPage<HomeViewModel>>
- Create one local fields for our bindable object(
AssociatedObject
) - Override
OnAttachedTo
andOnDetachingFrom
- There add subscription to
CurrentPageChanged
event onbindable
- Assign
bindable
into ourAssociatedObject
field. - At our function assigned to
CurrentPageChanged
event we should:- Check if
AssociatedObject.CurrentPage
isIMvxPage
(base interface for all MvvmCross pages)(p
) - Call function
OnSelectedTabChanged
from ourHomeViewModel
and pass like an argument page view modelp.ViewModel
- Check if
- At
HomeViewModel
inside our functionOnSelectedTabChanged
we should:- Check if
_previousViewModel
isDetailsViewModel
andselectedViewModel
isThirdDetailsViewModel
. - Then just assign
SecretUser
fromDetailsViewModel
intoThirdDetailsViewModel
- Check if
- Attach new behavior at
HomePage.xaml
:
<views:MvxTabbedPage ...>
<TabbedPage.Behaviors>
<behavior:ParametrizedTabBehavior/>
<behavior:HomeSecretBehavior/>
</TabbedPage.Behaviors>
</views:MvxTabbedPage>