Pass ReactiveObject ViewModel between Android Activity in Xamarin
I am using ReactiveObject
both ViewModel
in Xamarin.PCL
:
public class PlanViewerViewModel : ReactiveObject
There ViewModel
is a lot of logic inside ReactiveUI
, for example:
FilterIssueTypeCommand = ReactiveCommand.Create<string, List<IGrouping<int, StandardIssueTypeTable>>>(
searchTerm =>
{
Func<StandardIssueTypeTable, bool> filterIssueType = d =>
string.IsNullOrEmpty(this.IssueTypeSearchQuery) ? true :
Contains(d.Title, this.IssueTypeSearchQuery) ||
Contains(d.Category.Title, this.IssueTypeSearchQuery) ||
Contains(d.Issues.Count().ToString(), this.IssueTypeSearchQuery);
return RealmConnection.All<StandardIssueTypeTable>().Where(filterIssueType)
.GroupBy(g => g.CategoryId)
.ToList();
}
);
this.WhenAnyValue(x => x.IssueTypeSearchQuery)
.Throttle(TimeSpan.FromMilliseconds(500), RxApp.MainThreadScheduler)
.Select(x => x?.Trim())
.DistinctUntilChanged()
.InvokeCommand(FilterIssueTypeCommand);
_groupedStandardIssueType = FilterIssueTypeCommand.ToProperty(this, x => x.GroupedStandardIssueType, new List<IGrouping<int, StandardIssueTypeTable>>());
In development, Xamarin.iOS
I could easily pass ViewModel
between each controller with this code:
var finderController = segue.DestinationViewController as PVFinderTabBarController;
finderController.ViewModel = this.ViewModel;
I am wondering how could I do this in Xamarin.Android
?
I know there are several ways to transfer data between Activity
:
- Give it as
string
,int
,bool
: This is what I'm using now - Serialize object to
JSON
: slow - Embed
Parcelable
orSerializable
: I don't want to use it because that means I need to copy the projectViewModel
toXamarin.Android
.
If I do that, there is no point in using ReactiveUI
.
I am thinking to use the class Application
in Android
and create one instance of mine ViewModel
in it so that everyone Activity
can reference this. Something like that:
[Application]
public class GlobalState: Application
{
public PlanViewerViewModel viewModel { get; set; }
}
Is there any other solution?
source to share
No one has answered this question yet
Check out similar questions: