My app breaks if I add LoginViewModel

Eduardo Gomez 3,651 Reputation points
2025-04-27T15:46:29.04+00:00

For some reason my app breaks, when I add the loginViewModel to the mainPage

I am using firebase, so I register the user, and navigate to the Main Page

  builder.Services.AddTransient<RegisterPopUp>();
  builder.Services.AddSingleton<AppShell>();
  builder.Services.AddTransient<MainViewModel>();
  builder.Services.AddSingleton<MainPage>();
  builder.Services.AddSingleton<LoginPage>();
  builder.Services.AddTransient<LoginPageViewModel>();


Login page

public partial class LoginPageViewModel : BaseViewModel {

    public required Action openPopUp;
    public required Action closePopUp;
    private FirebaseAuthClient _authClient;

    public LoginPageViewModel(FirebaseAuthClient authClient) {
        _authClient = authClient;

        if(authClient.User != null) {
            Shell.Current.GoToAsync($"{nameof(MainPage)}");
        }
    }

    [ObservableProperty]
    LocalUser _localUser = new();


    [RelayCommand]
    async Task Login() {

        await _authClient.SignInWithEmailAndPasswordAsync(LocalUser.Email, LocalUser.Password);

        await Shell.Current.GoToAsync($"{nameof(MainPage)}");
    }

    [RelayCommand]
    async Task Register() {
        openPopUp.Invoke();

    }

    // Popup Commands
    [RelayCommand]
    void ClosePopUp() {
        closePopUp.Invoke();
    }

    [RelayCommand]
    async Task SignUp() {

        await _authClient.CreateUserWithEmailAndPasswordAsync(
            LocalUser.Email,
            LocalUser.Password,
            LocalUser.Username);

        closePopUp.Invoke();

        await Shell.Current.GoToAsync($"{nameof(MainPage)}");
    }


 readonly RegisterPopUp _registerPopUp;
 public LoginPage(LoginPageViewModel loginPageViewModel, RegisterPopUp registerPopUp) {
     InitializeComponent();
     BindingContext = loginPageViewModel;
     _registerPopUp = registerPopUp;
     loginPageViewModel.openPopUp = () => {
         _registerPopUp.IsOpen = true;
     };
     loginPageViewModel.closePopUp = () => {
         _registerPopUp.IsOpen = false;
     };
 }


AppShell



public partial class AppShell : Shell {
    public AppShell() {

        InitializeComponent();

        Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
    }
}

so

I will show you what is going on

Untitled video - Made with Clipchamp

the app is in

https://github.com/eduardoagr/MauiSamples

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,114 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.