Navigate from viewmodel

3 posts, 1 answers
  1. Joe Bohen
    Joe Bohen avatar
    71 posts
    Member since:
    May 2009

    Posted 04 Nov Link to this post

    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. Answer
    Lance | Team Lead - US DevTools Support
    Admin
    Lance | Team Lead - US DevTools Support avatar
    1045 posts

    Posted 04 Nov Link to this post

    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
  3. Joe Bohen
    Joe Bohen avatar
    71 posts
    Member since:
    May 2009

    Posted 04 Nov in reply to Lance | Team Lead - US DevTools Support Link to this post

    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.
Back to Top