4

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 Parcelable or Serializable: I don't want to use it because that means I need to copy my ViewModel into Xamarin.Android project.

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?

currarpickt
  • 2,290
  • 4
  • 24
  • 39
  • I'm still new to RxUI, but you could probably hack up a RxUI version of a View-based Navigation Service using Intent and looking at how [ MvvmLight accomplishes it - https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight.Platform (Android)/Views/NavigationService.cs ] You can additionally mix MVVMCross's ViewModel first approach for navigation and use RxUI for binding/rx/etc. Otherwise wait for ViewModel first approach to become reality: https://github.com/reactiveui/ReactiveUI/issues/1048 – Jon Douglas Aug 09 '17 at 06:15

0 Answers0