Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm currently working on a Xamarin.forms project using .NET Standard as code sharing strategy. I try to use the MVVM pattern by using the MvvmLightLibsStd10 library. I already successfully setup the MVVM structure by using this tutorial: https://www.linkedin.com/pulse/build-xamarinforms-net-standard-mvvm-light-app-rafael-carvalho

I can't use Navigation.PushAsync(new Page()); because it only works in code behind and not in the ViewModel.

I already tried to Pass Navigation trough the VM constructor, like describe over here:
Xamarin.form Page Navigation in mvvm

But when I try this method, an error occurred at "LoadApplication(new DemoMVVM2.App());" in MainPage.

How can I switch pages using MVVM Xamarin.Forms with MVVMLight (based on the code from my first url)?

but I have no Idea how I can switch Pages via the ViewModel and keeping the header with back button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

Generally when working with MVVMLight you'll be using a NavigationService. This class can be constructor injected in your VM, thanks to the build in IOC in MVVMLight. With it you can do Navigate and GoBack in your VM, triggering a real navigation on the current stack.

Only thing that you maybe missed, is the fact that you need to write one yourself for Xamarin forms. But Laurent Bugnion ( the owner of MVVMLight ) supplied an example available here: https://github.com/lbugnion/sample-2016-vslive-crossplatform/blob/master/Flowers/Flowers.Forms/Flowers.Forms/Helpers/NavigationService.cs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...