Widgetaah
Widgetaah

Reputation: 31

FreshMVVM Navigation throws an exception when leaving a Modal Page

I am using a LocalNotification plugin(nuget) to generate a Phone Notification upon an event, which works fine. If the user navigates to a page which lists the notifications and selects one, it goes to the NotificationPageModel.cs page. From there they press a button which navigates them away, which works as long as it is manually navigated to.

If the user clicks on the phone's notification (app.xaml.cs:OnLocalNotificationTapped) then when trying to navigate away, it generates a FreshTinyIoC.TinyIoCResolutionException.

How can I navigate away from the page when the user has clicked the notification (masterDetailNav.Navigation.PushModalAsync(npage);) ?

Relevant versions: Xamarin.Forms v3.6.0.264807 FreshMvvm v3.0.0 Plugin.LocalNotification by Thudugala v4.0.5

App.xaml.cs

void OnLocalNotificationTapped(NotificationTappedEventArgs e)
{
    var _logger = FreshMvvm.FreshIOC.Container.Resolve<ILoggingService>();
    _logger.Info("Pressed notification: {0}", e.Data);
    Notification notification = new Notification();
    if (string.IsNullOrWhiteSpace(e.Data))
    {
        return;
    }
    else
    {
        notification = JsonConvert.DeserializeObject<Notification>(e.Data);
    }

    //CoreMethods.PushPageModel<NotificationPageModel>(notification); //cant find coremethods
    var npage = FreshPageModelResolver.ResolvePageModel<NotificationPageModel>(notification);
    masterDetailNav.Navigation.PushModalAsync(npage);
}

NotificationPageModel.cs:

public async void Cancel()
{
    _logger.Info("Notification {0} Cancel: {1}", Notification.Gateway, Notification.TimeStamp);

    //Navigate away from page
    //await CoreMethods.PopPageModel(); //this only works if manually navigated to
    //attempt #1
    await CoreMethods.PushPageModel<HomePageModel>();
}

public async void Remove()
{
    _logger.Info("Notification {0} Remove: {1}", Notification.Gateway, Notification.TimeStamp);

    //Remove notification
    try
    {
        _user.Notifications.Remove(Notification);
    }
    catch (Exception ex)
    {
        _logger.Warn("Remove Notification: {0}", ex.Message);
    }

    //Navigate away from page
    //await CoreMethods.PopPageModel(); //this only works if manually navigated to
    //attempt #2
    var hpage = FreshPageModelResolver.ResolvePageModel<HomePageModel>();
    await CoreMethods.PushPageModelWithNewNavigation<HomePageModel>(hpage);
}

Stacktrace

FreshTinyIoC.TinyIoCResolutionException Message=Resolve failed: IFreshNavigationService Source=mscorlib StackTrace: at FreshTinyIoC.FreshTinyIoCContainer.ResolveInternal (FreshTinyIoC.FreshTinyIoCContainer+TypeRegistration registration, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x000f7] in C:\Projects\FreshMvvm\src\FreshIOC\FreshTinyIOC.cs:3142 at FreshTinyIoC.FreshTinyIoCContainer.Resolve (System.Type resolveType, System.String name) [0x00000] in C:\Projects\FreshMvvm\src\FreshIOC\FreshTinyIOC.cs:1211 at FreshTinyIoC.FreshTinyIoCContainer.Resolve[ResolveType] (System.String name) [0x00000] in C:\Projects\FreshMvvm\src\FreshIOC\FreshTinyIOC.cs:1332 at FreshMvvm.FreshTinyIOCBuiltIn.Resolve[ResolveType] (System.String name) [0x00000] in C:\Projects\FreshMvvm\src\FreshMvvm\FreshTinyIOCBuiltIn.cs:31 at FreshMvvm.PageModelCoreMethods.PushNewNavigationServiceModal (FreshMvvm.IFreshNavigationService newNavigationService, FreshMvvm.FreshBasePageModel[] basePageModels, System.Boolean animate) [0x00073] in C:\Projects\FreshMvvm\src\FreshMvvm\PageModelCoreMethods.cs:178 at FreshMvvm.PageModelCoreMethods.PushPageModelWithNewNavigation[T] (System.Object data, System.Boolean animate) [0x00060] in C:\Projects\FreshMvvm\src\FreshMvvm\PageModelCoreMethods.cs:235 at cdaxrobot.PageModels.NotificationPageModel.Go () [0x00225] in C:\Work\RobotApp\App\cdaxrobot\cdaxrobot\cdaxrobot\PageModels\NotificationPageModel.cs:113 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in <11f101b564894ca7af6c482ddc51c698>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <11f101b564894ca7af6c482ddc51c698>:0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <11f101b564894ca7af6c482ddc51c698>:0 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.41(intptr,intptr)

Upvotes: 0

Views: 893

Answers (1)

Widgetaah
Widgetaah

Reputation: 31

Thank you for the response. I managed to fix it by creating a new NavigationContainer:

notificationPage = FreshMvvm.FreshPageModelResolver.ResolvePageModel<NotificationPageModel>(true);
notificationContainer = new FreshNavigationContainer(notificationPage, Models.Constants.NotificationContainer);

void OnLocalNotificationTapped(NotificationTappedEventArgs e)
{
    MainPage = notificationContainer;
}

and then in the page I do this:

if (CameFromNotification == true)
{
    _logger.Debug("Navigating back to MainContainer");
    CoreMethods.SwitchOutRootNavigation(Models.Constants.MainContainer);
}
else
{
    _logger.Debug("Navigating by popping the stack");
    await CoreMethods.PopPageModel();
}

Upvotes: 0

Related Questions