This is a migrated thread and some comments may be shown as answers.

Navigate from viewmodel

2 Answers 605 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joe Bohen
Top achievements
Rank 1
Joe Bohen asked on 04 Nov 2019, 12:20 PM
I am new to Xamarin and have written a test application which uses the Telerik template LoginScreenSimple to authenticate users when a user successfully logs on the app should then navigate to another form.  In the scenario above the  code behind is handled in a viewmodel how do I then load a content page from the viewmodel?

2 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 04 Nov 2019, 03:23 PM

Hello Joe,

Navigation in MVVM (i.e. a view model class) is usually done via a MVVM framework's navigation classes (e.g. see Prism's NavigationService).

If you are not using an MVVM framework, the most common approach is to just call navigation methods via App.Current.MainPage reference.  For example, if MainPage was a NavigationPage, you can do this:

public class MyViewModel
{
    private async Task GoToNextPage()
    {
        await (Application.Current.MainPage as NavigationPage).PushAsync(new MySecondPage())
    }

}

Note: referencing App.Current references the view, thus is not 100% view - view model separation. If you must be 100% MVVM pure, then you'll need to consider other options like using Prism or writing your own interface.

For more assistance on this topic, I recommend visiting the Xamarin, Prism or StackOverflow forums. The Telerik forums primarily focus on using the UI for Xamarin controls and aren't a perfect resource for general Xamarin questions.

Regards,
Lance | Team Lead - US DevTools Support
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joe Bohen
Top achievements
Rank 1
answered on 04 Nov 2019, 09:16 PM
Hi Lance,  Thanks for your reply,  I appreciate that this a more general question and perhaps better suited to one of the other sites.  However your suggestion did work for me.
Tags
General Discussions
Asked by
Joe Bohen
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Joe Bohen
Top achievements
Rank 1
Share this question
or