I use ReactiveObject as a ViewModel in Xamarin.PCL:
public class PlanViewerViewModel : ReactiveObject
Inside the ViewModel, there are a lot of ReactiveUI logic such as:
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 Xamarin.iOS development, I could easily pass the ViewModel between each controller using this code:
var finderController = segue.DestinationViewController as PVFinderTabBarController;
finderController.ViewModel = this.ViewModel;
I am wondering how could I do it in Xamarin.Android?
I know there are several ways to pass data between Activity:
- Pass it as a
string,int,bool: This is what I am currently use - Serialize the object into
JSON: slow - Implement
ParcelableorSerializable: I don't want to use it because that means I need to copy myViewModelintoXamarin.Androidproject.
If I do it that way, there is no point in using ReactiveUI.
I am thinking of using Application class in Android and create one instance of my ViewModel in it so every Activity could refer to that. Something like this:
[Application]
public class GlobalState: Application
{
public PlanViewerViewModel viewModel { get; set; }
}
Is there any other solution?