Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
Navegación de página usando MVVM en Xamarin.Forms

Estoy trabajando en la aplicación multiplataforma xamarin.form, quiero navegar de una página a otra al hacer clic en el botón. Como no puedo hacer Navigation.PushAsync(new Page2()); en ViewModel porque solo es posible en el archivo Code-Behid. por favor sugiera alguna forma de hacer esto?

Aquí está mi vista:

 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Calculator.Views.SignIn" xmlns:ViewModels="clr-namespace:Calculator.ViewModels;assembly=Calculator"> <ContentPage.BindingContext> <ViewModels:LocalAccountViewModel/> </ContentPage.BindingContext> <ContentPage.Content> <StackLayout> <Button Command="{Binding ContinueBtnClicked}" /> </StackLayout> </ContentPage.Content> </ContentPage>

Aquí está mi modelo de vista:

 public class LocalAccountViewModel : INotifyPropertyChanged { public LocalAccountViewModel() { this.ContinueBtnClicked = new Command(GotoPage2); } public void GotoPage2() { ///// } public ICommand ContinueBtnClicked { protected set; get; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanges([CallerMemberName] string PropertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); } }
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Una forma es pasar la Navegación a través de VM Constructor. Dado que las páginas heredan de VisualElement , heredan directamente la propiedad de Navigation .

Código detrás del archivo:

 public class SignIn : ContentPage { public SignIn(){ InitializeComponent(); // Note the VM constructor takes now a INavigation parameter BindingContext = new LocalAccountViewModel(Navigation); } }

Luego, en su máquina virtual, agregue una propiedad INavigation y cambie el constructor para aceptar INavigation . A continuación, puede utilizar esta propiedad para la navegación:

 public class LocalAccountViewModel : INotifyPropertyChanged { public INavigation Navigation { get; set;} public LocalAccountViewModel(INavigation navigation) { this.Navigation = navigation; this.ContinueBtnClicked = new Command(async () => await GotoPage2()); } public async Task GotoPage2() { ///// await Navigation.PushAsync(new Page2()); } ... }

Tenga en cuenta un problema con su código que debe solucionar: el método GoToPage2() debe configurarse de forma async y devolver el tipo de Task . Además, el comando realizará una llamada de acción asincrónica. ¡Esto se debe a que debe navegar por la página de forma asincrónica!

¡Espero eso ayude!

about 4 years ago · Santiago Trujillo Report

0

Una forma sencilla es

 this.ContinueBtnClicked = new Command(async()=>{ await Application.Current.MainPage.Navigation.PushAsync(new Page2()); });
about 4 years ago · Santiago Trujillo Report

0

Desde tu máquina virtual

 public Command RegisterCommand { get { return new Command(async () => { await Application.Current.MainPage.Navigation.PushAsync(new RegisterNewUser()); }); } }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!