首页 > 解决方案 > How to navigate through modal pages in Xamarin.Forms using Fresh.MVVM

问题描述

I was trying to push a modal page in my xamarin forms app using MVVM when pressing a button. I already know how to use navigation stacks, but no idea how to do it with content pages as modal pages, I have tried multiple things, especially, calling the PushPageModel method.

This is the last thing I have tried: View or Page:

<Label
                x:Name="forgottenPasswordLabel"
                Text="Forgot password?"
                TextColor="LightPink"
                FontSize="16"
                FontAttributes="Bold"

                VerticalOptions="Start"
                HorizontalOptions="End"
                Margin="25,0,25,25">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ForgotPasswordCommand}"/>
                </Label.GestureRecognizers>
</Label>

ViewModel class:

public class LogInViewModel : FreshBasePageModel
    {
        public ICommand ForgotPasswordCommand { get; set; }

        public LogInViewModel()
        {
        }

        public override void Init(object initData)
        {
            ForgotPasswordCommand = new Command (async() =>
            {
                var newPage = FreshPageModelResolver.ResolvePageModel<ForgottenPasswordViewModel>();
                await CoreMethods.PushPageModel<ForgottenPasswordViewModel>(null, false, true);
            }); 
        }
    }

App.xaml.cs class:

public App()
        {
            InitializeComponent();

            MainPage = FreshPageModelResolver.ResolvePageModel<LogInViewModel>();
        }

This code gives the following error: "FreshTinyIoC.TinyIoCResolutionException: 'Resolve failed: IFreshNavigationService"

And that is all, if you need more information I will provide it as soon as I see your request, thank you all for your time, hope you have a great day.

Edit: Stack trace requested:

at FreshTinyIoC.FreshTinyIoCContainer.ResolveInternal (FreshTinyIoC.FreshTinyIoCContainer+TypeRegistration registration, FreshTinyIoC.NamedParameterOverloads parameters, FreshTinyIoC.ResolveOptions options) [0x000f7] in C:"Here goes the path" at FreshTinyIoC.FreshTinyIoCContainer.Resolve (System.Type resolveType, System.String name) [0x00000] in C:\"Here goes the path" at FreshTinyIoC.FreshTinyIoCContainer.Resolve[ResolveType] (System.String name) [0x00000] in C:"Here goes the path" at FreshMvvm.FreshTinyIOCBuiltIn.Resolve[ResolveType] (System.String name) [0x00000] in C:"Here goes the path" at FreshMvvm.PageModelCoreMethods.PushPageModelWithPage (Xamarin.Forms.Page page, FreshMvvm.FreshBasePageModel pageModel, System.Object data, System.Boolean modal, System.Boolean animate) [0x00177] in C:"Here goes the path" at FreshMvvm.PageModelCoreMethods.PushPageModel (FreshMvvm.FreshBasePageModel pageModel, System.Object data, System.Boolean modal, System.Boolean animate) [0x00048] in C:"Here goes the path" at FreshMvvm.PageModelCoreMethods.PushPageModel[T] (System.Object data, System.Boolean modal, System.Boolean animate) [0x00040] in C:"Here goes the path" at FirstApp.ViewModels.LogInViewModel.b__5_0 () [0x0003a] in D:\"Here goes the path"

标签: c#xamarinxamarin.formsfreshmvvm

解决方案


The problem was a misunderstanding of concepts, I thought that a content page and a navigation page were two different types of pages, like a tabbed page, master detailed page, carousel page, and content page. So I was trying not to use the following code thinking that FreshNavigationContainer forced me to use a navigation page and not a content page, which is true but not completely, because yes, it forces you to use a navigation page, but you are also using a content page, since navigation page is a property of content page, so as I said it was all a misunderstanding of concepts :

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            var thePage = FreshPageModelResolver.ResolvePageModel<LogInViewModel>();
            MainPage = new FreshNavigationContainer(thePage);
        }
}

I didn't say this on the question, but the purpose of all of this was to not see a bar at the top of the screen, which now I understand that because a navigation page and a content page are not 2 different things, you can do it in the page.xaml with:

<ContentPage NavigationPage.HasNavigationBar="False">

推荐阅读