0
votes

I have a xamarin.forms app using FreshMvvm. The following method in App.xaml.cs handles a push notification:

   public static async void NewCall(string message)
    {
        ...
        await Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
        {          
            if (!IsVideoCallViewOpen)
            {          
                var navService = FreshIOC.Container.Resolve<IFreshNavigationService>(FreshMvvm.Constants.DefaultNavigationServiceName);
                Page videoCallPage = FreshPageModelResolver.ResolvePageModel<VideoCallPageModel>();
                await navService.PushPage(videoCallPage, null);
            }

            VideoCallMessage caller = JsonConvert.DeserializeObject<VideoCallMessage>(message);
            VideoCallPeer peer = new VideoCallPeer(caller.UserId, caller.UserName, caller.FirstName);

            OnStartCall?.Invoke(null, peer);
        });
    }

As you can see, navService.PushPage() opens a certain VideoCallPage, if needed. Only after that OnStartCall event should be fired. But it does not happen this way. It fires BEFORE VideoCallPageModel's Init() method finishes execution, which causes various troubles. How can I make sure OnStartCall event doesn't fire before the VideoCallPage is done loading?

Here is the VideoCallPageModel's Init signature:

public async override void Init(object initData)

And here is VideoCallPageModel's StartCallHandler() that handles OnStartCall event:

public void StartCallHandler(object sender, VideoCallPeer remotePeer)
1
Have you thought about using Messaging Center? You could send a message once you are sure you are ready and then whatever is subscribed to it, will do the work. - Gaz Winter
@GazWinter Thank you for your suggestion. I think it may work. But it would add additional complexity to the design. I will try it if there is no simpler way. I would like to know what causes my problem. It seems like I am missing something about FreshMvvm page lifecycle. - David Shochet
@Gaz Winter Thank you again. Please make it an Answer, and I will mark it as such. - David Shochet

1 Answers

1
votes

Have you thought about using Messaging Center?

Messaging Center Documentation

You could send a message once you are sure you are ready and then whatever is subscribed to it, will do the work for you once the Message has been received.