Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

Xamarin.Forms How to switch pages using MVVMLight

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...