4

Strangely, I did not find a direct answer to the very simple question on the internet.

How do I determine currently active ViewModel in MvvmCross runtime?

Is it possible? If not, why am I not supposed to do it?

2
  • Could you perhaps try editing your "very simple question" so that it helps others work out what you are asking. What is a currently active ViewModel? Where are you asking? Is there any code or pseudo-code that might help others understand what you are talking about. Jon Skeet has some advice on asking good questions on msmvps.com/blogs/jon_skeet/archive/2012/11/24/…
    – Stuart
    Mar 18, 2014 at 10:47
  • Well, when the app is running there is always an active View which is shown on a screen. And obviously an active ViewModel corresponding to this view. Is there a global environment variable or method or whatever which I can use to determine this "active" ViewModel? Mar 18, 2014 at 13:22

2 Answers 2

15

As I learned recently, which ViewModel is "active" depends heavily on which Presenter you use. If you just use the default presenters, it seems easy because only one ViewModel is shown at any given time. However, with more advanced presenters, you can have multiple active ViewModels.

Since the current active ViewModel(s) depends on which Presenter you are using (which lives in the view layer), Mvx core can't know how to access it/them. If this is something you think you need, I would recommend implementing your own Presenter with your own interface.

Here's an example for iOS:

ICurrentViewModelPresenter.cs

public interface ICurrentViewModelPresenter
{
    IMvxViewModel CurrentViewModel { get; }
}

CurrentViewModelPresenter.cs:

public class CurrentViewModelPresenter : MvxTouchViewPresenter, ICurrentViewModelPresenter
{
    public CurrentViewModelPresenter(UIApplicationDelegate del, UIWindow win)
        : base(del, win)
    {
    }

    public IMvxViewModel CurrentViewModel
    { 
        get
        {
            var viewController = MasterNavigationController.TopViewController;
            if (viewController == null) return null;

            var touchView = viewController as IMvxTouchView;
            if (touchView == null) return null;

            return touchView.ReflectionGetViewModel();
        }
    }
}

Setup.cs:

public class Setup : MvxTouchSetup
{
    private readonly MvxApplicationDelegate _del;
    private readonly UIWindow _win;

    public Setup(MvxApplicationDelegate del, UIWindow win)
        : base(del, win)
    {
        _del = del;
        _win = win;
    }

    ...

    protected override IMvxTouchViewPresenter CreatePresenter()
    {
        var presenter = new CurrentViewModelPresenter(_del, _win);

        Mvx.RegisterSingleton<ICurrentViewModelPresenter>(presenter);

        return presenter;
    }
}

Anywhere in your code:

var presenter = Mvx.Resolve<ICurrentViewModelPresenter>(); // or inject with IoC
var current = presenter.CurrentViewModel;

Note that exactly none of this was tested, but it should give you an idea of how it would work.

1
  • 1
    Thank you Bognar, I get your idea. Now I understand why there is no standard way of doing this. Mar 19, 2014 at 11:10
0

Another way if you only want to see from your ViewModel if it's active/visible is just to have a bool field and update it on ViewAppeared / ViewDisappeared:

public class MyViewModel : MvxViewModel
{
    private bool _isVisible;

    public override void ViewAppeared()
    {
        base.ViewAppeared();

        _isVisible = true;
    }

    public override void ViewDisappeared()
    {
        base.ViewDisappeared();

        _isVisible = false;
    }
}

Most of the times this is enough and you can use _isVisible to check that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.