Question

I'm using the BTProgressHUD Xamarin component in an app that is built using MvvmCross. I'm currently working on the iOS version of the app. My ViewModels make several web service calls and expose an 'IsBusy' property, which the associated views are binding to, in order to show or hide the progress spinner. This is pretty much the way things are set up in the N=34 MvvmCross sample (https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-34-Progress) as well.

The problem is that in some cases a ViewModel must automatically call a service as soon as it is created. I tried to make the call in the Start() function of the ViewModel, but I noticed that the BTProgressHUD spinner does not show up on top of the view. I suspect that the problem is that BTProgressHUD must be displayed only after a view has been made visible, and probably this is not the case when ViewModel.Start() runs.

Has anyone encountered this before? Is there a simple way to run code in the ViewModel after the view has been made visible?

Thanks.

Was it helpful?

Solution

Is there a simple way to run code in the ViewModel after the view has been made visible?

The N=42 video - http://slodge.blogspot.co.uk/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html - introduces an IVisible interface that you can add to your ViewModel - it's your job to call this from your View - but this is easy to do on each platform. For example,, on iOS it is done using ViewDidAppear/ViewDidDisappear in https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-42-Lifecycles/Lifecycle.Touch/Views/FirstView.cs

    protected IVisible VisibleViewModel
    {
        get { return base.ViewModel as IVisible; }
    }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        VisibleViewModel.IsVisible(true);
    }

    public override void ViewDidDisappear(bool animated)
    {
        base.ViewDidDisappear(animated);
        VisibleViewModel.IsVisible(false);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top