Finally, we need to add some life cycle events that will take care of our SignalR connection in case the app goes to sleep or when it wakes up again:
- Open the App.xaml.cs file.
- Add the following code somewhere inside the App class:
protected override void OnSleep()
{
var chatService = Resolver.Resolve<IChatService>();
chatService.Dispose();
}
protected override void OnResume()
{
Task.Run(async() =>
{
var chatService = Resolver.Resolve<IChatService>();
if (!chatService.IsConnected)
{
await chatService.CreateConnection();
}
});
Page view = null;
if(ViewModel.User != null)
{
view = Resolver.Resolve<ChatView>();
}
else
{
view = Resolver.Resolve<MainView>();
}
var navigationPage = new NavigationPage(view);
MainPage = navigationPage;
}
The OnSleep() method will be called when the user minimizes the app. It will dispose of any...